requirements.txt changed, auth.py file added for user management, announcement_service_spec.yaml changed since api definition did not load, main.py changes to access new mongoDB collection, patchnotes_controller.py expendet so save user last login

This commit is contained in:
2025-05-21 17:02:09 +02:00
parent bdf0466d79
commit 7f8d7cbc86
5 changed files with 78 additions and 41 deletions
@@ -15,8 +15,9 @@ from connexion import ProblemException
from flask import Response
from __main__ import collection
from openapi_server.__main__ import collection
from openapi_server.__main__ import collection, last_login_collection
from openapi_server.models.patch_notes import PatchNotes
from openapi_server.utils.auth import current_user_id
def create_patchnote(body): # noqa: E501
@@ -119,36 +120,46 @@ def get_patchnote(patch_id): # noqa: E501
def list_patchnotes(since: str | None = None):
# 1) validate query param only if present
"""GET /patchnotes – liefert neue Patchnotes und setzt last_login."""
user_id = current_user_id() # 1) aktueller Benutzer
# 2) Wenn ?since fehlt, gespeichertes last_login holen
if since is None:
rec = last_login_collection.find_one({"user_id": user_id})
since = rec["last_login"] if rec else None
# 3) Datum validieren (nur wenn vorhanden)
if since:
try:
datetime.strptime(since, "%Y-%m-%d")
except ValueError:
raise ProblemException(
title="Bad Request",
detail="The date is invalid. Needs to be in this format YYYY-MM-DD",
detail="since must be YYYY-MM-DD",
status=400,
)
query = {"patch_date": {"$gte": since}}
mongo_filter = {"patch_date": {"$gte": since}}
else:
query = {} # no filter -> all notes
mongo_filter = {} # erster Login → alle Notes
# 2) fetch from Mongo
docs = list(collection.find(query).sort("patch_date", 1))
# 3) ensure every patch_date is *string* before serialising
# 4) Patchnotes abholen
docs = list(collection.find(mongo_filter).sort("patch_date", 1))
for d in docs:
if isinstance(d.get("patch_date"), (datetime, date)):
d["patch_date"] = d["patch_date"].strftime("%Y-%m-%d")
d.pop("_id", None) # optional: hide Mongo’s internal id
d.pop("_id", None)
if isinstance(d["patch_date"], (datetime, date)):
d["patch_date"] = d["patch_date"].isoformat()
# 4) serialise – default=str handles any remaining non-JSON types
return Response(
json_util.dumps(docs, default=str),
mimetype="application/json",
status=200,
# 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)
@@ -206,3 +217,5 @@ def update_patchnote(patch_id, body): # noqa: E501
mimetype="application/json",
status=200,
)