From 5ec51451ee1f19bc4c27354f0a315490b794c2c1 Mon Sep 17 00:00:00 2001 From: justinbaer Date: Thu, 26 Jun 2025 09:58:55 +0200 Subject: [PATCH] list_patchnotes to fix unbound variable error --- .../controllers/patchnotes_controller.py | 62 ++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/gen/openapi_server/controllers/patchnotes_controller.py b/gen/openapi_server/controllers/patchnotes_controller.py index 411f9d8..e07badf 100644 --- a/gen/openapi_server/controllers/patchnotes_controller.py +++ b/gen/openapi_server/controllers/patchnotes_controller.py @@ -120,63 +120,67 @@ def get_patchnote(patch_id): # noqa: E501 ) +from datetime import datetime, date +from flask import Response +from bson import json_util + def list_patchnotes(since: str | None = None): """GET /patchnotes – liefert neue Patchnotes und setzt last_login.""" - user_id = current_user_id() # 1) aktueller Benutzer - last_login = last_login_collection.find_one({"user_id": user_id}, {"last_login"}) + user_id = current_user_id() + + # Last login aus DB holen + last_login_doc = last_login_collection.find_one({"user_id": user_id}) + last_login = last_login_doc.get("last_login") if last_login_doc else None + + # Abfrage definieren (default: alles holen) + query = {} + if since: - query = { - "patch_date": { - "$gte": last_login, - "$lte": since - } - } + # since validieren try: - datetime.strptime(since, "%Y-%m-%d") + since_date = datetime.strptime(since, "%Y-%m-%d").date() except ValueError: raise ProblemException( title="Bad Request", - detail="since must be YYYY-MM-DD", + detail="since must be in format YYYY-MM-DD", status=400, ) + # last_login ebenfalls in date konvertieren, wenn vorhanden + if last_login: + if isinstance(last_login, str): + last_login = datetime.strptime(last_login, "%Y-%m-%d").date() + elif isinstance(last_login, datetime): + last_login = last_login.date() - # 3) Datum validieren (nur wenn vorhanden) - elif since is None: - try: - docs = collection.find({}) - for d in docs: - d.pop("_id", None) - if isinstance(d["patch_date"], (datetime, date)): - d["patch_date"] = d["patch_date"].isoformat() - except ValueError as e: - raise ProblemException( - title="Internal Server Error", - detail=str(e), - status=500, - ) - # 4) Patchnotes abholen + # Datumsbereichs-Query + query = { + "patch_date": { + "$gte": last_login if last_login else date.min, + "$lte": since_date + } + } + # Patchnotes holen docs = list(collection.find(query).sort("patch_date", 1)) for d in docs: d.pop("_id", None) - if isinstance(d["patch_date"], (datetime, date)): + if isinstance(d.get("patch_date"), (datetime, date)): d["patch_date"] = d["patch_date"].isoformat() - # 5) Heutiges Datum als neuen last_login speichern + # Heutiges Datum als neuen last_login speichern today = date.today().isoformat() last_login_collection.update_one( {"user_id": user_id}, {"$set": {"last_login": today}}, upsert=True, ) - if since == last_login: - return Response(json_util.dumps([]), mimetype="application/json", status=200) return Response(json_util.dumps(docs), mimetype="application/json", status=200) + def update_patchnote(patch_id, body): # noqa: E501 """Updates one patch note (full replace)