announcement_service_spec.yaml updated, patchnotes_controller.py updated accordingly to changes

This commit is contained in:
2025-05-21 12:55:42 +02:00
parent 5007a434ca
commit 87651e70d4
9 changed files with 586 additions and 329 deletions
@@ -1,3 +1,11 @@
import connexion
from typing import Dict
from typing import Tuple
from typing import Union
from openapi_server.models.patch_notes import PatchNotes # noqa: E501
from openapi_server.models.patch_notes_input import PatchNotesInput # noqa: E501
from openapi_server import util
import logging
import uuid
from datetime import datetime
@@ -8,47 +16,55 @@ from flask import Response
from __main__ import collection
from openapi_server.__main__ import collection
from openapi_server.models.patch_notes import PatchNotes # noqa: E501
from openapi_server.models.patch_notes import PatchNotes
def patchnotes_date_get(date): # noqa: E501
"""patchnotes_date_get
def create_patchnote(body): # noqa: E501
"""Creates new patch notes
returns list of all unseen Patch notes from the user # noqa: E501
# noqa: E501
:param date:
:type date: str
:param body:
:param patch_notes_input:
:type patch_notes_input: dict | bytes
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
:rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]]
"""
patch_notes_input = body
if body is None:
return Response('error: No patch notes provided', status=400)
pn = PatchNotes.from_dict(body)
try:
datetime.strptime(date, '%Y-%m-%d') # 2025-01-0
date_obj = datetime.strptime(pn.patch_date, '%Y-%m-%d').date()
date_str = date_obj.strftime('%Y-%m-%d')
except ValueError as e:
raise ProblemException(
title="Bad Request",
detail="The date is invalid. Needs to be in this Format YYYY-MM-DD",
status=400,
)
query = {
'patch_date': {'$gte': date}
patch_note = {
'patchID': str(uuid.uuid4()),
'title': pn.title,
'changes': pn.changes,
'version': pn.version,
'patch_date': date_str
}
results = []
for entry in collection.find(query):
patch_note = PatchNotes.from_dict(entry)
results.append(patch_note.to_dict())
return Response(
json_util.dumps(results),
mimetype='application/json',
status=200,
)
collection.insert_one(patch_note)
patch_note_obj = PatchNotes.from_dict(patch_note)
patch_note = patch_note_obj.to_dict()
return Response(json_util.dumps(patch_note), mimetype='application/json',
status=200)
def patchnotes_patch_iddelete(patch_id): # noqa: E501
"""patchnotes_patch_iddelete
def delete_patchnote(patch_id): # noqa: E501
"""Deletes one patch note
deletes one Patch note # noqa: E501
# noqa: E501
:param patch_id:
:param patch_id: ID of the patch note
:type patch_id: str
:type patch_id: str
@@ -62,23 +78,85 @@ def patchnotes_patch_iddelete(patch_id): # noqa: E501
status=404,
)
return Response(
"Document was deleted",
"Document was deleted",
mimetype='application/json',
status=200,
)
def patchnotes_patch_idput(patch_id, body): # noqa: E501
"""patchnotes_patch_idput
def get_patchnote(patch_id): # noqa: E501
"""Returns one patch note
Updates one Patch # noqa: E501
# noqa: E501
:param patch_id: ID of the patch note
:type patch_id: str
:type patch_id: str
:rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]]
"""
pn = collection.find_one({'patchID': patch_id})
if pn is None:
raise ProblemException(
title="Not found",
detail="No document has been found",
status=404,
)
return Response(
json_util.dumps(pn),
mimetype='application/json',
status=200,
)
def list_patchnotes(since=None, limit=None, offset=None): # noqa: E501
"""Returns list of patch notes newer than the caller’s 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]]
"""
try:
datetime.strptime(since, '%Y-%m-%d') # 2025-01-0
except ValueError as e:
raise ProblemException(
title="Bad Request",
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())
return Response(
json_util.dumps(results),
mimetype='application/json',
status=200,
)
def update_patchnote(patch_id, body): # noqa: E501
"""Updates one patch note (full replace)
# noqa: E501
:param body:
:param patch_id:
:param patch_id: ID of the patch note
:type patch_id: str
:type patch_id: str
:param patch_notes_input:
:type patch_notes_input: dict | bytes
:rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]]
:rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]]
"""
pn = PatchNotes.from_dict(body)
try:
@@ -93,10 +171,10 @@ def patchnotes_patch_idput(patch_id, body): # noqa: E501
query_filter = {'patchID': patch_id}
updated_post = {'$set':
{
'title': pn.title,
'changes': pn.changes,
'version': pn.version,
'date': date_str
'title': pn.title,
'changes': pn.changes,
'version': pn.version,
'date': date_str
}
}
result = collection.update_one(query_filter, updated_post)
@@ -107,108 +185,3 @@ def patchnotes_patch_idput(patch_id, body): # noqa: E501
status=404,
)
return Response("Document was updated", status=200, mimetype='application/json')
def patchnotes_post(body):
"""patchnotes_post
creates new Patch notes # noqa: E501
:param body:
:param patch_notes:
:type patch_notes: dict | bytes
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
"""
if body is None:
return Response('error: No patch notes provided', status=400)
pn = PatchNotes.from_dict(body)
try:
date_obj = datetime.strptime(pn.patch_date, '%Y-%m-%d').date()
date_str = date_obj.strftime('%Y-%m-%d')
except ValueError as e:
raise ProblemException(
title="Bad Request",
detail="The date is invalid. Needs to be in this Format YYYY-MM-DD",
status=400,
)
patch_note = {
'patchID': str(uuid.uuid4()),
'title': pn.title,
'changes': pn.changes,
'version': pn.version,
'patch_date': date_str
}
collection.insert_one(patch_note)
patch_note_obj = PatchNotes.from_dict(patch_note)
patch_note = patch_note_obj.to_dict()
return Response(json_util.dumps(patch_note), mimetype='application/json',
status=200)
def patchnotes_post(body):
"""patchnotes_post
creates new Patch notes # noqa: E501
:param body:
:param patch_notes:
:type patch_notes: dict | bytes
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
"""
if body is None:
return Response('error: No patch notes provided', status=400)
pn = PatchNotes.from_dict(body)
try:
date_obj = datetime.strptime(pn.patch_date, '%Y-%m-%d').date()
date_str = date_obj.strftime('%Y-%m-%d')
except ValueError as e:
raise ProblemException(
title="Bad Request",
detail="The date is invalid. Needs to be in this Format YYYY-MM-DD",
status=400,
)
patch_note = {
'patchID': str(uuid.uuid4()),
'title': pn.title,
'changes': pn.changes,
'version': pn.version,
'patch_date': date_str
}
collection.insert_one(patch_note)
patch_note_obj = PatchNotes.from_dict(patch_note)
patch_note = patch_note_obj.to_dict()
return Response(json_util.dumps(patch_note), mimetype='application/json',
status=200)
def patchnotes_get(): # noqa: E501
"""patchnotes_get
returns list of all Patch notes # noqa: E501
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
"""
results = []
for entry in collection.find({}):
patch_note = PatchNotes.from_dict(entry)
results.append(patch_note.to_dict())
return Response(
json_util.dumps(results),
mimetype='application/json',
status=200,
)
def cors_options_handler():
from flask import make_response
response = make_response('')
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
return response