#NEWLINE# def add_email( self , email , resend_threshold = '3 minutes' ) : #NEWLINE# #INDENT# '\n            This is called when\n            1) Adding a new email address\n            2) Resending the verification email for an unverified email address\n\n            Returns the number of emails sent.\n        ' #NEWLINE# owner = self . db . one ( '\n            SELECT p.username\n              FROM emails e INNER JOIN participants p\n                ON e.participant_id = p.id\n             WHERE e.address = %(email)s\n               AND e.verified IS true\n        ' , locals ( ) ) #NEWLINE# if owner : #NEWLINE# #INDENT# if ( owner == self . username ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise EmailAlreadyTaken ( email ) #NEWLINE# #UNINDENT# #UNINDENT# if ( len ( self . get_emails ( ) ) > 9 ) : #NEWLINE# #INDENT# raise TooManyEmailAddresses ( email ) #NEWLINE# #UNINDENT# nonce = str ( uuid . uuid4 ( ) ) #NEWLINE# verification_start = utcnow ( ) #NEWLINE# nrecent = self . db . one ( 'SELECT count(*) FROM emails WHERE address=%s AND %s - verification_start < %s' , ( email , verification_start , resend_threshold ) ) #NEWLINE# if nrecent : #NEWLINE# #INDENT# raise ResendingTooFast ( ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# with self . db . get_cursor ( ) as c : #NEWLINE# #INDENT# add_event ( c , 'participant' , dict ( id = self . id , action = 'add' , values = dict ( email = email ) ) ) #NEWLINE# c . run ( '\n                    INSERT INTO emails\n                                (address, nonce, verification_start, participant_id)\n                         VALUES (%s, %s, %s, %s)\n                ' , ( email , nonce , verification_start , self . id ) ) #NEWLINE# #UNINDENT# #UNINDENT# except IntegrityError : #NEWLINE# #INDENT# nonce = self . db . one ( '\n                UPDATE emails\n                   SET verification_start=%s\n                 WHERE participant_id=%s\n                   AND address=%s\n                   AND verified IS NULL\n             RETURNING nonce\n            ' , ( verification_start , self . id , email ) ) #NEWLINE# if ( not nonce ) : #NEWLINE# #INDENT# return self . add_email ( link ) #NEWLINE# #UNINDENT# #UNINDENT# base_url = gratipay . base_url #NEWLINE# username = self . username_lower #NEWLINE# encoded_email = encode_for_querystring ( email ) #NEWLINE# link = '{base_url}/~{username}/emails/verify.html?email2={encoded_email}&nonce={nonce}' #NEWLINE# r = self . send_email ( 'verification' , email = email , link = link . format ( ** locals ( ) ) , include_unsubscribe = False ) #NEWLINE# assert ( r == 1 ) #NEWLINE# if self . email_address : #NEWLINE# #INDENT# self . send_email ( 'verification_notice' , new_email = email , include_unsubscribe = False ) #NEWLINE# return 2 #NEWLINE# #UNINDENT# return 1