-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.py
73 lines (60 loc) · 2.32 KB
/
load.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Author: Ren Demeis-Ortiz
# Course: CS493 Cloud Application Development (Final Project)
# Description: Load Routes
#
# Sources: OSU CS493 Module 4 Exploration
# https://canvas.oregonstate.edu/courses/1870359/modules/items/22099651
from flask import abort, request, Blueprint
import json
import constants as c
import datastoreHelpers as ds
import methodHelpers as m
bp = Blueprint('load', __name__, url_prefix='/loads')
@bp.route('', methods=['POST', 'GET'])
def loads_get_post():
# Validate correct accept type
if not request.accept_mimetypes.accept_json:
abort(406)
if request.method == 'POST':
# Validate the Number of Attributes Received
# if incorrect respond with 400 Bad Request
content = request.get_json()
if len(content) != 3:
abort(400, {"message": c.edit_errors["missing_attribute"]})
content["carrier"] = None
new_load = ds.create_entity(c.loads, content)
new_load["id"] = new_load.key.id
new_load["self"] = ds.get_self_url(request, c.loads,
new_load["id"])
return new_load, 201
elif request.method == 'GET':
results = ds.get_all(c.loads, request)
for e in results["loads"]:
e["self"] = ds.get_self_url(request, c.loads, e["id"])
e = ds.repackage_carrier(e, request)
return json.dumps(results), 200
else:
abort(405)
@bp.route('/<id>', methods=['GET', 'DELETE', 'PATCH', 'PUT'])
def load_get_delete_patch_put(id):
load = ds.get_entity(c.loads, int(id))
if not load:
abort(404, {"message": c.load_errors["invalid_load_id"]})
elif request.method == 'GET':
# Validate correct accept type
if not request.accept_mimetypes.accept_json:
abort(406)
load = ds.add_id_self(load, request, c.loads)
load = ds.repackage_carrier(load, request)
return json.dumps(load), 200
elif request.method == 'DELETE':
ds.delete_entity(c.loads, int(id))
return '', 204
elif request.method == 'PATCH':
edited_load = m.run_patch(request, c.loads, id)
return edited_load, 200
elif request.method == 'PUT':
edited_load = m.run_put(request, c.loads, id)
return edited_load, 200
else:
abort(405)