maj via codex
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlmodel import Session, select
|
||||
@@ -7,10 +8,45 @@ from app.models.astuce import Astuce
|
||||
router = APIRouter(tags=["astuces"])
|
||||
|
||||
|
||||
def _decode_tags(raw: Optional[str]) -> list[str]:
|
||||
if not raw:
|
||||
return []
|
||||
try:
|
||||
parsed = json.loads(raw)
|
||||
except Exception:
|
||||
parsed = [p.strip() for p in raw.split(",") if p.strip()]
|
||||
if not isinstance(parsed, list):
|
||||
return []
|
||||
return [str(x).strip().lower() for x in parsed if str(x).strip()]
|
||||
|
||||
|
||||
def _decode_mois(raw: Optional[str]) -> list[int]:
|
||||
if not raw:
|
||||
return []
|
||||
try:
|
||||
parsed = json.loads(raw)
|
||||
except Exception:
|
||||
parsed = [p.strip() for p in raw.split(",") if p.strip()]
|
||||
if not isinstance(parsed, list):
|
||||
return []
|
||||
result: list[int] = []
|
||||
for x in parsed:
|
||||
try:
|
||||
month = int(x)
|
||||
if 1 <= month <= 12:
|
||||
result.append(month)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/astuces", response_model=List[Astuce])
|
||||
def list_astuces(
|
||||
entity_type: Optional[str] = Query(None),
|
||||
entity_id: Optional[int] = Query(None),
|
||||
categorie: Optional[str] = Query(None),
|
||||
tag: Optional[str] = Query(None),
|
||||
mois: Optional[int] = Query(None, ge=1, le=12),
|
||||
session: Session = Depends(get_session),
|
||||
):
|
||||
q = select(Astuce)
|
||||
@@ -18,7 +54,21 @@ def list_astuces(
|
||||
q = q.where(Astuce.entity_type == entity_type)
|
||||
if entity_id is not None:
|
||||
q = q.where(Astuce.entity_id == entity_id)
|
||||
return session.exec(q).all()
|
||||
|
||||
if categorie:
|
||||
q = q.where(Astuce.categorie == categorie)
|
||||
|
||||
items = session.exec(q).all()
|
||||
|
||||
if tag:
|
||||
wanted = tag.strip().lower()
|
||||
items = [a for a in items if wanted in _decode_tags(a.tags)]
|
||||
|
||||
if mois is not None:
|
||||
# mois null/empty = astuce valable toute l'année
|
||||
items = [a for a in items if not _decode_mois(a.mois) or mois in _decode_mois(a.mois)]
|
||||
|
||||
return items
|
||||
|
||||
|
||||
@router.post("/astuces", response_model=Astuce, status_code=status.HTTP_201_CREATED)
|
||||
|
||||
Reference in New Issue
Block a user