patchnotes_controller.py done
This commit is contained in:
@@ -1,50 +1,17 @@
|
||||
import logging
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from xxlimited_35 import error
|
||||
|
||||
from bson import json_util
|
||||
from connexion import ProblemException
|
||||
from connexion.lifecycle import ConnexionResponse
|
||||
from flask import Response
|
||||
|
||||
from openapi_server import util
|
||||
from openapi_server.__main__ import collection
|
||||
from openapi_server.models.patch_notes import PatchNotes # noqa: E501
|
||||
|
||||
|
||||
def patchnotes_date_get1(date): # noqa: E501
|
||||
"""patchnotes_date_get
|
||||
|
||||
returns list of all unseen Patch notes from the user # noqa: E501
|
||||
|
||||
:param date:
|
||||
:type date: str
|
||||
|
||||
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
|
||||
"""
|
||||
logging.error(type(date))
|
||||
|
||||
try:
|
||||
formatted_date = datetime.strptime(date, '%Y-%m-%d')
|
||||
logging.error(formatted_date)
|
||||
except ValueError:
|
||||
logging.error("Not valid date")
|
||||
|
||||
try:
|
||||
formatted_date = formatted_date.strftime('%Y-%m-%d')
|
||||
logging.error('formatted date:' + str(formatted_date))
|
||||
logging.info("valid date")
|
||||
except ValueError:
|
||||
logging.error("Not valid date")
|
||||
|
||||
date = util.deserialize_date(date)
|
||||
logging.error('date:' + str(date))
|
||||
query = {
|
||||
'date': {'$gte': date.strftime('%Y-%m-%d')}
|
||||
}
|
||||
results = list(collection.find(query))
|
||||
logging.error('results:' + str(results))
|
||||
return Response(json_util.dumps(f'all unseen patchnotes: {results}'), mimetype='application/json', status=200)
|
||||
|
||||
|
||||
def patchnotes_date_get(date): # noqa: E501
|
||||
"""patchnotes_date_get
|
||||
|
||||
@@ -55,8 +22,18 @@ def patchnotes_date_get(date): # noqa: E501
|
||||
|
||||
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
|
||||
"""
|
||||
logging.error(type(date))
|
||||
try:
|
||||
datetime.strptime(date, '%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,
|
||||
)
|
||||
logging.error('date:' + str(date))
|
||||
query = {
|
||||
'date': {'$gte': date}
|
||||
'patch_date': {'$gte': date}
|
||||
}
|
||||
results = []
|
||||
for entry in collection.find(query):
|
||||
@@ -80,10 +57,22 @@ def patchnotes_patch_iddelete(patch_id): # noqa: E501
|
||||
|
||||
:rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]]
|
||||
"""
|
||||
return 'do some magic!'
|
||||
deleted_doc = collection.delete_one({'patchID': patch_id})
|
||||
logging.error(deleted_doc.deleted_count)
|
||||
if deleted_doc.deleted_count == 0:
|
||||
raise ProblemException(
|
||||
title="Not found",
|
||||
detail="No document has been deleted",
|
||||
status=404,
|
||||
)
|
||||
return Response(
|
||||
"Document was deleted",
|
||||
mimetype='application/json',
|
||||
status=200,
|
||||
)
|
||||
|
||||
|
||||
def patchnotes_post(body): # TODO _date to date change
|
||||
def patchnotes_post(body):
|
||||
"""patchnotes_post
|
||||
|
||||
creates new Patch notes # noqa: E501
|
||||
@@ -95,22 +84,33 @@ def patchnotes_post(body): # TODO _date to date change
|
||||
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
|
||||
"""
|
||||
if body is None:
|
||||
return Response(json_util.dumps({'error': 'No patch notes provided'}), status=400)
|
||||
return Response('error: No patch notes provided', status=400)
|
||||
pn = PatchNotes.from_dict(body)
|
||||
logging.error('patch_notes_type:' + str(type(pn)))
|
||||
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,
|
||||
)
|
||||
|
||||
logging.error('patch_notes_type:' + str(type(date_obj)))
|
||||
logging.error('patch notes' + str(pn))
|
||||
|
||||
patch_notes_uuid = uuid.uuid4()
|
||||
|
||||
patch_notes = {
|
||||
'patchID': str(patch_notes_uuid),
|
||||
patch_note = {
|
||||
'patchID': str(uuid.uuid4()),
|
||||
'title': pn.title,
|
||||
'changes': pn.changes,
|
||||
'date': pn._date,
|
||||
'patch_date': date_str
|
||||
}
|
||||
logging.error('patch_notes_type:' + str(type(patch_notes)))
|
||||
logging.error('patch notes' + str(patch_notes))
|
||||
posted_patch_id = collection.insert_one(patch_notes)
|
||||
|
||||
return Response(json_util.dumps(f'patch with ID: {patch_notes} was created'), mimetype='application/json',
|
||||
logging.error('patch_notes_type:' + str(type(patch_note)))
|
||||
logging.error('patch notes' + str(patch_note))
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user