def set_code_consumed(c, code_id): """ Mark the specified verify code as consumed. """ code_table = get_table('_verify_code') stmt = code_table.update().values(consumed=True) \ .where(code_table.c.id == code_id) c.execute(stmt)
def get_conversation_by_uid(uid): with conn() as c: conversation = get_table('conversation') stmt = select([conversation.c._id]) \ .where(conversation.c._created_by == uid) r = c.execute(stmt).fetchone() if r is None: return None else: return r[0]
def get_user_by_username(fb_id): with conn() as c: users = get_table('_user') stmt = select([users.c.id]) \ .where(users.c.username == fb_id) r = c.execute(stmt).fetchone() if r is None: return None else: return r[0]
def get_user_record(c, user_id): """ Get user record from the database with the specified user ID. """ if not has_table('user'): return None users = get_table('user') stmt = select([users]).where(users.c._id == user_id) result = c.execute(stmt) return result.fetchone()
def set_new_password(c, user_id, new_password): """ Set the password of a user to a new password. """ encoded_password = new_password.encode('utf-8') hashed = bcrypt.hashpw(encoded_password, bcrypt.gensalt()).decode() users = get_table('_auth') stmt = users.update() \ .where(users.c.id == user_id) \ .values(password=hashed) \ .values(token_valid_since=func.now()) \ .values(last_seen_at=func.now()) return c.execute(stmt)
def get_user_from_email(c, email): """ Get user information from the database with the specified user email. """ users = get_table('_user') stmt = select([ users.c.id, users.c.email, users.c.password, users.c.last_login_at, ]) \ .where(users.c.email == email) result = c.execute(stmt) return result.fetchone()
def get_verify_code(c, auth_id, code): """ Get a previously created verify code from database. """ code_table = get_table('_verify_code') # Query the table, will only return the newest code if multiple exists # for the same verification code stmt = select([code_table]) \ .where(and_(code_table.c.auth_id == auth_id, code_table.c.code == code)) \ .order_by(desc(code_table.c.created_at)) # noqa result = c.execute(stmt) return result.fetchone()
def add_verify_code(c, auth_id, record_key, record_value, code): """ Create a new verify code into the database. """ code_table = get_table('_verify_code') values = { 'id': str(uuid.uuid4()), 'auth_id': auth_id, 'record_key': record_key, 'record_value': record_value, 'code': code.strip(), 'consumed': False, 'created_at': func.now(), } c.execute(code_table.insert().values(**values))
def test_get_table_nonexistent(self): with self.assertRaises(Exception): db.get_table('something')
def test_get_table(self): table = db.get_table('note') assert isinstance(table, Table) assert table.c.content is not None
def test_get_table_nonexistent(self): with self.assertRaises(Exception): db.get_table('something')
def test_get_table(self): table = db.get_table('note') assert isinstance(table, Table) assert table.c.content is not None