From 39569db8365eaa9b79f2c9ffe5ca7f7f1dc1d65e Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Fri, 31 Mar 2017 15:54:46 -0400 Subject: [PATCH] Refactor update_email -> set_primary_email - push down into finish_email_verification - bring your own cursor --- gratipay/models/participant/email.py | 21 ++++++++++++++------- www/~/%username/emails/modify.json.spt | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/gratipay/models/participant/email.py b/gratipay/models/participant/email.py index 3179d11afa..1ed2f213fa 100644 --- a/gratipay/models/participant/email.py +++ b/gratipay/models/participant/email.py @@ -196,13 +196,14 @@ def start_package_claims(self, c, nonce, *packages): ) - def update_email(self, email): - """Set the email address for the participant. + def set_primary_email(self, email, cursor=None): + """Set the primary email address for the participant. """ if not getattr(self.get_email(email), 'verified', False): raise EmailNotVerified() username = self.username - with self.db.get_cursor() as c: + + def _set(c): self.app.add_event( c , 'participant' , dict(id=self.id, action='set', values=dict(primary_email=email)) @@ -211,7 +212,14 @@ def update_email(self, email): UPDATE participants SET email_address=%(email)s WHERE username=%(username)s - """, locals()) + """, dict(email=email, username=username)) + + if cursor: + _set(cursor) + else: + with self.db.get_cursor() as cursor: + _set(cursor) + self.set_attributes(email_address=email) @@ -232,9 +240,6 @@ def verify_email(self, email, nonce): self.finish_email_verification(email, nonce) except IntegrityError: return VERIFICATION_STYMIED - - if not self.email_address: - self.update_email(email) return VERIFICATION_SUCCEEDED @@ -249,6 +254,8 @@ def finish_email_verification(self, email, nonce): AND address=%s AND verified IS NULL """, (self.id, email)) + if not self.email_address: + self.set_primary_email(email, c) def get_email(self, email): diff --git a/www/~/%username/emails/modify.json.spt b/www/~/%username/emails/modify.json.spt index e7d7452dc4..9b75a54e4d 100644 --- a/www/~/%username/emails/modify.json.spt +++ b/www/~/%username/emails/modify.json.spt @@ -46,7 +46,7 @@ if action in ('add-email', 'resend', 'start-verification'): participant.start_email_verification(address, *packages) msg = _("Check your inbox for a verification link.") elif action == 'set-primary': - participant.update_email(address) + participant.set_primary_email(address) elif action == 'remove': participant.remove_email(address) else: