45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
|
|
from fastapi import APIRouter, Depends
|
||
|
|
from sqlalchemy import select
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.auth.deps import CurrentUser, get_current_user
|
||
|
|
from app.database import get_db
|
||
|
|
from app.models import Department, DeviationCategory
|
||
|
|
from app.schemas.lookup import LookupsOut, NamedLookupOut
|
||
|
|
|
||
|
|
router = APIRouter(tags=["lookups"])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/lookups", response_model=LookupsOut)
|
||
|
|
async def get_lookups(
|
||
|
|
_: CurrentUser = Depends(get_current_user),
|
||
|
|
db: AsyncSession = Depends(get_db),
|
||
|
|
) -> LookupsOut:
|
||
|
|
"""Active departments and deviation categories for form dropdowns."""
|
||
|
|
departments = (
|
||
|
|
(
|
||
|
|
await db.execute(
|
||
|
|
select(Department)
|
||
|
|
.where(Department.is_active.is_(True))
|
||
|
|
.order_by(Department.name)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
.scalars()
|
||
|
|
.all()
|
||
|
|
)
|
||
|
|
categories = (
|
||
|
|
(
|
||
|
|
await db.execute(
|
||
|
|
select(DeviationCategory)
|
||
|
|
.where(DeviationCategory.is_active.is_(True))
|
||
|
|
.order_by(DeviationCategory.name)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
.scalars()
|
||
|
|
.all()
|
||
|
|
)
|
||
|
|
return LookupsOut(
|
||
|
|
departments=[NamedLookupOut.model_validate(d) for d in departments],
|
||
|
|
deviation_categories=[NamedLookupOut.model_validate(c) for c in categories],
|
||
|
|
)
|