100 lines
3.6 KiB
Python
100 lines
3.6 KiB
Python
"""CRUD administrativo da tabela `categoria` (categorias e palavras-chave)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Form, Request
|
|
from starlette.responses import RedirectResponse
|
|
|
|
from .. import auth
|
|
from .. import database as db
|
|
from ..templating import flash, render
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/categorias")
|
|
def list_categorias(request: Request):
|
|
with db.session() as conn:
|
|
rows = [dict(r) for r in db.list_categorias_por_nome(conn)]
|
|
return render(request, "categorias_list.html", rows=rows, reserved_id=db.RESERVED_CATEGORIA_ID)
|
|
|
|
|
|
@router.get("/categorias/new")
|
|
def new_form(request: Request):
|
|
return render(request, "categoria_form.html", categoria=None, mode="new")
|
|
|
|
|
|
@router.post("/categorias/new")
|
|
def create_categoria(
|
|
request: Request,
|
|
csrf_token: str = Form(""),
|
|
categoria: str = Form(""),
|
|
palavra_chave: str = Form(""),
|
|
):
|
|
if not auth.check_csrf(request, csrf_token):
|
|
flash(request, "Sessão expirada.", "error")
|
|
return RedirectResponse("/categorias/new", status_code=303)
|
|
|
|
nome = categoria.strip()
|
|
keyword = palavra_chave.strip()
|
|
if not nome or not keyword:
|
|
flash(request, "Informe categoria e palavra-chave válidas.", "error")
|
|
return RedirectResponse("/categorias/new", status_code=303)
|
|
|
|
with db.session() as conn:
|
|
db.create_categoria(conn, nome, keyword)
|
|
flash(request, "Categoria criada.", "success")
|
|
return RedirectResponse("/categorias", status_code=303)
|
|
|
|
|
|
@router.get("/categorias/{categoria_id}/edit")
|
|
def edit_form(request: Request, categoria_id: int):
|
|
with db.session() as conn:
|
|
row = db.get_categoria(conn, categoria_id)
|
|
if row is None:
|
|
flash(request, "Categoria não encontrada.", "error")
|
|
return RedirectResponse("/categorias", status_code=303)
|
|
return render(request, "categoria_form.html", categoria=dict(row), mode="edit")
|
|
|
|
|
|
@router.post("/categorias/{categoria_id}/edit")
|
|
def update_categoria(
|
|
request: Request,
|
|
categoria_id: int,
|
|
csrf_token: str = Form(""),
|
|
categoria: str = Form(""),
|
|
palavra_chave: str = Form(""),
|
|
):
|
|
if not auth.check_csrf(request, csrf_token):
|
|
flash(request, "Sessão expirada.", "error")
|
|
return RedirectResponse(f"/categorias/{categoria_id}/edit", status_code=303)
|
|
|
|
nome = categoria.strip()
|
|
keyword = palavra_chave.strip()
|
|
if not nome or not keyword:
|
|
flash(request, "Informe categoria e palavra-chave válidas.", "error")
|
|
return RedirectResponse(f"/categorias/{categoria_id}/edit", status_code=303)
|
|
|
|
with db.session() as conn:
|
|
if db.get_categoria(conn, categoria_id) is None:
|
|
flash(request, "Categoria não encontrada.", "error")
|
|
return RedirectResponse("/categorias", status_code=303)
|
|
db.update_categoria(conn, categoria_id, nome, keyword)
|
|
flash(request, "Categoria atualizada.", "success")
|
|
return RedirectResponse("/categorias", status_code=303)
|
|
|
|
|
|
@router.post("/categorias/{categoria_id}/delete")
|
|
def delete_categoria(request: Request, categoria_id: int, csrf_token: str = Form("")):
|
|
if not auth.check_csrf(request, csrf_token):
|
|
flash(request, "Sessão expirada.", "error")
|
|
return RedirectResponse("/categorias", status_code=303)
|
|
|
|
with db.session() as conn:
|
|
deleted = db.delete_categoria(conn, categoria_id)
|
|
if deleted:
|
|
flash(request, "Categoria excluída.", "info")
|
|
else:
|
|
flash(request, "A categoria \"Não Encontrado\" é reservada e não pode ser excluída.", "error")
|
|
return RedirectResponse("/categorias", status_code=303)
|