patchnotes_controller.py get patchnotes bug fixes

This commit is contained in:
Justin Weins 2025-05-21 14:42:39 +02:00
parent 87651e70d4
commit ab26234c20
1 changed files with 26 additions and 29 deletions

View File

@ -8,7 +8,7 @@ from openapi_server.models.patch_notes_input import PatchNotesInput # noqa: E50
from openapi_server import util
import logging
import uuid
from datetime import datetime
from datetime import datetime, date
from bson import json_util
from connexion import ProblemException
@ -108,38 +108,35 @@ def get_patchnote(patch_id): # noqa: E501
status=200,
)
def list_patchnotes(since=None, limit=None, offset=None): # noqa: E501
"""Returns list of patch notes newer than the callers last login
# noqa: E501
:param since: Only patchnotes newer than this date (YYYY-MM-DD)
:type since: str
:param limit: Maximum number of items to return
:type limit: int
:param offset: Pagination offset
:type offset: int
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
"""
def list_patchnotes(since: str | None = None):
# 1) validate query param only if present
if since:
try:
datetime.strptime(since, '%Y-%m-%d') # 2025-01-0
except ValueError as e:
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="The date is invalid. Needs to be in this format YYYY-MM-DD",
status=400,
)
query = {
'patch_date': {'$gte': since}
}
results = []
for entry in collection.find(query):
patch_note = PatchNotes.from_dict(entry)
results.append(patch_note.to_dict())
query = {"patch_date": {"$gte": since}}
else:
query = {} # no filter -> all notes
# 2) fetch from Mongo
docs = list(collection.find(query).sort("patch_date", 1))
# 3) ensure every patch_date is *string* before serialising
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 Mongos internal id
# 4) serialise default=str handles any remaining non-JSON types
return Response(
json_util.dumps(results),
mimetype='application/json',
json_util.dumps(docs, default=str),
mimetype="application/json",
status=200,
)