예제 #1
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
예제 #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
예제 #3
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
예제 #4
0
def set_bookmark(
    session: Session, cache: Cache, user_uuid: UUID, bookmark: Bookmark
) -> UUID:
    url = bookmark.url
    if len(bookmark.tag_triples) > 0:
        tag_names, tag_updates, tag_deleted = zip(*bookmark.tag_triples)
    else:
        tag_names, tag_updates, tag_deleted = [()] * 3
    session.execute(
        func.insert_bookmark_v1(
            url.url_uuid,
            url.scheme,
            url.netloc,
            url.path,
            url.query,
            url.fragment,
            user_uuid,
            bookmark.title,
            bookmark.description,
            bookmark.created,
            bookmark.updated,
            bookmark.unread,
            bookmark.deleted,
            sa_cast(pg_array(tag_names), PGARRAY(satypes.String)),  # type:ignore
            sa_cast(
                pg_array(tag_updates),  # type:ignore
                PGARRAY(satypes.DateTime(timezone=True)),
            ),
            sa_cast(pg_array(tag_deleted), PGARRAY(satypes.Boolean)),  # type:ignore
        )
    )

    ub_cache_ns = UserBookmarksNamespaceKey(user_uuid)
    cache.set(ub_cache_ns, monotonic_ns())

    return url.url_uuid
예제 #5
0
def put_user_in_cache(cache: Cache, user: User):
    cache.set(UserUUIDToUserKey(user.user_uuid), user)
    cache.set(UsernameToUserKey(user.username), user)