
- Complete authentication system with JWT and role-based access control
- User management for Admin, Teacher, Student, and Parent roles
- Student management with CRUD operations
- Class management and assignment system
- Subject and grade tracking functionality
- Daily attendance marking and viewing
- Notification system for announcements
- SQLite database with Alembic migrations
- Comprehensive API documentation with Swagger/ReDoc
- Proper project structure with services, models, and schemas
- Environment variable configuration
- CORS support and security features
🤖 Generated with BackendIM
Co-Authored-By: BackendIM <noreply@anthropic.com>
115 lines
3.6 KiB
Python
115 lines
3.6 KiB
Python
from typing import Any, List
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.api import deps
|
|
from app.models.user import User, UserRole
|
|
from app.schemas.user import User as UserSchema, UserCreate, UserUpdate
|
|
from app.services.user import user_service
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/", response_model=List[UserSchema])
|
|
def read_users(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(deps.get_current_admin_user),
|
|
) -> Any:
|
|
users = user_service.get_multi(db, skip=skip, limit=limit)
|
|
return users
|
|
|
|
@router.post("/", response_model=UserSchema)
|
|
def create_user(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
user_in: UserCreate,
|
|
current_user: User = Depends(deps.get_current_admin_user),
|
|
) -> Any:
|
|
user = user_service.get_by_email(db, email=user_in.email)
|
|
if user:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="The user with this email already exists in the system",
|
|
)
|
|
user = user_service.create(db, obj_in=user_in)
|
|
return user
|
|
|
|
@router.put("/{user_id}", response_model=UserSchema)
|
|
def update_user(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
user_id: int,
|
|
user_in: UserUpdate,
|
|
current_user: User = Depends(deps.get_current_teacher_or_admin),
|
|
) -> Any:
|
|
user = user_service.get(db, id=user_id)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="The user with this id does not exist in the system",
|
|
)
|
|
|
|
if current_user.role == UserRole.TEACHER and user_in.role and user_in.role != UserRole.STUDENT:
|
|
raise HTTPException(
|
|
status_code=403,
|
|
detail="Teachers can only modify student profiles"
|
|
)
|
|
|
|
user = user_service.update(db, db_obj=user, obj_in=user_in)
|
|
return user
|
|
|
|
@router.get("/{user_id}", response_model=UserSchema)
|
|
def read_user(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
user_id: int,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
user = user_service.get(db, id=user_id)
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
if current_user.role not in [UserRole.ADMIN, UserRole.TEACHER] and current_user.id != user_id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
|
|
return user
|
|
|
|
@router.delete("/{user_id}", response_model=UserSchema)
|
|
def delete_user(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
user_id: int,
|
|
current_user: User = Depends(deps.get_current_teacher_or_admin),
|
|
) -> Any:
|
|
user = user_service.get(db, id=user_id)
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
if current_user.role == UserRole.TEACHER and user.role != UserRole.STUDENT:
|
|
raise HTTPException(
|
|
status_code=403,
|
|
detail="Teachers can only delete student profiles"
|
|
)
|
|
|
|
user = user_service.remove(db, id=user_id)
|
|
return user
|
|
|
|
@router.get("/students/", response_model=List[UserSchema])
|
|
def read_students(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(deps.get_current_teacher_or_admin),
|
|
) -> Any:
|
|
students = user_service.get_students(db, skip=skip, limit=limit)
|
|
return students
|
|
|
|
@router.get("/teachers/", response_model=List[UserSchema])
|
|
def read_teachers(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(deps.get_current_admin_user),
|
|
) -> Any:
|
|
teachers = user_service.get_teachers(db, skip=skip, limit=limit)
|
|
return teachers |