diff --git a/.env.install b/.env.install index 0d90f13..732ec24 100644 --- a/.env.install +++ b/.env.install @@ -6,7 +6,7 @@ MYSQL_PASSWORD= STL_UPLOAD_DIR=/data/stl TZ=Europe/Berlin FILAMENTDB_URL=https://web.filamentdb.orb.local -VIKUNJA_URL=https://vikunja.example.com +VIKUNJA_URL=https://task.example.com VIKUNJA_API_TOKEN= VIKUNJA_PROJECT_ID= VAT_RATE=19 diff --git a/app/vikunja.py b/app/vikunja.py index b4be014..57b6f81 100644 --- a/app/vikunja.py +++ b/app/vikunja.py @@ -6,6 +6,8 @@ import httpx logger = logging.getLogger("pops") +_pops_label_id: int | None = None + def _base() -> str: return os.environ.get("VIKUNJA_URL", "").rstrip("/") @@ -22,6 +24,33 @@ def _project_id() -> int: return int(os.environ.get("VIKUNJA_PROJECT_ID", 0)) +def _get_or_create_pops_label() -> int | None: + """Return the ID of the 'POPS' label, creating it if it doesn't exist.""" + global _pops_label_id + if _pops_label_id is not None: + return _pops_label_id + try: + r = httpx.get(f"{_base()}/api/v1/labels", headers=_headers(), timeout=5) + r.raise_for_status() + for label in r.json() or []: + if label.get("title") == "POPS": + _pops_label_id = label["id"] + return _pops_label_id + # Not found — create it + r = httpx.put( + f"{_base()}/api/v1/labels", + headers=_headers(), + json={"title": "POPS"}, + timeout=5, + ) + r.raise_for_status() + _pops_label_id = r.json()["id"] + return _pops_label_id + except Exception: + logger.warning("Vikunja: failed to get or create POPS label") + return None + + def get_tasks() -> list[dict]: try: r = httpx.get( @@ -38,11 +67,15 @@ def get_tasks() -> list[dict]: def create_task(title: str) -> dict | None: + label_id = _get_or_create_pops_label() + payload: dict = {"title": title} + if label_id: + payload["labels"] = [{"id": label_id}] try: r = httpx.put( f"{_base()}/api/v1/projects/{_project_id()}/tasks", headers=_headers(), - json={"title": title}, + json=payload, timeout=5, ) r.raise_for_status()