- Add max_length constraints to all string fields in request schemas, matching DB column limits (title:255, description:5000, etc.) - Add min_length=1 to required name/title fields - Add ConfigDict(extra="forbid") to all request schemas to reject unknown fields (prevents silent field injection) - Add Path(ge=1, le=2147483647) to all integer path parameters across all routers to prevent integer overflow → 500 errors - Add max_length to TOTP inline schemas (code:6, mfa_token:256, etc.) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Path
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, update
|
|
from typing import List
|
|
|
|
from app.database import get_db
|
|
from app.models.calendar import Calendar
|
|
from app.models.calendar_event import CalendarEvent
|
|
from app.schemas.calendar import CalendarCreate, CalendarUpdate, CalendarResponse
|
|
from app.routers.auth import get_current_user
|
|
from app.models.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[CalendarResponse])
|
|
async def get_calendars(
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
result = await db.execute(
|
|
select(Calendar)
|
|
.where(Calendar.user_id == current_user.id)
|
|
.order_by(Calendar.is_default.desc(), Calendar.name.asc())
|
|
)
|
|
return result.scalars().all()
|
|
|
|
|
|
@router.post("/", response_model=CalendarResponse, status_code=201)
|
|
async def create_calendar(
|
|
calendar: CalendarCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
new_calendar = Calendar(
|
|
name=calendar.name,
|
|
color=calendar.color,
|
|
is_default=False,
|
|
is_system=False,
|
|
is_visible=True,
|
|
user_id=current_user.id,
|
|
)
|
|
db.add(new_calendar)
|
|
await db.commit()
|
|
await db.refresh(new_calendar)
|
|
return new_calendar
|
|
|
|
|
|
@router.put("/{calendar_id}", response_model=CalendarResponse)
|
|
async def update_calendar(
|
|
calendar_id: int = Path(ge=1, le=2147483647),
|
|
calendar_update: CalendarUpdate = ...,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
result = await db.execute(
|
|
select(Calendar).where(Calendar.id == calendar_id, Calendar.user_id == current_user.id)
|
|
)
|
|
calendar = result.scalar_one_or_none()
|
|
|
|
if not calendar:
|
|
raise HTTPException(status_code=404, detail="Calendar not found")
|
|
|
|
update_data = calendar_update.model_dump(exclude_unset=True)
|
|
|
|
# System calendars: allow visibility toggle but block name changes
|
|
if calendar.is_system and "name" in update_data:
|
|
raise HTTPException(status_code=400, detail="Cannot rename system calendars")
|
|
|
|
for key, value in update_data.items():
|
|
setattr(calendar, key, value)
|
|
|
|
await db.commit()
|
|
await db.refresh(calendar)
|
|
return calendar
|
|
|
|
|
|
@router.delete("/{calendar_id}", status_code=204)
|
|
async def delete_calendar(
|
|
calendar_id: int = Path(ge=1, le=2147483647),
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
result = await db.execute(
|
|
select(Calendar).where(Calendar.id == calendar_id, Calendar.user_id == current_user.id)
|
|
)
|
|
calendar = result.scalar_one_or_none()
|
|
|
|
if not calendar:
|
|
raise HTTPException(status_code=404, detail="Calendar not found")
|
|
|
|
if calendar.is_system:
|
|
raise HTTPException(status_code=400, detail="Cannot delete system calendars")
|
|
|
|
if calendar.is_default:
|
|
raise HTTPException(status_code=400, detail="Cannot delete the default calendar")
|
|
|
|
# Reassign all events on this calendar to the user's default calendar
|
|
default_result = await db.execute(
|
|
select(Calendar).where(
|
|
Calendar.user_id == current_user.id,
|
|
Calendar.is_default == True,
|
|
)
|
|
)
|
|
default_calendar = default_result.scalar_one_or_none()
|
|
|
|
if default_calendar:
|
|
await db.execute(
|
|
update(CalendarEvent)
|
|
.where(CalendarEvent.calendar_id == calendar_id)
|
|
.values(calendar_id=default_calendar.id)
|
|
)
|
|
|
|
await db.delete(calendar)
|
|
await db.commit()
|
|
return None
|