
- 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>
83 lines
3.2 KiB
Python
83 lines
3.2 KiB
Python
from typing import Any, List
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from datetime import date
|
|
from app.api import deps
|
|
from app.models.user import User, UserRole
|
|
from app.schemas.attendance import Attendance, AttendanceCreate, AttendanceUpdate
|
|
from app.services.attendance import attendance_service
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/", response_model=List[Attendance])
|
|
def read_attendance(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
student_id: int = None,
|
|
class_id: int = None,
|
|
date: date = None,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
if current_user.role == UserRole.STUDENT:
|
|
attendance = attendance_service.get_by_student(db, student_id=current_user.id)
|
|
elif current_user.role == UserRole.PARENT:
|
|
if not student_id:
|
|
raise HTTPException(status_code=400, detail="Parent must specify student_id")
|
|
attendance = attendance_service.get_by_student(db, student_id=student_id)
|
|
elif student_id and date:
|
|
attendance = [attendance_service.get_by_student_and_date(db, student_id=student_id, date=date)]
|
|
elif student_id:
|
|
attendance = attendance_service.get_by_student(db, student_id=student_id)
|
|
elif class_id:
|
|
attendance = attendance_service.get_by_class(db, class_id=class_id)
|
|
elif date:
|
|
attendance = attendance_service.get_by_date(db, date=date)
|
|
else:
|
|
attendance = attendance_service.get_multi(db, skip=skip, limit=limit)
|
|
|
|
return [a for a in attendance if a is not None]
|
|
|
|
@router.post("/", response_model=Attendance)
|
|
def create_attendance(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
attendance_in: AttendanceCreate,
|
|
current_user: User = Depends(deps.get_current_teacher_or_admin),
|
|
) -> Any:
|
|
attendance = attendance_service.create_with_teacher(
|
|
db, obj_in=attendance_in, teacher_id=current_user.id
|
|
)
|
|
return attendance
|
|
|
|
@router.put("/{attendance_id}", response_model=Attendance)
|
|
def update_attendance(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
attendance_id: int,
|
|
attendance_in: AttendanceUpdate,
|
|
current_user: User = Depends(deps.get_current_teacher_or_admin),
|
|
) -> Any:
|
|
attendance = attendance_service.get(db, id=attendance_id)
|
|
if not attendance:
|
|
raise HTTPException(status_code=404, detail="Attendance not found")
|
|
attendance = attendance_service.update(db, db_obj=attendance, obj_in=attendance_in)
|
|
return attendance
|
|
|
|
@router.get("/{attendance_id}", response_model=Attendance)
|
|
def read_attendance_record(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
attendance_id: int,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
attendance = attendance_service.get(db, id=attendance_id)
|
|
if not attendance:
|
|
raise HTTPException(status_code=404, detail="Attendance not found")
|
|
|
|
if current_user.role == UserRole.STUDENT and attendance.student_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
elif current_user.role == UserRole.PARENT and attendance.student.parent_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
|
|
return attendance |