-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
317 lines (290 loc) · 10.2 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import os
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from models import setup_db, Chocolate, Chocolatier
from auth import AuthError, requires_auth
def create_app(test_config=None):
# using the Flask class to create an instance of "app"
app = Flask(__name__)
setup_db(app)
CORS(app)
@app.after_request
def after_request(response):
response.headers.add(
"Access-Control-Allow-Headers", "Content-Type, Authorization, true" # noqa
)
response.headers.add(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS", # noqa
)
return response
# INDEX route--authentication should be requested for endpoints
@app.route("/")
def index():
greeting = "Welcome to The Chocolatier Electric!!!"
return greeting
# GET route for all chocolates, available to customers and managers
@app.route("/chocolates")
# @requires_auth("get:chocolates")
# def get_chocolates(payload):
def get_chocolates():
recipe = [c.format() for c in Chocolate.query.all()]
return jsonify({"success": True, "Chocolates": recipe}), 200
# POST route for all chocolates, available to customers and managers
@app.route("/chocolates", methods=["POST"])
@requires_auth("post:chocolates")
def create_chocolate(payload):
body = request.get_json()
try:
name = body.get("name", None)
chocolate_type = body.get("chocolate_type", None)
vendor = body.get("vendor", None)
vendor_id = body.get("vendor_id", None)
comments = body.get("comments", None)
try:
chocolate = Chocolate(
name=name,
chocolate_type=chocolate_type,
vendor=vendor,
vendor_id=vendor_id,
comments=comments,
)
chocolate.insert()
return (
jsonify(
{
"success": True,
"created": chocolate.id,
"name": chocolate.name,
"total_chocolates": len(Chocolate.query.all()),
}
),
200,
)
except Exception as e:
print("Exception is ", e)
abort(422)
except Exception as e:
print("Exception is ", e)
abort(422)
@app.route("/chocolates/search", methods=["POST"])
@requires_auth("post:chocolates")
def search(payload):
body = request.get_json()
searchTerm = body.get("searchTerm", "")
try:
results = Chocolate.query.filter(
Chocolate.name.ilike("%{}%".format(searchTerm))
).all()
chocolate = [result.format() for result in results]
return jsonify({"success": True, "chocolates": chocolate}), 200
except Exception as e:
print("Exception is ", e)
abort(404)
# PATCH route for all chocolates, available to customers and managers
@app.route("/chocolates/<id>", methods=["PATCH"])
@requires_auth("patch:chocolates")
def edit_chocolates(payload, id):
body = request.get_json()
name = body.get("name", None)
chocolate_type = body.get("chocolate_type", None)
vendor = body.get("vendor", None)
vendor_id = body.get("vendor_id", None)
comments = body.get("comments", None)
try:
chocolate = Chocolate.query.filter(Chocolate.id == id).one_or_none()
if chocolate is None:
abort(404)
chocolate.name = name
chocolate.chocolate_type = chocolate_type
chocolate.vendor = vendor
chocolate.vendor_id = vendor_id
chocolate.comments = comments
chocolate.update()
return (
jsonify({"success": True, "chocolate": [chocolate.format()]}),
200,
) # noqa
except Exception as e:
print("Exception is ", e)
abort(422)
# DELETE route for all chocolates, available to managers only
@app.route("/chocolates/<id>", methods=["DELETE"])
@requires_auth("delete:chocolates")
def delete_chocolates(payload, id):
try:
chocolate = Chocolate.query.filter(Chocolate.id == id).one_or_none()
if chocolate is None:
abort(404)
chocolate.delete()
return jsonify({"success": True, "deleted": chocolate.id}), 200
except Exception as e:
abort(422)
# GET route for chocolatiers, available to customers and managers
@app.route("/chocolatiers")
@requires_auth("get:chocolatiers")
def get_chocolatiers(payload):
recipe = [c.format() for c in Chocolatier.query.all()]
return jsonify({"success": True, "Chocolatiers": recipe}), 200
# POST route for chocolatiers, available to managers only
@app.route("/chocolatiers", methods=["POST"])
@requires_auth("post:chocolatiers")
def create_chocolatier(payload):
body = request.get_json()
try:
name = body.get("name", None)
address = body.get("address", None)
website = body.get("website", None)
facebook = body.get("facebook", None)
phone = body.get("phone", None)
chef = body.get("chef", None)
comments = body.get("comments", None)
try:
chocolatier = Chocolatier(
name=name,
address=address,
website=website,
facebook=facebook,
phone=phone,
chef=chef,
comments=comments,
)
chocolatier.insert()
return (
jsonify(
{
"success": True,
"created": chocolatier.id,
"name": chocolatier.name,
"total_chocolatiers": len(Chocolatier.query.all()),
}
),
200,
)
except Exception as e:
print("Exception is ", e)
abort(422)
except Exception as e:
print("Exception is ", e)
abort(422)
# PATCH route for chocolatiers, available to managers only
@app.route("/chocolatiers/<id>", methods=["PATCH"])
@requires_auth("patch:chocolatiers")
def edit_chocolatiers(payload, id):
body = request.get_json()
name = body.get("name", None)
address = body.get("address", None)
website = body.get("website", None)
facebook = body.get("facebook", None)
phone = body.get("phone", None)
comments = body.get("comments", None)
try:
chocolatier = Chocolatier.query.filter(Chocolatier.id == id).one_or_none()
if chocolatier is None:
abort(404)
chocolatier.name = name
chocolatier.address = address
chocolatier.website = website
chocolatier.facebook = facebook
chocolatier.phone = phone
chocolatier.comments = comments
chocolatier.update()
return (
jsonify(
{"success": True, "chocolatier": [chocolatier.format()]}
), # noqa
200,
)
except Exception as e:
print("Exception is ", e)
abort(422)
# DELETE route for chocolatiers, available to managers only
@app.route("/chocolatiers/<id>", methods=["DELETE"])
@requires_auth("delete:chocolatiers")
def delete_chocolatiers(payload, id):
try:
chocolatier = Chocolatier.query.filter(Chocolatier.id == id).one_or_none()
if chocolatier is None:
abort(404)
chocolatier.delete()
return jsonify({"success": True, "deleted": chocolatier.id}), 200
except Exception as e:
abort(422)
# ERROR HANDLING
@app.errorhandler(400)
def bad_request(error):
return (
jsonify(
{
"success": False,
"error": 400,
"message": "Bad Request.",
}
),
400,
)
@app.errorhandler(404)
def not_found(error):
return (
jsonify(
{
"success": False,
"error": 404,
"message": "Resource Not Found. Sorry!",
}
),
404,
)
@app.errorhandler(405)
def not_allowed(error):
return (
jsonify(
{
"success": False,
"error": 405,
"message": "That method is not allowed for this endpoint.",
}
),
405,
)
@app.errorhandler(422)
def unprocessable(error):
return (
jsonify(
{
"success": False,
"error": 422,
"message": "Unprocessable.",
}
),
422,
)
@app.errorhandler(500)
def server_error(error):
return (
jsonify(
{
"success": False,
"error": 500,
"message": "Server Error",
}
),
500,
)
@app.errorhandler(AuthError)
def auth_error(error):
return (
jsonify(
{
"success": False,
"error": 401,
"message": "You don't have permission for this endpoint.",
}
),
401,
)
return app
app = create_app()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=True)