78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
"""Units: the picker list, read, field edits, renumber (new lot) and soft delete.
|
|||
|
|
|
||
|
|
New units are made through POST /api/projects/{id}/units. Same access rules as projects: reads need any
|
||
|
|
session; edits need Clerical; renumber and delete need clerical_supervisor; admin with an it_reason.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, Query
|
||
|
|
from pydantic import BaseModel, Field
|
||
|
|
|
||
|
|
from ..domain import projects as dom
|
||
|
|
from .deps import Db, Principal, current_principal, get_db
|
||
|
|
from .projects import TEXT, FieldCas, ReasonBody, authorize
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api/units", tags=["units"])
|
||
|
|
|
||
|
|
|
||
|
|
class RenumberUnit(BaseModel):
|
||
|
|
lot: str = Field(max_length=40)
|
||
|
|
reason: str | None = Field(None, max_length=TEXT)
|
||
|
|
it_reason: str | None = Field(None, max_length=TEXT)
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("")
|
||
|
|
def list_units(
|
||
|
|
p: Principal = Depends(current_principal), # noqa: B008
|
||
|
|
d: Db = Depends(get_db), # noqa: B008
|
||
|
|
project_id: int | None = Query(None),
|
||
|
|
q: str = Query("", max_length=200),
|
||
|
|
status: str = Query("active", max_length=20),
|
||
|
|
) -> list[dict[str, Any]]:
|
||
|
|
return dom.list_units(d.conn, project_id=project_id, q=q, status=status)
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/{unit_id}")
|
||
|
|
def unit_detail(
|
||
|
|
unit_id: int,
|
||
|
|
p: Principal = Depends(current_principal), # noqa: B008
|
||
|
|
d: Db = Depends(get_db), # noqa: B008
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
return dom.unit_detail(d.conn, unit_id)
|
||
|
|
|
||
|
|
|
||
|
|
@router.patch("/{unit_id}")
|
||
|
|
def update_unit(
|
||
|
|
unit_id: int,
|
||
|
|
body: FieldCas,
|
||
|
|
p: Principal = Depends(current_principal), # noqa: B008
|
||
|
|
d: Db = Depends(get_db), # noqa: B008
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
who = authorize(p, supervisor=False, it_reason=body.it_reason)
|
||
|
|
return dom.update_unit(d.conn, who, unit_id, body.values, body.base)
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/{unit_id}/renumber")
|
||
|
|
def renumber_unit(
|
||
|
|
unit_id: int,
|
||
|
|
body: RenumberUnit,
|
||
|
|
p: Principal = Depends(current_principal), # noqa: B008
|
||
|
|
d: Db = Depends(get_db), # noqa: B008
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
who = authorize(p, supervisor=True, it_reason=body.it_reason)
|
||
|
|
return dom.renumber_unit(d.conn, who, unit_id, body.lot, body.reason)
|
||
|
|
|
||
|
|
|
||
|
|
@router.delete("/{unit_id}")
|
||
|
|
def remove_unit(
|
||
|
|
unit_id: int,
|
||
|
|
body: ReasonBody,
|
||
|
|
p: Principal = Depends(current_principal), # noqa: B008
|
||
|
|
d: Db = Depends(get_db), # noqa: B008
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
who = authorize(p, supervisor=True, it_reason=body.it_reason)
|
||
|
|
return dom.remove_unit(d.conn, who, unit_id, body.reason)
|