forked from seb_vallee/BillManager
96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
from fastapi import APIRouter, Depends, Request, Form, HTTPException
|
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
|
from fastapi.templating import Jinja2Templates
|
|
from sqlalchemy.orm import Session
|
|
|
|
from database import get_db
|
|
from models import Client
|
|
from auth import get_current_user
|
|
from template_helper import render
|
|
|
|
router = APIRouter(prefix="/clients", tags=["clients"],
|
|
dependencies=[Depends(get_current_user)])
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
|
@router.get("/", response_class=HTMLResponse)
|
|
def liste_clients(request: Request, db: Session = Depends(get_db)):
|
|
clients = db.query(Client).filter(Client.actif == True).order_by(Client.nom).all()
|
|
return render(templates, "clients/liste.html", request, {
|
|
"clients": clients
|
|
})
|
|
|
|
|
|
@router.get("/nouveau", response_class=HTMLResponse)
|
|
def nouveau_client_form(request: Request):
|
|
return render(templates, "clients/form.html", request, {
|
|
"client": None, "titre": "Nouveau client"
|
|
})
|
|
|
|
|
|
@router.post("/nouveau")
|
|
def creer_client(
|
|
request: Request,
|
|
nom: str = Form(...),
|
|
adresse: str = Form(...),
|
|
code_postal: str = Form(...),
|
|
ville: str = Form(...),
|
|
email: str = Form(""),
|
|
telephone: str = Form(""),
|
|
siret: str = Form(""),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
client = Client(
|
|
nom=nom, adresse=adresse, code_postal=code_postal,
|
|
ville=ville, email=email, telephone=telephone, siret=siret
|
|
)
|
|
db.add(client)
|
|
db.commit()
|
|
return RedirectResponse("/clients/", status_code=303)
|
|
|
|
|
|
@router.get("/{client_id}/modifier", response_class=HTMLResponse)
|
|
def modifier_client_form(request: Request, client_id: int, db: Session = Depends(get_db)):
|
|
client = db.query(Client).get(client_id)
|
|
if not client:
|
|
raise HTTPException(status_code=404)
|
|
return render(templates, "clients/form.html", request, {
|
|
"client": client, "titre": "Modifier le client"
|
|
})
|
|
|
|
|
|
@router.post("/{client_id}/modifier")
|
|
def modifier_client(
|
|
client_id: int,
|
|
nom: str = Form(...),
|
|
adresse: str = Form(...),
|
|
code_postal: str = Form(...),
|
|
ville: str = Form(...),
|
|
email: str = Form(""),
|
|
telephone: str = Form(""),
|
|
siret: str = Form(""),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
client = db.query(Client).get(client_id)
|
|
if not client:
|
|
raise HTTPException(status_code=404)
|
|
client.nom = nom
|
|
client.adresse = adresse
|
|
client.code_postal = code_postal
|
|
client.ville = ville
|
|
client.email = email
|
|
client.telephone = telephone
|
|
client.siret = siret
|
|
db.commit()
|
|
return RedirectResponse("/clients/", status_code=303)
|
|
|
|
|
|
@router.post("/{client_id}/supprimer")
|
|
def supprimer_client(client_id: int, db: Session = Depends(get_db)):
|
|
client = db.query(Client).get(client_id)
|
|
if not client:
|
|
raise HTTPException(status_code=404)
|
|
client.actif = False
|
|
db.commit()
|
|
return RedirectResponse("/clients/", status_code=303)
|