aurweb/aurweb/users/util.py
Kevin Morris 4103ab49c9
housekeep(fastapi): rework aurweb.db session API
Changes:
-------
- Add aurweb.db.get_session()
    - Returns aurweb.db's global `session` instance
    - Provides us a way to change the implementation of the session
      instance without interrupting user code.
- Use aurweb.db.get_session() in session API methods
- Add docstrings to session API methods
- Refactor aurweb.db.delete
    - Normalize aurweb.db.delete to an alias of session.delete
- Refresh instances in places we depend on their non-PK columns
  being up to date.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-11-14 16:15:50 -08:00

19 lines
457 B
Python

from http import HTTPStatus
from fastapi import HTTPException
from aurweb import db
from aurweb.models import User
def get_user_by_name(username: str) -> User:
"""
Query a user by its username.
:param username: User.Username
:return: User instance
"""
user = db.query(User).filter(User.Username == username).first()
if not user:
raise HTTPException(status_code=int(HTTPStatus.NOT_FOUND))
return db.refresh(user)