
- 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>
82 lines
3.1 KiB
Python
82 lines
3.1 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.notification import Notification, NotificationCreate, NotificationUpdate
|
|
from app.services.notification import notification_service
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/", response_model=List[Notification])
|
|
def read_notifications(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
if current_user.role in [UserRole.STUDENT, UserRole.PARENT]:
|
|
notifications = notification_service.get_by_recipient(db, recipient_id=current_user.id)
|
|
else:
|
|
notifications = notification_service.get_multi(db, skip=skip, limit=limit)
|
|
|
|
return notifications
|
|
|
|
@router.post("/", response_model=Notification)
|
|
def create_notification(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
notification_in: NotificationCreate,
|
|
current_user: User = Depends(deps.get_current_teacher_or_admin),
|
|
) -> Any:
|
|
notification = notification_service.create_with_recipients(
|
|
db, obj_in=notification_in, sender_id=current_user.id
|
|
)
|
|
return notification
|
|
|
|
@router.put("/{notification_id}", response_model=Notification)
|
|
def update_notification(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
notification_id: int,
|
|
notification_in: NotificationUpdate,
|
|
current_user: User = Depends(deps.get_current_teacher_or_admin),
|
|
) -> Any:
|
|
notification = notification_service.get(db, id=notification_id)
|
|
if not notification:
|
|
raise HTTPException(status_code=404, detail="Notification not found")
|
|
|
|
if notification.sender_id != current_user.id and current_user.role != UserRole.ADMIN:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
|
|
notification = notification_service.update(db, db_obj=notification, obj_in=notification_in)
|
|
return notification
|
|
|
|
@router.get("/{notification_id}", response_model=Notification)
|
|
def read_notification(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
notification_id: int,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
notification = notification_service.get(db, id=notification_id)
|
|
if not notification:
|
|
raise HTTPException(status_code=404, detail="Notification not found")
|
|
return notification
|
|
|
|
@router.delete("/{notification_id}", response_model=Notification)
|
|
def delete_notification(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
notification_id: int,
|
|
current_user: User = Depends(deps.get_current_teacher_or_admin),
|
|
) -> Any:
|
|
notification = notification_service.get(db, id=notification_id)
|
|
if not notification:
|
|
raise HTTPException(status_code=404, detail="Notification not found")
|
|
|
|
if notification.sender_id != current_user.id and current_user.role != UserRole.ADMIN:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
|
|
notification = notification_service.remove(db, id=notification_id)
|
|
return notification |