Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Refactor update_email -> set_primary_email
Browse files Browse the repository at this point in the history
- push down into finish_email_verification
- bring your own cursor
  • Loading branch information
chadwhitacre committed Mar 31, 2017
1 parent 2dd332b commit 39569db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions gratipay/models/participant/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)


Expand All @@ -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


Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion www/~/%username/emails/modify.json.spt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 39569db

Please sign in to comment.