Replaces ambiguous shorthand references like `event` and `user` with more descriptive names such as `event_crud` and `user_crud`. Updates imports, function calls, tests, and other references across the codebase to maintain consistency. This improves code readability and reduces potential confusion.
56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
# app/crud/user.py
|
|
from typing import Optional, Union, Dict, Any
|
|
from sqlalchemy.orm import Session
|
|
from app.crud.base import CRUDBase
|
|
from app.models.user import User
|
|
from app.schemas.users import UserCreate, UserUpdate
|
|
from app.core.auth import get_password_hash
|
|
|
|
|
|
class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
|
|
def get_by_email(self, db: Session, *, email: str) -> Optional[User]:
|
|
return db.query(User).filter(User.email == email).first()
|
|
|
|
def create(self, db: Session, *, obj_in: UserCreate) -> User:
|
|
db_obj = User(
|
|
email=obj_in.email,
|
|
password_hash=get_password_hash(obj_in.password),
|
|
first_name=obj_in.first_name,
|
|
last_name=obj_in.last_name,
|
|
phone_number=obj_in.phone_number if hasattr(obj_in, 'phone_number') else None,
|
|
is_superuser=obj_in.is_superuser if hasattr(obj_in, 'is_superuser') else False,
|
|
preferences={}
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def update(
|
|
self,
|
|
db: Session,
|
|
*,
|
|
db_obj: User,
|
|
obj_in: Union[UserUpdate, Dict[str, Any]]
|
|
) -> User:
|
|
if isinstance(obj_in, dict):
|
|
update_data = obj_in
|
|
else:
|
|
update_data = obj_in.model_dump(exclude_unset=True)
|
|
|
|
# Handle password separately if it exists in update data
|
|
if "password" in update_data:
|
|
update_data["password_hash"] = get_password_hash(update_data["password"])
|
|
del update_data["password"]
|
|
|
|
return super().update(db, db_obj=db_obj, obj_in=update_data)
|
|
|
|
def is_active(self, user: User) -> bool:
|
|
return user.is_active
|
|
|
|
def is_superuser(self, user: User) -> bool:
|
|
return user.is_superuser
|
|
|
|
|
|
# Create a singleton instance for use across the application
|
|
user_crud = CRUDUser(User) |