072: regles automatiques eteintes par defaut (reglage regles_auto), plus de course ni de regle proposee automatiques
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XGaEeTZLvHXfYPUNWkgYoi
This commit is contained in:
1 parent
883b4e1745
commit
5d98d08a8b
6 files changed
+112
-5
No files matched your search
@@ -345,3 +345,23 @@ def creer_routeur(mirror_client) -> APIRouter:
|
||||
return {"analyses": lister(depuis=debut, n=n)}
|
||||
|
||||
return router
|
||||
|
||||
|
||||
def _routeur_regles_auto():
|
||||
from fastapi import APIRouter
|
||||
from pydantic import BaseModel
|
||||
from app import classifier as _c
|
||||
r = APIRouter()
|
||||
|
||||
class Etat(BaseModel):
|
||||
actif: bool
|
||||
|
||||
@r.get("/regles/auto")
|
||||
def lire_regles_auto():
|
||||
return {"actif": _c.regles_auto_actives()}
|
||||
|
||||
@r.put("/regles/auto")
|
||||
def regler_regles_auto(e: Etat):
|
||||
_c.regler_regles_auto(e.actif)
|
||||
return {"actif": _c.regles_auto_actives()}
|
||||
return r
|
||||
+38
-4
@@ -19,6 +19,40 @@ STOPWORDS = {
|
||||
"ces", "a", "à", "the", "of", "to", "in", "for", "and", "is", "on", "at", "by"
|
||||
}
|
||||
|
||||
def regles_auto_actives(db_path: Optional[str] = None) -> bool:
|
||||
"""Reglage global : les regles automatiques (mots-cles, expediteur, etiquette) ne decident plus
|
||||
du classement ni des courses tant qu'il est eteint (defaut). Decision de Nabil du 20/11/2026 :
|
||||
un meme expediteur ou un meme objet ne dit pas la meme chose d'un jour a l'autre, c'est le contenu
|
||||
(lu par l'IA, propose puis valide) qui decide."""
|
||||
from app.db import get_connection
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
conn.execute("CREATE TABLE IF NOT EXISTS reglages (cle TEXT PRIMARY KEY, valeur TEXT NOT NULL, le TEXT NOT NULL)")
|
||||
r = conn.execute("SELECT valeur FROM reglages WHERE cle='regles_auto'").fetchone()
|
||||
return bool(r and r[0] == "1")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def regler_regles_auto(actif: bool, db_path: Optional[str] = None) -> None:
|
||||
from app.db import get_connection
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
conn.execute("CREATE TABLE IF NOT EXISTS reglages (cle TEXT PRIMARY KEY, valeur TEXT NOT NULL, le TEXT NOT NULL)")
|
||||
with conn:
|
||||
conn.execute("INSERT INTO reglages (cle, valeur, le) VALUES ('regles_auto', ?, ?) "
|
||||
"ON CONFLICT(cle) DO UPDATE SET valeur=excluded.valeur, le=excluded.le",
|
||||
("1" if actif else "0", get_tunis_now_iso()))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def _regles_actives(db_path: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
if not regles_auto_actives(db_path):
|
||||
return []
|
||||
return list_regles(statut="active", db_path=db_path)
|
||||
|
||||
|
||||
def extract_keywords(text: Optional[str]) -> str:
|
||||
"""Extrait les mots significatifs d'un objet de mail."""
|
||||
if not text:
|
||||
@@ -79,7 +113,7 @@ def classify_mail(mail: Dict[str, Any], db_path: Optional[str] = None) -> Dict[s
|
||||
# Conserver le classement manuel existant
|
||||
return existing
|
||||
|
||||
rules = list_regles(statut="active", db_path=db_path)
|
||||
rules = _regles_actives(db_path)
|
||||
categorie = match_mail_to_rules(mail, rules)
|
||||
|
||||
tags = mail.get("etiquettes") or mail.get("tags") or []
|
||||
@@ -129,7 +163,7 @@ def manual_classify_and_propose(
|
||||
# Proposition d'une règle basée sur l'expéditeur et les mots-clés
|
||||
mots = extract_keywords(obj)
|
||||
proposed_rule = None
|
||||
if exp or mots:
|
||||
if (exp or mots) and regles_auto_actives(db_path):
|
||||
proposed_rule = add_regle(
|
||||
categorie=new_category,
|
||||
expediteur=exp or None,
|
||||
@@ -145,7 +179,7 @@ def reclassify_all_non_manual(db_path: Optional[str] = None, date_prefix: Option
|
||||
Réévalue et reclasse tous les emails non-manuels enregistrés en base.
|
||||
RÈGLE ABSOLUE : Les classements manuels ne sont jamais touchés.
|
||||
"""
|
||||
active_rules = list_regles(statut="active", db_path=db_path)
|
||||
active_rules = _regles_actives(db_path)
|
||||
all_classements = list_classements(date_prefix=date_prefix, db_path=db_path)
|
||||
reclassified_count = 0
|
||||
|
||||
@@ -226,7 +260,7 @@ def classify_batch(mails: List[Dict[str, Any]], db_path: Optional[str] = None) -
|
||||
mails = [m for m in mails if m.get("message_id")]
|
||||
if not mails:
|
||||
return []
|
||||
rules = list_regles(statut="active", db_path=db_path)
|
||||
rules = _regles_actives(db_path)
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
ids = [m["message_id"] for m in mails]
|
||||
|
||||
+5
-1
@@ -431,7 +431,10 @@ def exporter_courses(statut: Optional[str] = Query(None, pattern="^(a_faire|fait
|
||||
|
||||
@api.post("/courses/synchroniser")
|
||||
def synchroniser_courses(jours: int = Query(14, ge=1, le=30)):
|
||||
"""Cree une course par fil de discussion classe Tunis. Un fil supprime ou deja importe n'est pas recree."""
|
||||
"""Cree une course par fil classe Tunis, seulement si les regles automatiques sont reactivees (eteintes par defaut)."""
|
||||
from app.classifier import regles_auto_actives
|
||||
if not regles_auto_actives():
|
||||
return {"ajoutees": 0, "courses": [], "regles_auto": False}
|
||||
from datetime import date as _date, timedelta
|
||||
from app.suivi import sujet_normalise
|
||||
fin = _date.fromisoformat(get_tunis_now_iso()[:10])
|
||||
@@ -681,4 +684,5 @@ def confirm_rule_endpoint(regle_id: int):
|
||||
|
||||
api.include_router(creer_routeur(mirror_client))
|
||||
api.include_router(_analyse.creer_routeur(mirror_client))
|
||||
api.include_router(_analyse._routeur_regles_auto())
|
||||
app.include_router(api)
|
||||
Reference in new issue
Block a user