Ajoute les rappels de pointage via ntfy et détection de présence Tasker
Notifie l'utilisateur toutes les 5 min s'il est présent dans les locaux sans avoir pointé le matin, ou parti sans avoir dépointé la sortie.
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
"""Flat-file storage: JSON files per week for pointages, one file for congés."""
|
||||
import json
|
||||
import secrets
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
@ -125,6 +126,60 @@ def save_conges(conges: list[dict], user_id: str = ""):
|
||||
)
|
||||
|
||||
|
||||
# ── Notifications (ntfy topic + presence tracking) ────────────────────────────
|
||||
|
||||
def _notif_file(user_id: str) -> Path:
|
||||
return _user_root(user_id) / "notif.json"
|
||||
|
||||
|
||||
def load_notif_config(user_id: str) -> dict:
|
||||
"""Return {"ntfy_topic": str|None, "token": str}, generating a token on first use."""
|
||||
f = _notif_file(user_id)
|
||||
if f.exists():
|
||||
cfg = json.loads(f.read_text())
|
||||
if cfg.get("token"):
|
||||
return cfg
|
||||
else:
|
||||
cfg = {"ntfy_topic": None}
|
||||
cfg["token"] = secrets.token_hex(16)
|
||||
_ensure_user_dirs(user_id)
|
||||
f.write_text(json.dumps(cfg, indent=2, ensure_ascii=False))
|
||||
return cfg
|
||||
|
||||
|
||||
def save_notif_config(user_id: str, ntfy_topic: str):
|
||||
cfg = load_notif_config(user_id)
|
||||
cfg["ntfy_topic"] = ntfy_topic.strip() or None
|
||||
_ensure_user_dirs(user_id)
|
||||
_notif_file(user_id).write_text(json.dumps(cfg, indent=2, ensure_ascii=False))
|
||||
|
||||
|
||||
def find_user_by_token(token: str) -> str | None:
|
||||
for user_id in list_users():
|
||||
f = _notif_file(user_id)
|
||||
if f.exists() and json.loads(f.read_text()).get("token") == token:
|
||||
return user_id
|
||||
return None
|
||||
|
||||
|
||||
def _presence_file(user_id: str) -> Path:
|
||||
return _user_root(user_id) / "presence.json"
|
||||
|
||||
|
||||
def load_presence(user_id: str) -> dict:
|
||||
f = _presence_file(user_id)
|
||||
if f.exists():
|
||||
return json.loads(f.read_text())
|
||||
return {"present": False, "since": None}
|
||||
|
||||
|
||||
def save_presence(user_id: str, present: bool, since: str | None):
|
||||
_ensure_user_dirs(user_id)
|
||||
_presence_file(user_id).write_text(
|
||||
json.dumps({"present": present, "since": since}, indent=2, ensure_ascii=False)
|
||||
)
|
||||
|
||||
|
||||
def toggle_conge(date: str, type_conge: str, user_id: str = ""):
|
||||
assert type_conge in ("jour", "matin", "aprem")
|
||||
conges = load_conges(user_id)
|
||||
|
||||
Reference in New Issue
Block a user