Exemplo n.º 1
0
def user_from_username_if_exists(session, cache: Cache,
                                 username: str) -> Optional[User]:
    key = UsernameToUserKey(username)
    user = cache.get(key)
    if user is not None:
        return user

    user_tuple = (session.query(
        SQLUser.user_uuid,
        UserEmail.email_address,
        SQLUser.timezone,
        SQLUser.registered,
    ).outerjoin(UserEmail).filter(SQLUser.username == username).first())

    # User doesn't exist
    if user_tuple is None:
        return None

    user_uuid, email, timezone, registered = user_tuple

    user = User(
        user_uuid=user_uuid,
        username=username,
        email=email,
        timezone=pytz.timezone(timezone),
        registered=registered,
    )
    put_user_in_cache(cache, user)
    return user
Exemplo n.º 2
0
def user_from_user_uuid(session, cache: Cache,
                        user_uuid: UUID) -> Optional[User]:
    # FIXME: This should return a nullable type as the user for this user uuid
    # may have been deleted (among other things)
    key = UserUUIDToUserKey(user_uuid)
    user = cache.get(key)
    if user is not None:
        return user

    row = (session.query(
        SQLUser.username,
        UserEmail.email_address,
        SQLUser.timezone,
        SQLUser.registered,
    ).outerjoin(UserEmail).filter(
        SQLUser.user_uuid == user_uuid).one_or_none())
    if row is None:
        return row
    else:
        username, email, timezone, registered = row
    user = User(
        user_uuid=user_uuid,
        username=username,
        email=email,
        timezone=pytz.timezone(timezone),
        registered=registered,
    )
    put_user_in_cache(cache, user)
    return user
Exemplo n.º 3
0
def get_api_key(session, cache: Cache, username: str) -> Optional[bytes]:
    cache_key = UsernameToApiKey(username)
    api_key = cache.get(cache_key)
    if api_key is not None:
        return api_key

    api_key = (session.query(APIKey.api_key).join(SQLUser).filter(
        SQLUser.username == username).scalar())
    cache.set(cache_key, api_key)
    return api_key