list_patchnotes to fix encoding error

This commit is contained in:
Justin Weins 2025-06-26 10:06:56 +02:00
parent 5ec51451ee
commit b58b56249a
1 changed files with 27 additions and 33 deletions

View File

@ -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): def list_patchnotes(since: str | None = None):
"""GET /patchnotes liefert neue Patchnotes und setzt last_login.""" """GET /patchnotes liefert neue Patchnotes und setzt last_login."""
user_id = current_user_id() user_id = current_user_id() # 1) aktueller Benutzer
last_login = last_login_collection.find_one({"user_id": user_id}, {"last_login"})
# 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 = {} query = {}
if since: if since:
# since validieren query = {
"patch_date": {
"$gte": last_login,
"$lte": since
}
}
try: try:
since_date = datetime.strptime(since, "%Y-%m-%d").date() datetime.strptime(since, "%Y-%m-%d")
except ValueError: except ValueError:
raise ProblemException( raise ProblemException(
title="Bad Request", title="Bad Request",
detail="since must be in format YYYY-MM-DD", detail="since must be YYYY-MM-DD",
status=400, 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 # 3) Datum validieren (nur wenn vorhanden)
query = { elif since is None:
"patch_date": { try:
"$gte": last_login if last_login else date.min, docs = collection.find({})
"$lte": since_date 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)) docs = list(collection.find(query).sort("patch_date", 1))
for d in docs: for d in docs:
d.pop("_id", None) 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() 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() today = date.today().isoformat()
last_login_collection.update_one( last_login_collection.update_one(
{"user_id": user_id}, {"user_id": user_id},
{"$set": {"last_login": today}}, {"$set": {"last_login": today}},
upsert=True, upsert=True,
) )
return Response(json_util.dumps(docs), mimetype="application/json", status=200) return Response(json_util.dumps(docs), mimetype="application/json", status=200)
def update_patchnote(patch_id, body): # noqa: E501 def update_patchnote(patch_id, body): # noqa: E501
"""Updates one patch note (full replace) """Updates one patch note (full replace)