Files
pointeuse-optimisator/app/models.py
toto 6258fd3662 Ajoute colonnes Δ sem. / Fin cible, supprime solde global, indicateurs minima
- Nouvelles colonnes : Δ sem. (solde cumulé semaine) et Fin cible (heure de
  sortie aprem optimale pour 0h suppl. en fin de semaine)
- Δ sem. n'est affiché que quand la journée est complète et la chaîne de jours
  ininterrompue (pas de saut de jour non saisi)
- Fin cible disparaît quand la journée est validée (passée/complète)
- Delta = "—" pour les jours non travaillés (futur ou congé jour)
- Indicateur warn (fond rouge) sur matin/aprem si horaires < minimum
  (matin 09:00→12:00, aprem 14:00→17:00) ; mise à jour live via JS
- Suppression du solde global (3 cartes au lieu de 4)
- Tableau sans largeurs fixes + container 1100 px pour éviter scroll
  horizontal sur écrans normaux ; touch-scroll conservé sur mobile
- Base reste 39h/semaine (7h48/jour)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-24 15:35:17 +02:00

110 lines
3.2 KiB
Python

"""Flat-file storage: JSON files per week for pointages, one file for congés."""
import json
from pathlib import Path
DATA_DIR = Path("/data")
POINTAGES_DIR = DATA_DIR / "pointages"
CONGES_FILE = DATA_DIR / "conges.json"
CONFIG_FILE = DATA_DIR / "config.json"
DEFAULT_CONFIG = {
"heures_jour": 7.8, # 7h48 = 39h/semaine
}
def _ensure_dirs():
POINTAGES_DIR.mkdir(parents=True, exist_ok=True)
# ---------- config ----------
def load_config() -> dict:
_ensure_dirs()
if CONFIG_FILE.exists():
return json.loads(CONFIG_FILE.read_text())
CONFIG_FILE.write_text(json.dumps(DEFAULT_CONFIG, indent=2))
return DEFAULT_CONFIG.copy()
def save_config(cfg: dict):
_ensure_dirs()
CONFIG_FILE.write_text(json.dumps(cfg, indent=2, ensure_ascii=False))
# ---------- pointages ----------
def _week_file(year: int, week: int) -> Path:
return POINTAGES_DIR / f"{year}-W{week:02d}.json"
def load_week_pointages(year: int, week: int) -> list[dict]:
f = _week_file(year, week)
if f.exists():
return json.loads(f.read_text())
return []
def save_week_pointages(year: int, week: int, pointages: list[dict]):
_ensure_dirs()
_week_file(year, week).write_text(json.dumps(pointages, indent=2, ensure_ascii=False))
def upsert_pointages(records: list[dict]):
"""Insert or update pointage records, grouped by ISO week."""
from datetime import datetime
by_week: dict[tuple, dict[str, dict]] = {}
for r in records:
d = datetime.strptime(r["date"], "%Y-%m-%d").date()
iso = d.isocalendar()
key = (iso.year, iso.week)
by_week.setdefault(key, {})
by_week[key][r["date"]] = r
for (year, week), new_by_date in by_week.items():
existing = {p["date"]: p for p in load_week_pointages(year, week)}
existing.update(new_by_date)
save_week_pointages(year, week, sorted(existing.values(), key=lambda p: p["date"]))
def list_all_weeks() -> list[tuple[int, int]]:
"""Return sorted list of (year, week) for which we have data."""
_ensure_dirs()
weeks = []
for f in POINTAGES_DIR.glob("????-W??.json"):
parts = f.stem.split("-W")
if len(parts) == 2:
weeks.append((int(parts[0]), int(parts[1])))
return sorted(weeks)
# ---------- congés ----------
def load_conges() -> list[dict]:
if CONGES_FILE.exists():
return json.loads(CONGES_FILE.read_text())
return []
def save_conges(conges: list[dict]):
_ensure_dirs()
CONGES_FILE.write_text(json.dumps(conges, indent=2, ensure_ascii=False))
def toggle_conge(date: str, type_conge: str):
assert type_conge in ("jour", "matin", "aprem")
conges = load_conges()
already = any(c["date"] == date and c["type"] == type_conge for c in conges)
if already:
conges = [c for c in conges if not (c["date"] == date and c["type"] == type_conge)]
else:
# Adding: remove conflicting entries first
if type_conge == "jour":
conges = [c for c in conges if c["date"] != date]
else:
conges = [c for c in conges if not (c["date"] == date and c["type"] == "jour")]
conges.append({"date": date, "type": type_conge})
conges.sort(key=lambda c: (c["date"], c["type"]))
save_conges(conges)