Automated Action 5a02fb8b1f Implement comprehensive school portal API with FastAPI
- 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>
2025-06-25 13:31:56 +00:00

90 lines
3.2 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.grade import Grade, GradeCreate, GradeUpdate
from app.services.grade import grade_service
router = APIRouter()
@router.get("/", response_model=List[Grade])
def read_grades(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
student_id: int = None,
subject_id: int = None,
current_user: User = Depends(deps.get_current_active_user),
) -> Any:
if current_user.role == UserRole.STUDENT:
grades = grade_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")
grades = grade_service.get_by_student(db, student_id=student_id)
elif student_id and subject_id:
grades = grade_service.get_by_student_and_subject(db, student_id=student_id, subject_id=subject_id)
elif student_id:
grades = grade_service.get_by_student(db, student_id=student_id)
elif subject_id:
grades = grade_service.get_by_subject(db, subject_id=subject_id)
else:
grades = grade_service.get_multi(db, skip=skip, limit=limit)
return grades
@router.post("/", response_model=Grade)
def create_grade(
*,
db: Session = Depends(deps.get_db),
grade_in: GradeCreate,
current_user: User = Depends(deps.get_current_teacher_or_admin),
) -> Any:
grade = grade_service.create(db, obj_in=grade_in)
return grade
@router.put("/{grade_id}", response_model=Grade)
def update_grade(
*,
db: Session = Depends(deps.get_db),
grade_id: int,
grade_in: GradeUpdate,
current_user: User = Depends(deps.get_current_teacher_or_admin),
) -> Any:
grade = grade_service.get(db, id=grade_id)
if not grade:
raise HTTPException(status_code=404, detail="Grade not found")
grade = grade_service.update(db, db_obj=grade, obj_in=grade_in)
return grade
@router.get("/{grade_id}", response_model=Grade)
def read_grade(
*,
db: Session = Depends(deps.get_db),
grade_id: int,
current_user: User = Depends(deps.get_current_active_user),
) -> Any:
grade = grade_service.get(db, id=grade_id)
if not grade:
raise HTTPException(status_code=404, detail="Grade not found")
if current_user.role == UserRole.STUDENT and grade.student_id != current_user.id:
raise HTTPException(status_code=403, detail="Not enough permissions")
elif current_user.role == UserRole.PARENT and grade.student.parent_id != current_user.id:
raise HTTPException(status_code=403, detail="Not enough permissions")
return grade
@router.delete("/{grade_id}", response_model=Grade)
def delete_grade(
*,
db: Session = Depends(deps.get_db),
grade_id: int,
current_user: User = Depends(deps.get_current_teacher_or_admin),
) -> Any:
grade = grade_service.get(db, id=grade_id)
if not grade:
raise HTTPException(status_code=404, detail="Grade not found")
grade = grade_service.remove(db, id=grade_id)
return grade