mirror of
https://github.com/RYDE-WORK/full-stack-fastapi-template.git
synced 2026-01-19 21:23:36 +08:00
* Update CRUD utils to use types better. * Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc. * Upgrade packages. * Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case. * Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`. * Update testing utils. * Update linting rules, relax vulture to reduce false positives. * Update migrations to include new Items. * Update project README.md with tips about how to start with backend.
66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
from typing import List, Optional
|
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.security import get_password_hash, verify_password
|
|
from app.db_models.user import User
|
|
from app.models.user import UserCreate, UserUpdate
|
|
|
|
|
|
def get(db_session: Session, *, user_id: int) -> Optional[User]:
|
|
return db_session.query(User).filter(User.id == user_id).first()
|
|
|
|
|
|
def get_by_email(db_session: Session, *, email: str) -> Optional[User]:
|
|
return db_session.query(User).filter(User.email == email).first()
|
|
|
|
|
|
def authenticate(db_session: Session, *, email: str, password: str) -> Optional[User]:
|
|
user = get_by_email(db_session, email=email)
|
|
if not user:
|
|
return None
|
|
if not verify_password(password, user.hashed_password):
|
|
return None
|
|
return user
|
|
|
|
|
|
def is_active(user) -> bool:
|
|
return user.is_active
|
|
|
|
|
|
def is_superuser(user) -> bool:
|
|
return user.is_superuser
|
|
|
|
|
|
def get_multi(db_session: Session, *, skip=0, limit=100) -> List[Optional[User]]:
|
|
return db_session.query(User).offset(skip).limit(limit).all()
|
|
|
|
|
|
def create(db_session: Session, *, user_in: UserCreate) -> User:
|
|
user = User(
|
|
email=user_in.email,
|
|
hashed_password=get_password_hash(user_in.password),
|
|
full_name=user_in.full_name,
|
|
is_superuser=user_in.is_superuser,
|
|
)
|
|
db_session.add(user)
|
|
db_session.commit()
|
|
db_session.refresh(user)
|
|
return user
|
|
|
|
|
|
def update(db_session: Session, *, user: User, user_in: UserUpdate) -> User:
|
|
user_data = jsonable_encoder(user)
|
|
update_data = user_in.dict(skip_defaults=True)
|
|
for field in user_data:
|
|
if field in update_data:
|
|
setattr(user, field, update_data[field])
|
|
if user_in.password:
|
|
passwordhash = get_password_hash(user_in.password)
|
|
user.hashed_password = passwordhash
|
|
db_session.add(user)
|
|
db_session.commit()
|
|
db_session.refresh(user)
|
|
return user
|