From b58b56249a966aa7d6fd40471a3404ad6908e9c5 Mon Sep 17 00:00:00 2001 From: justinbaer Date: Thu, 26 Jun 2025 10:06:56 +0200 Subject: [PATCH] list_patchnotes to fix encoding error --- .../controllers/patchnotes_controller.py | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/gen/openapi_server/controllers/patchnotes_controller.py b/gen/openapi_server/controllers/patchnotes_controller.py index e07badf..4b74ded 100644 --- a/gen/openapi_server/controllers/patchnotes_controller.py +++ b/gen/openapi_server/controllers/patchnotes_controller.py @@ -120,67 +120,61 @@ 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() - - # 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) + user_id = current_user_id() # 1) aktueller Benutzer + last_login = last_login_collection.find_one({"user_id": user_id}, {"last_login"}) query = {} - if since: - # since validieren + query = { + "patch_date": { + "$gte": last_login, + "$lte": since + } + } try: - since_date = datetime.strptime(since, "%Y-%m-%d").date() + datetime.strptime(since, "%Y-%m-%d") except ValueError: raise ProblemException( title="Bad Request", - detail="since must be in format YYYY-MM-DD", + detail="since must be 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() - # Datumsbereichs-Query - query = { - "patch_date": { - "$gte": last_login if last_login else date.min, - "$lte": since_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 - # Patchnotes holen docs = list(collection.find(query).sort("patch_date", 1)) for d in docs: d.pop("_id", None) - if isinstance(d.get("patch_date"), (datetime, date)): + if isinstance(d["patch_date"], (datetime, date)): d["patch_date"] = d["patch_date"].isoformat() - # Heutiges Datum als neuen last_login speichern + # 5) 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, ) - 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)