feat: dashboard web FastAPI Sprint 4
Ajout d'un dashboard lecture seule par-dessus la DB SQLite existante.
Fichiers créés :
- tickettracker/web/queries.py : 7 fonctions SQL (stats, compare, historique...)
- tickettracker/web/api.py : router /api/* JSON (FastAPI)
- tickettracker/web/app.py : routes HTML + Jinja2 + point d'entrée uvicorn
- tickettracker/web/templates/ : base.html, index.html, compare.html, product.html, receipt.html
- tickettracker/web/static/style.css : personnalisations Pico CSS
- tests/test_web.py : 19 tests (96 passent, 1 xfail OCR)
Fichiers modifiés :
- requirements.txt : +fastapi, uvicorn[standard], jinja2, python-multipart, httpx
- config.py : +DB_PATH (lu depuis TICKETTRACKER_DB_PATH)
Lancement : python -m tickettracker.web.app
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 20:04:55 +01:00
|
|
|
"""
|
|
|
|
|
Router FastAPI pour les endpoints JSON /api/*.
|
|
|
|
|
|
|
|
|
|
Chaque endpoint ouvre sa propre connexion SQLite (via config.DB_PATH),
|
|
|
|
|
appelle la fonction de queries.py correspondante, puis ferme la connexion.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, HTTPException
|
2026-02-25 18:02:48 +01:00
|
|
|
from fastapi.responses import Response
|
feat: dashboard web FastAPI Sprint 4
Ajout d'un dashboard lecture seule par-dessus la DB SQLite existante.
Fichiers créés :
- tickettracker/web/queries.py : 7 fonctions SQL (stats, compare, historique...)
- tickettracker/web/api.py : router /api/* JSON (FastAPI)
- tickettracker/web/app.py : routes HTML + Jinja2 + point d'entrée uvicorn
- tickettracker/web/templates/ : base.html, index.html, compare.html, product.html, receipt.html
- tickettracker/web/static/style.css : personnalisations Pico CSS
- tests/test_web.py : 19 tests (96 passent, 1 xfail OCR)
Fichiers modifiés :
- requirements.txt : +fastapi, uvicorn[standard], jinja2, python-multipart, httpx
- config.py : +DB_PATH (lu depuis TICKETTRACKER_DB_PATH)
Lancement : python -m tickettracker.web.app
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 20:04:55 +01:00
|
|
|
|
|
|
|
|
import tickettracker.config as config
|
|
|
|
|
from tickettracker.db.schema import get_connection
|
|
|
|
|
from tickettracker.web.queries import (
|
|
|
|
|
get_all_receipts,
|
|
|
|
|
get_compare_prices,
|
|
|
|
|
get_dashboard_stats,
|
|
|
|
|
get_product_history,
|
|
|
|
|
get_receipt_detail,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/stats")
|
|
|
|
|
def api_stats():
|
|
|
|
|
"""Statistiques globales (nb tickets, total dépensé, etc.)."""
|
|
|
|
|
conn = get_connection(config.DB_PATH)
|
|
|
|
|
try:
|
|
|
|
|
return get_dashboard_stats(conn)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/compare")
|
|
|
|
|
def api_compare():
|
|
|
|
|
"""Comparaison de prix Picnic vs Leclerc pour les produits communs."""
|
|
|
|
|
conn = get_connection(config.DB_PATH)
|
|
|
|
|
try:
|
|
|
|
|
return get_compare_prices(conn)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/product/{name:path}/history")
|
|
|
|
|
def api_product_history(name: str):
|
|
|
|
|
"""Historique des prix d'un produit normalisé.
|
|
|
|
|
|
|
|
|
|
Retourne 404 si le produit est inconnu.
|
|
|
|
|
Le paramètre {name:path} autorise les '/' dans le nom normalisé.
|
|
|
|
|
"""
|
|
|
|
|
conn = get_connection(config.DB_PATH)
|
|
|
|
|
try:
|
|
|
|
|
data = get_product_history(conn, name)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
if data is None:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Produit introuvable")
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/receipts")
|
|
|
|
|
def api_receipts():
|
|
|
|
|
"""Liste tous les tickets avec leur nombre d'articles."""
|
|
|
|
|
conn = get_connection(config.DB_PATH)
|
|
|
|
|
try:
|
|
|
|
|
return get_all_receipts(conn)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 18:02:48 +01:00
|
|
|
@router.post("/match/{match_id}/validate")
|
|
|
|
|
def api_match_validate(match_id: int):
|
|
|
|
|
"""Valide une paire fuzzy (status → 'validated').
|
|
|
|
|
|
|
|
|
|
Retourne 404 si l'id est inconnu.
|
|
|
|
|
"""
|
|
|
|
|
conn = get_connection(config.DB_PATH)
|
|
|
|
|
try:
|
|
|
|
|
with conn:
|
|
|
|
|
cur = conn.execute(
|
|
|
|
|
"UPDATE product_matches SET status='validated' WHERE id=?",
|
|
|
|
|
(match_id,),
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
if cur.rowcount == 0:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Match introuvable")
|
|
|
|
|
return {"status": "validated", "id": match_id}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/match/{match_id}/reject")
|
|
|
|
|
def api_match_reject(match_id: int):
|
|
|
|
|
"""Rejette une paire fuzzy (status → 'rejected').
|
|
|
|
|
|
|
|
|
|
Retourne 404 si l'id est inconnu.
|
|
|
|
|
"""
|
|
|
|
|
conn = get_connection(config.DB_PATH)
|
|
|
|
|
try:
|
|
|
|
|
with conn:
|
|
|
|
|
cur = conn.execute(
|
|
|
|
|
"UPDATE product_matches SET status='rejected' WHERE id=?",
|
|
|
|
|
(match_id,),
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
if cur.rowcount == 0:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Match introuvable")
|
|
|
|
|
return {"status": "rejected", "id": match_id}
|
|
|
|
|
|
|
|
|
|
|
feat: dashboard web FastAPI Sprint 4
Ajout d'un dashboard lecture seule par-dessus la DB SQLite existante.
Fichiers créés :
- tickettracker/web/queries.py : 7 fonctions SQL (stats, compare, historique...)
- tickettracker/web/api.py : router /api/* JSON (FastAPI)
- tickettracker/web/app.py : routes HTML + Jinja2 + point d'entrée uvicorn
- tickettracker/web/templates/ : base.html, index.html, compare.html, product.html, receipt.html
- tickettracker/web/static/style.css : personnalisations Pico CSS
- tests/test_web.py : 19 tests (96 passent, 1 xfail OCR)
Fichiers modifiés :
- requirements.txt : +fastapi, uvicorn[standard], jinja2, python-multipart, httpx
- config.py : +DB_PATH (lu depuis TICKETTRACKER_DB_PATH)
Lancement : python -m tickettracker.web.app
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 20:04:55 +01:00
|
|
|
@router.get("/receipt/{receipt_id}")
|
|
|
|
|
def api_receipt_detail(receipt_id: int):
|
|
|
|
|
"""Détail d'un ticket et de ses articles.
|
|
|
|
|
|
|
|
|
|
Retourne 404 si l'id est inconnu.
|
|
|
|
|
"""
|
|
|
|
|
conn = get_connection(config.DB_PATH)
|
|
|
|
|
try:
|
|
|
|
|
data = get_receipt_detail(conn, receipt_id)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
if data is None:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Ticket introuvable")
|
|
|
|
|
return data
|