def recreate_api_key(self): api_key = str(uuid.uuid4()) SQL = "UPDATE participants SET api_key=%s WHERE username=%s" with self.db.get_cursor() as c: add_event(c, 'participant', dict(action='set', id=self.id, values=dict(api_key=api_key))) c.run(SQL, (api_key, self.username)) return api_key
def update_email(self, email, confirmed=False): with self.db.get_cursor() as c: add_event(c, 'participant', dict(id=self.id, action='set', values=dict(current_email=email))) c.one("UPDATE participants SET email = ROW(%s, %s) WHERE username=%s RETURNING id" , (email, confirmed, self.username) ) self.set_attributes(email=(email, confirmed))
def delete_elsewhere(self, platform, user_id): """Deletes account elsewhere unless the user would not be able to log in anymore. """ user_id = unicode(user_id) with self.db.get_cursor() as c: accounts = c.all( """ SELECT platform, user_id FROM elsewhere WHERE participant=%s AND platform IN %s """, (self.username, AccountElsewhere.signin_platforms_names)) assert len(accounts) > 0 if len(accounts) == 1 and accounts[0] == (platform, user_id): raise LastElsewhere() c.one(""" DELETE FROM elsewhere WHERE participant=%s AND platform=%s AND user_id=%s RETURNING participant """, (self.username, platform, user_id), default=NonexistingElsewhere) add_event( c, 'participant', dict(id=self.id, action='disconnect', values=dict(platform=platform, user_id=user_id))) self.update_avatar()
def update_goal(self, goal): typecheck(goal, (Decimal, None)) with self.db.get_cursor() as c: tmp = goal if goal is None else unicode(goal) add_event(c, 'participant', dict(id=self.id, action='set', values=dict(goal=tmp))) c.one( "UPDATE participants SET goal=%s WHERE username=%s RETURNING id" , (goal, self.username) ) self.set_attributes(goal=goal)
def recreate_api_key(self): api_key = str(uuid.uuid4()) SQL = "UPDATE participants SET api_key=%s WHERE username=%s" with self.db.get_cursor() as c: add_event( c, 'participant', dict(action='set', id=self.id, values=dict(api_key=api_key))) c.run(SQL, (api_key, self.username)) return api_key
def update_goal(self, goal): typecheck(goal, (Decimal, None)) with self.db.get_cursor() as c: tmp = goal if goal is None else unicode(goal) add_event(c, 'participant', dict(id=self.id, action='set', values=dict(goal=tmp))) c.one( "UPDATE participants SET goal=%s WHERE username=%s RETURNING id", (goal, self.username)) self.set_attributes(goal=goal)
def update_email(self, email, confirmed=False): with self.db.get_cursor() as c: add_event( c, 'participant', dict(id=self.id, action='set', values=dict(current_email=email))) c.one( "UPDATE participants SET email = ROW(%s, %s) WHERE username=%s RETURNING id", (email, confirmed, self.username)) self.set_attributes(email=(email, confirmed))
def set_as_claimed(self): with self.db.get_cursor() as c: add_event(c, 'participant', dict(id=self.id, action='claim')) claimed_time = c.one("""\ UPDATE participants SET claimed_time=CURRENT_TIMESTAMP WHERE username=%s AND claimed_time IS NULL RETURNING claimed_time """, (self.username,)) self.set_attributes(claimed_time=claimed_time)
def set_as_claimed(self): with self.db.get_cursor() as c: add_event(c, 'participant', dict(id=self.id, action='claim')) claimed_time = c.one( """\ UPDATE participants SET claimed_time=CURRENT_TIMESTAMP WHERE username=%s AND claimed_time IS NULL RETURNING claimed_time """, (self.username, )) self.set_attributes(claimed_time=claimed_time)
def change_username(self, suggested): """Raise Response or return None. Usernames are limited to alphanumeric characters, plus ".,-_:@ ", and can only be 32 characters long. """ # TODO: reconsider allowing unicode usernames suggested = suggested.strip() if not suggested: raise UsernameIsEmpty(suggested) if len(suggested) > 32: raise UsernameTooLong(suggested) if set(suggested) - ASCII_ALLOWED_IN_USERNAME: raise UsernameContainsInvalidCharacters(suggested) lowercased = suggested.lower() if lowercased in gittip.RESTRICTED_USERNAMES: raise UsernameIsRestricted(suggested) if suggested != self.username: try: # Will raise IntegrityError if the desired username is taken. with self.db.get_cursor(back_as=tuple) as c: add_event( c, 'participant', dict(id=self.id, action='set', values=dict(username=suggested))) actual = c.one( "UPDATE participants " "SET username=%s, username_lower=%s " "WHERE username=%s " "RETURNING username, username_lower", (suggested, lowercased, self.username)) except IntegrityError: raise UsernameAlreadyTaken(suggested) assert (suggested, lowercased) == actual # sanity check self.set_attributes(username=suggested, username_lower=lowercased) return suggested
def change_username(self, suggested): """Raise Response or return None. Usernames are limited to alphanumeric characters, plus ".,-_:@ ", and can only be 32 characters long. """ # TODO: reconsider allowing unicode usernames suggested = suggested.strip() if not suggested: raise UsernameIsEmpty(suggested) if len(suggested) > 32: raise UsernameTooLong(suggested) if set(suggested) - ASCII_ALLOWED_IN_USERNAME: raise UsernameContainsInvalidCharacters(suggested) lowercased = suggested.lower() if lowercased in gittip.RESTRICTED_USERNAMES: raise UsernameIsRestricted(suggested) if suggested != self.username: try: # Will raise IntegrityError if the desired username is taken. with self.db.get_cursor(back_as=tuple) as c: add_event(c, 'participant', dict(id=self.id, action='set', values=dict(username=suggested))) actual = c.one( "UPDATE participants " "SET username=%s, username_lower=%s " "WHERE username=%s " "RETURNING username, username_lower" , (suggested, lowercased, self.username) ) except IntegrityError: raise UsernameAlreadyTaken(suggested) assert (suggested, lowercased) == actual # sanity check self.set_attributes(username=suggested, username_lower=lowercased) return suggested
def delete_elsewhere(self, platform, user_id): """Deletes account elsewhere unless the user would not be able to log in anymore. """ user_id = unicode(user_id) with self.db.get_cursor() as c: accounts = c.all(""" SELECT platform, user_id FROM elsewhere WHERE participant=%s AND platform IN %s """, (self.username, AccountElsewhere.signin_platforms_names)) assert len(accounts) > 0 if len(accounts) == 1 and accounts[0] == (platform, user_id): raise LastElsewhere() c.one(""" DELETE FROM elsewhere WHERE participant=%s AND platform=%s AND user_id=%s RETURNING participant """, (self.username, platform, user_id), default=NonexistingElsewhere) add_event(c, 'participant', dict(id=self.id, action='disconnect', values=dict(platform=platform, user_id=user_id)))