1
|
|
|
from flask_restful import Resource, reqparse |
2
|
|
|
from flask import json, request, jsonify |
3
|
|
|
from models import dtrequest |
4
|
|
|
|
5
|
|
|
from flask_jwt_extended import (create_access_token,create_refresh_token, |
6
|
|
|
jwt_required, jwt_refresh_token_required,get_jwt_identity, get_raw_jwt) |
7
|
|
|
|
8
|
|
|
class RequestResource(Resource): |
9
|
|
|
|
10
|
|
|
"""Get all request""" |
11
|
|
|
@jwt_required |
12
|
|
|
def get(self): |
13
|
|
|
#to set in utils |
14
|
|
|
current_user = ["[email protected]"] |
15
|
|
|
res = [d for d in dtrequest if d["email"] in current_user] |
16
|
|
|
return res |
17
|
|
|
|
18
|
|
|
"""create new request""" |
19
|
|
|
def post(self): |
20
|
|
|
json_data = request.get_json(force=True) |
21
|
|
|
if not json_data: |
22
|
|
|
return {'message': 'No input data provided'}, 400 |
23
|
|
|
elif (len(json_data)!=len(dtrequest[0])): |
24
|
|
|
return "Please enter all details" |
25
|
|
|
dtrequest.append(json_data) |
26
|
|
|
return {"status":"success", "data": json_data }, 201 |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class Request(Resource): |
31
|
|
|
"""Get request by ID""" |
32
|
|
|
def get(self, req_id): |
33
|
|
|
for _request in dtrequest: |
34
|
|
|
if (req_id == _request['id']): |
35
|
|
|
return _request, 200 |
36
|
|
|
return "request not found", 404 |
37
|
|
|
|
38
|
|
|
"""Modify a request""" |
39
|
|
|
def put(self, req_id): |
40
|
|
|
json_data = request.get_json(force=True) |
41
|
|
|
for _request in dtrequest: |
42
|
|
|
if (req_id == _request['id']): |
43
|
|
|
_request.update(json_data) |
44
|
|
|
return {"status":"success", "data": json_data }, 201 |
45
|
|
|
return "Request not found", 404 |