
- Set up project structure with FastAPI and SQLite - Implement user authentication with JWT - Create models for learning content (subjects, lessons, quizzes) - Add progress tracking and gamification features - Implement comprehensive API documentation - Add error handling and validation - Set up proper logging and health check endpoint
108 lines
3.1 KiB
Python
108 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, Optional, Union
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.security import get_password_hash, verify_password
|
|
from app.models.user import User
|
|
from app.schemas.user import UserCreate, UserUpdate
|
|
from app.utils.db import CRUDBase
|
|
|
|
|
|
class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
|
|
def get_by_email(self, db: Session, *, email: str) -> Optional[User]:
|
|
"""
|
|
Get a user by email.
|
|
"""
|
|
return db.query(User).filter(User.email == email).first()
|
|
|
|
def get_by_username(self, db: Session, *, username: str) -> Optional[User]:
|
|
"""
|
|
Get a user by username.
|
|
"""
|
|
return db.query(User).filter(User.username == username).first()
|
|
|
|
def create(self, db: Session, *, obj_in: UserCreate) -> User:
|
|
"""
|
|
Create a new user.
|
|
"""
|
|
db_obj = User(
|
|
email=obj_in.email,
|
|
username=obj_in.username,
|
|
hashed_password=get_password_hash(obj_in.password),
|
|
full_name=obj_in.full_name,
|
|
date_of_birth=obj_in.date_of_birth,
|
|
is_active=obj_in.is_active,
|
|
is_superuser=obj_in.is_superuser,
|
|
points=0,
|
|
level=1,
|
|
)
|
|
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:
|
|
"""
|
|
Update a user.
|
|
"""
|
|
if isinstance(obj_in, dict):
|
|
update_data = obj_in
|
|
else:
|
|
update_data = obj_in.model_dump(exclude_unset=True)
|
|
|
|
# Handle password update
|
|
if update_data.get("password"):
|
|
hashed_password = get_password_hash(update_data["password"])
|
|
del update_data["password"]
|
|
update_data["hashed_password"] = hashed_password
|
|
|
|
return super().update(db, db_obj=db_obj, obj_in=update_data)
|
|
|
|
def authenticate(self, db: Session, *, email: str, password: str) -> Optional[User]:
|
|
"""
|
|
Authenticate a user by email and password.
|
|
"""
|
|
user = self.get_by_email(db, email=email)
|
|
if not user:
|
|
return None
|
|
if not verify_password(password, user.hashed_password):
|
|
return None
|
|
return user
|
|
|
|
def is_active(self, user: User) -> bool:
|
|
"""
|
|
Check if a user is active.
|
|
"""
|
|
return user.is_active
|
|
|
|
def is_superuser(self, user: User) -> bool:
|
|
"""
|
|
Check if a user is a superuser.
|
|
"""
|
|
return user.is_superuser
|
|
|
|
def add_points(self, db: Session, *, user_id: int, points: int) -> User:
|
|
"""
|
|
Add points to a user's account and check for level up.
|
|
"""
|
|
user = self.get(db, id=user_id)
|
|
if not user:
|
|
return None
|
|
|
|
user.points += points
|
|
|
|
# Simple level up logic: 100 points per level
|
|
new_level = (user.points // 100) + 1
|
|
user.level = max(user.level, new_level)
|
|
|
|
db.commit()
|
|
db.refresh(user)
|
|
return user
|
|
|
|
|
|
user = CRUDUser(User)
|