From 003ca6e0bc0340159335dd1b47a35da79b7895e4 Mon Sep 17 00:00:00 2001 From: Brian Simpson Date: Thu, 8 Jan 2015 19:14:41 -0500 Subject: [PATCH] Add AccountsByCanonicalEmail. --- r2/r2/controllers/api.py | 10 +++++----- r2/r2/models/account.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/r2/r2/controllers/api.py b/r2/r2/controllers/api.py index 20efaf7f8..973bab74a 100644 --- a/r2/r2/controllers/api.py +++ b/r2/r2/controllers/api.py @@ -648,7 +648,7 @@ class ApiController(RedditController): #anything else we know (email, languages)? if email: - user.email = email + user.set_email(email) emailer.verify_email(user) user.pref_lang = c.lang @@ -1157,7 +1157,7 @@ class ApiController(RedditController): if c.user.email: emailer.email_change_email(c.user) - c.user.email = email + c.user.set_email(email) c.user.email_verified = None c.user._commit() Award.take_away("verified_email", c.user) @@ -1178,7 +1178,7 @@ class ApiController(RedditController): c.errors.remove((errors.NO_EMAILS, 'email')) if c.user.email: emailer.email_change_email(c.user) - c.user.email = '' + c.user.set_email('') c.user.email_verified = None c.user._commit() Award.take_away("verified_email", c.user) @@ -1245,7 +1245,7 @@ class ApiController(RedditController): if (not hasattr(c.user,'email') or c.user.email != email): if c.user.email: emailer.email_change_email(c.user) - c.user.email = email + c.user.set_email(email) # unverified email for now c.user.email_verified = None c.user._commit() @@ -1266,7 +1266,7 @@ class ApiController(RedditController): if (not email and c.user.email and (errors.NO_EMAILS, 'email') in c.errors): c.errors.remove((errors.NO_EMAILS, 'email')) - c.user.email = '' + c.user.set_email('') c.user.email_verified = None c.user._commit() Award.take_away("verified_email", c.user) diff --git a/r2/r2/models/account.py b/r2/r2/models/account.py index 5a515202b..11f825b1b 100644 --- a/r2/r2/models/account.py +++ b/r2/r2/models/account.py @@ -556,6 +556,12 @@ class Account(Thing): return rv + def set_email(self, email): + old_email = self.email + self.email = email + self._commit() + AccountsByCanonicalEmail.update_email(self, old_email, email) + def has_banned_email(self): canon = self.canonical_email() which = self.which_emails_are_banned((canon,)) @@ -937,3 +943,34 @@ def on_account_deletion(mature_items): account.password = "" account._commit() + + +class AccountsByCanonicalEmail(tdb_cassandra.View): + __metaclass__ = tdb_cassandra.ThingMeta + + _use_db = True + _compare_with = tdb_cassandra.UTF8_TYPE + _extra_schema_creation_args = dict( + key_validation_class=tdb_cassandra.UTF8_TYPE, + ) + + @classmethod + def update_email(cls, account, old, new): + old, new = map(canonicalize_email, (old, new)) + + if old == new: + return + + with cls._cf.batch() as b: + if old: + b.remove(old, {account._id36: ""}) + if new: + b.insert(new, {account._id36: ""}) + + @classmethod + def get_accounts(cls, email_address): + canonical = canonicalize_email(email_address) + if not canonical: + return [] + account_id36s = cls.get_time_sorted_columns(canonical).keys() + return Account._byID36(account_id36s, data=True, return_dict=False)