gsparc-mezzouna-api/app/routes/vehicles.py

63 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Routes — véhicules."""
from fastapi import APIRouter, Depends, Request, Form
from fastapi.responses import HTMLResponse, RedirectResponse
from app.auth import require_auth
from app.baserow import list_rows, create_row, update_row, delete_row, get_row
from app.templates import templates
from app.config import TABLE_VEHICULES
router = APIRouter(prefix="/vehicules")
@router.get("/", response_class=HTMLResponse)
async def liste_vehicules(request: Request, user=Depends(require_auth)):
vehicules = await list_rows(TABLE_VEHICULES)
return templates.TemplateResponse("vehicules.html", {"request": request, "vehicules": vehicules})
@router.post("/add")
async def ajouter_vehicule(
request: Request,
matricule: str = Form(...),
type_v: str = Form(...),
marque: str = Form(""),
capacite: float = Form(None),
notes: str = Form(""),
user=Depends(require_auth),
):
await create_row(TABLE_VEHICULES, {
"رقم_الماتريكول": matricule,
"النوع": type_v,
"العلامة": marque,
"حمولة_الخزان": capacite,
"ملاحظات": notes,
})
return RedirectResponse(url="/vehicules", status_code=303)
@router.post("/{row_id}/delete")
async def supprimer_vehicule(request: Request, row_id: int, user=Depends(require_auth)):
await delete_row(TABLE_VEHICULES, row_id)
return RedirectResponse(url="/vehicules", status_code=303)
@router.post("/{row_id}/edit")
async def modifier_vehicule(
request: Request,
row_id: int,
matricule: str = Form(...),
type_v: str = Form(...),
marque: str = Form(""),
capacite: float = Form(None),
notes: str = Form(""),
user=Depends(require_auth),
):
await update_row(TABLE_VEHICULES, row_id, {
"رقم_الماتريكول": matricule,
"النوع": type_v,
"العلامة": marque,
"حمولة_الخزان": capacite,
"ملاحظات": notes,
})
return RedirectResponse(url="/vehicules", status_code=303)