Example #1
0
def create_kitten(kitten: Kitten, session: Session) -> Kitten:
    kitten_ob = KittenModel(name=kitten.name)
    session.add(kitten_ob)
    session.flush()

    kitten.id = kitten_ob.id
    return kitten
Example #2
0
def create_user(user: UserSchema, session: Session) -> Tuple[str, UserSchema]:
    user_ob = User(email_address=user.email_address,
                   display_name=user.display_name,
                   title=user.title)
    session.add(user_ob)
    session.flush()

    user.id = user_ob.id
    # user.comments = user_ob.comments
    return HTTP_201, user
Example #3
0
def truncate_all_tables(session: Session):
    table_names = session.execute("""
        select table_name from information_schema.tables
        where table_schema = 'public'
        and table_type = 'BASE TABLE'
        and table_name != 'alembic_version'
    """)
    for (table_name,) in table_names:
        # "truncate" can deadlock so we use delete which is guaranteed not to.
        session.execute(f"delete from {table_name}")
    session.commit()
Example #4
0
def chat(account: Optional[Account], handler_factory: ChatHandlerFactory,
         session: Session, socket: Websocket):
    if not account:
        raise HTTPError(HTTP_403, {"errors": "forbidden"})

    # Grab the username and close the DB session to avoid starvation.
    username = account.username
    session.close()

    handler = handler_factory(socket, username)
    handler.handle_until_close()
Example #5
0
def create_comment(comment: CommentSchema,
                   session: Session) -> Tuple[str, CommentSchema]:
    comment_ob = Comment(
        user_id=comment.user_id,
        content=comment.content,
    )
    session.add(comment_ob)
    session.flush()

    comment.id = comment_ob.id
    comment.date_created = comment_ob.date_created
    return HTTP_201, comment
Example #6
0
def list_comments(session: Session) -> List[CommentSchema]:
    comments = session.query(Comment).all()
    return [
        CommentSchema(id=ob.id,
                      content=ob.content,
                      user_id=ob.user_id,
                      date_created=ob.date_created) for ob in comments
    ]
Example #7
0
def get_comment(comment_id: int, session: Session) -> CommentSchema:
    ob = session.query(Comment).get(comment_id)
    if ob is None:
        raise HTTPError(HTTP_404, {"error": f"comment {comment_id} not found"})

    return CommentSchema(id=ob.id,
                         content=ob.content,
                         user_id=ob.user_id,
                         date_created=ob.date_created)
Example #8
0
def list_users(session: Session) -> List[UserSchema]:
    user_obs = session.query(User).all()
    return [
        UserSchema(
            id=ob.id,
            email_address=ob.email_address,
            display_name=ob.display_name,
            title=ob.title,
            # comments=ob.comments
        ) for ob in user_obs
    ]
Example #9
0
def get_user(user_id: int, session: Session) -> UserSchema:
    ob = session.query(User).get(user_id)
    if ob is None:
        raise HTTPError(HTTP_404, {"error": f"user {user_id} not found"})

    return UserSchema(
        id=ob.id,
        email_address=ob.email_address,
        display_name=ob.display_name,
        title=ob.title,
        # comments=ob.comments
    )
Example #10
0
def get_kitten(kitten_id: int, session: Session) -> Kitten:
    kitten_ob = session.query(KittenModel).get(kitten_id)
    if kitten_ob is None:
        raise HTTPError(HTTP_404, {"error": f"kitten {kitten_id} not found"})

    return Kitten(id=kitten_ob.id, name=kitten_ob.name)
Example #11
0
def list_kittens(session: Session) -> List[Kitten]:
    kitten_obs = session.query(KittenModel).all()
    return [Kitten(id=ob.id, name=ob.name) for ob in kitten_obs]
Example #12
0
 def _create_user(session: Session):
     user = UserModel(email=email,
                      display_name=display_name,
                      password=password)
     session.add(user)
     session.commit()
Example #13
0
def delete_user(user_id: str, session: Session) -> Tuple[str, None]:
    session.query(User).filter(User.id == user_id).delete()
    return HTTP_204, None
Example #14
0
def delete_comment(comment_id: int, session: Session) -> Tuple[str, None]:
    session.query(Comment).filter(Comment.id == comment_id).delete()
    return HTTP_204, None