Account: ensure all non-defaulted attributes are atomically created.

This prevents an issue where registration_ip is not set on the account
object causing downstream code to break. This happened because the app
would crash for whatever reason between committing in register() and
adding the IP and committing again in ApiController:_handle_register.
This commit is contained in:
Neil Williams
2012-10-25 10:54:24 -07:00
parent 0922ab4ec4
commit b55934ab6f
3 changed files with 5 additions and 5 deletions

View File

@@ -451,14 +451,13 @@ class ApiController(RedditController, OAuth2ResourceController):
responder.has_errors('ratelimit', errors.RATELIMIT) or
(not g.disable_captcha and bad_captcha)):
user = register(name, password)
user = register(name, password, request.ip)
VRatelimit.ratelimit(rate_ip = True, prefix = "rate_register_")
#anything else we know (email, languages)?
if email:
user.email = email
user.registration_ip = request.ip
user.pref_lang = c.lang
if c.content_langs == 'all':
user.pref_content_langs = 'all'

View File

@@ -749,7 +749,7 @@ def change_password(user, newpassword):
return True
#TODO reset the cache
def register(name, password):
def register(name, password, registration_ip):
try:
a = Account._by_name(name)
raise AccountExists
@@ -758,6 +758,7 @@ def register(name, password):
password = bcrypt_password(password))
# new accounts keep the profanity filter settings until opting out
a.pref_no_profanity = True
a.registration_ip = registration_ip
a._commit()
#clear the caches

View File

@@ -38,7 +38,7 @@ def populate(num_srs = 10, num_users = 1000, num_links = 100, num_comments = 20,
try:
a = Account._by_name(g.system_user)
except NotFound:
a = register(g.system_user, "password")
a = register(g.system_user, "password", "127.0.0.1")
srs = []
for i in range(num_srs):
@@ -60,7 +60,7 @@ def populate(num_srs = 10, num_users = 1000, num_links = 100, num_comments = 20,
in range(int(random.uniform(1, 10))) ])
name = 'test_' + name_ext
try:
a = register(name, name)
a = register(name, name, "127.0.0.1")
except AccountExists:
a = Account._by_name(name)
accounts.append(a)