from flask_smorest import abort from flask.views import MethodView from ...database import Database from ...models.requests import UuidSchema, RequestsInteractionDelete, RequestStatus from ...models.requestentry import RequestDatabaseEntry from . import routes_requests as blp db = Database.get_instance().db @blp.route("/") class InteractRequest(MethodView): @blp.doc(summary="Returns a specific request by its UID") @blp.arguments(UuidSchema, location='path', as_kwargs=True) @blp.response(200, RequestDatabaseEntry, description="Successful operation") @blp.alt_response(404) def get(self, uuid): response = db.at("requests", key=uuid).read() if response is None: abort(404, message=f"Request {uuid} does not exist.") response.update({ "status": RequestStatus(response["status"]) }) return response @blp.doc(summary="Deletes a specific request by its UID") @blp.arguments(UuidSchema, location='path', as_kwargs=True) @blp.response(200, RequestsInteractionDelete, description="Successful operation") @blp.alt_response(404) def delete(self, uuid): if not db.at("requests", key=uuid).exists(): abort(404, message=f"Request {uuid} does not exist.") with db.at("requests").session() as (session, requests): del requests[uuid] session.write() response = { "status": RequestStatus.DELETED, "message": f"Request {uuid} was deleted." } return response