2023-10-01 19:40:06 +00:00
|
|
|
from flask_smorest import abort
|
2023-10-01 17:47:35 +00:00
|
|
|
from flask.views import MethodView
|
|
|
|
|
2023-11-24 19:50:18 +00:00
|
|
|
from ...database import Database
|
|
|
|
from ...models.requests import UuidSchema, RequestsInteractionDelete, RequestStatus
|
|
|
|
from ...models.requestentry import RequestDatabaseEntry
|
2023-10-01 17:47:35 +00:00
|
|
|
from . import routes_requests as blp
|
|
|
|
|
2023-10-01 19:40:06 +00:00
|
|
|
db = Database.get_instance().db
|
|
|
|
|
|
|
|
@blp.route("/<string:uuid>")
|
2023-10-01 17:47:35 +00:00
|
|
|
class InteractRequest(MethodView):
|
|
|
|
@blp.doc(summary="Returns a specific request by its UID")
|
2023-10-01 19:40:06 +00:00
|
|
|
@blp.arguments(UuidSchema, location='path', as_kwargs=True)
|
2023-10-03 16:25:35 +00:00
|
|
|
@blp.response(200, RequestDatabaseEntry, description="Successful operation")
|
2023-10-01 19:40:06 +00:00
|
|
|
@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.")
|
2023-10-03 16:25:35 +00:00
|
|
|
response.update({
|
|
|
|
"status": RequestStatus(response["status"])
|
|
|
|
})
|
2023-10-01 17:47:35 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
@blp.doc(summary="Deletes a specific request by its UID")
|
2023-10-01 19:40:06 +00:00
|
|
|
@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 = {
|
2023-10-02 13:29:19 +00:00
|
|
|
"status": RequestStatus.DELETED,
|
|
|
|
"message": f"Request {uuid} was deleted."
|
2023-10-01 19:40:06 +00:00
|
|
|
}
|
2023-10-01 17:47:35 +00:00
|
|
|
return response
|