list_patchnotes to fix unbound variable error
This commit is contained in:
parent
8d4bcfdf0c
commit
5ec51451ee
|
|
@ -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):
|
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() # 1) aktueller Benutzer
|
user_id = current_user_id()
|
||||||
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 = {}
|
||||||
|
|
||||||
if since:
|
if since:
|
||||||
query = {
|
# since validieren
|
||||||
"patch_date": {
|
|
||||||
"$gte": last_login,
|
|
||||||
"$lte": since
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try:
|
try:
|
||||||
datetime.strptime(since, "%Y-%m-%d")
|
since_date = datetime.strptime(since, "%Y-%m-%d").date()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ProblemException(
|
raise ProblemException(
|
||||||
title="Bad Request",
|
title="Bad Request",
|
||||||
detail="since must be YYYY-MM-DD",
|
detail="since must be in format 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()
|
||||||
|
|
||||||
# 3) Datum validieren (nur wenn vorhanden)
|
# Datumsbereichs-Query
|
||||||
elif since is None:
|
query = {
|
||||||
try:
|
"patch_date": {
|
||||||
docs = collection.find({})
|
"$gte": last_login if last_login else date.min,
|
||||||
for d in docs:
|
"$lte": since_date
|
||||||
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["patch_date"], (datetime, date)):
|
if isinstance(d.get("patch_date"), (datetime, date)):
|
||||||
d["patch_date"] = d["patch_date"].isoformat()
|
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()
|
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,
|
||||||
)
|
)
|
||||||
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)
|
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)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue