-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
344 lines (290 loc) · 9.24 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from flask import Flask, request, abort, jsonify
from flask_cors import CORS
from models import *
from auth import AuthError, requires_auth
from datetime import datetime
def create_app(test_config=None):
# create and configure the 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, Autorization')
response.headers.add(
'Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS')
return response
# GET all movies
@app.route('/movies')
@requires_auth('get:movies')
def movies_retrieve(jwt):
q_movie = Movie.query.all()
movies = [movie.format() for movie in q_movie]
return jsonify(
{
'success': True,
'movies': movies,
})
# GET all actors
@app.route('/actors')
@requires_auth('get:actors')
def actors_retrieve(jwt):
q_actors = Actor.query.all()
actors = [actor.format() for actor in q_actors]
return jsonify(
{
'success': True,
'actors': actors,
})
# Create a new movie
@app.route('/movies', methods=['POST'])
@requires_auth('post:movies')
def movies_create(jwt):
# if body exists
try:
body = request.get_json()
movie_title = body.get('title', None)
movie_release_date = body.get('release_date', None)
except KeyError:
abort(422)
# if missing some required data
movie_parameters = [movie_title, movie_release_date]
if any(parameter is None for parameter in movie_parameters):
abort(400)
try:
# create new movie
movie_release_date = datetime.strptime(
movie_release_date, "%Y-%m-%d")
new_movie = Movie(title=movie_title,
release_date=movie_release_date)
new_movie.insert()
movie_id = new_movie.id
return jsonify(
{
'success': True,
'movie_id': movie_id,
})
except KeyError:
abort(422)
# Create a new actor
@app.route('/actors', methods=['POST'])
@requires_auth('post:actors')
def actors_create(jwt):
# if body exists
try:
body = request.get_json()
actor_name = body.get('name', None)
actor_age = body.get('age', None)
actor_gender = body.get('gender', None)
except KeyError:
abort(422)
# check if missing some required data
actor_parameters = [actor_name, actor_gender, actor_age]
if any(parameter is None for parameter in actor_parameters):
abort(400)
try:
# create new actor
new_actor = Actor(name=actor_name, age=actor_age,
gender=actor_gender)
new_actor.insert()
actor_id = new_actor.id
return jsonify(
{
'success': True,
'actor_id': actor_id,
})
except KeyError:
abort(422)
# Create a movie_actor relation
@app.route('/movies/actors', methods=['POST'])
@requires_auth('post:movies_actors')
def movie_actor_create(jwt):
# if body exists
try:
body = request.get_json()
actor_id = body.get('actor_id', None)
movie_id = body.get('movie_id', None)
except KeyError:
abort(422)
# check if missing some required data
if any(parameter is None for parameter in [actor_id, movie_id]):
abort(400)
# check if movie exists
movie = Movie.query.get(movie_id)
if not movie:
abort(404)
# check if actor exist
actor = Actor.query.get(actor_id)
if not actor:
abort(404)
try:
# add actor to movie
movie.actor_movie = [actor]
movie.update()
return jsonify(
{
'success': True,
'actor_id': actor_id,
'movie_id': movie_id
})
except KeyError:
abort(422)
# Update movie by ID
@app.route('/movies/<int:movie_id>', methods=['PATCH'])
@requires_auth('patch:movies')
def movie_patch(jwt, movie_id):
# if body exists
body = request.get_json()
if body:
movie_title = body.get('title', None)
movie_release_date = body.get('release_date', None)
else:
abort(422)
movie = Movie.query.filter(Movie.id == movie_id).one_or_none()
if movie is None:
abort(404)
try:
# check parameter to update
if movie_title is not None:
movie.title = movie_title
if movie_release_date is not None:
movie_release_date = datetime.strptime(
movie_release_date, "%Y-%m-%d")
movie.release_date = movie_release_date
movie.update()
return jsonify({
'success': True,
'movie_id': movie_id,
})
except KeyError:
abort(422)
# Update actor by ID
@app.route('/actors/<int:actor_id>', methods=['PATCH'])
@requires_auth('patch:actors')
def actor_patch(jwt, actor_id):
# if body exists
body = request.get_json()
if body:
actor_name = body.get('name', None)
actor_age = body.get('age', None)
actor_gender = body.get('gender', None)
else:
abort(422)
actor = Actor.query.filter(Actor.id == actor_id).one_or_none()
if actor is None:
abort(404)
try:
# check parameter to update
if actor_name is not None:
actor.name = actor_name
if actor_age is not None:
actor.age = actor_age
if actor_gender is not None:
actor.gender = actor_gender
actor.update()
return jsonify({
'success': True,
'actor_id': actor_id,
})
except KeyError:
abort(422)
# Delete movie by ID
@app.route('/movies/<int:movie_id>', methods=['DELETE'])
@requires_auth('delete:movies')
def movies_delete(jwt, movie_id):
movie = Movie.query.filter(Movie.id == movie_id).one_or_none()
if movie is None:
abort(404)
try:
movie.delete()
return jsonify({
'success': True,
'movie_id': movie_id,
})
except KeyError:
abort(422)
# Delete actor by ID
@app.route('/actors/<int:actor_id>', methods=['DELETE'])
@requires_auth('delete:actors')
def actors_delete(jwt, actor_id):
actor = Actor.query.filter(Actor.id == actor_id).one_or_none()
if actor is None:
abort(404)
try:
actor.delete()
return jsonify({
'success': True,
'actor_id': actor_id,
})
except KeyError:
abort(422)
# Error Handling
@app.errorhandler(400)
def bad_request(error):
return jsonify(
{
"success": False,
"error": 400,
"message": "Bad request"
}), 400
@app.errorhandler(401)
def unauthorized(error):
return jsonify(
{
"success": False,
"error": 401,
"message": "Unauthorized"
}), 401
@app.errorhandler(403)
def forbidden(error):
return jsonify(
{
"success": False,
"error": 403,
"message": "Forbidden"
}), 403
@app.errorhandler(404)
def not_found(error):
return jsonify(
{
"success": False,
"error": 404,
"message": "Resource not found"
}), 404
@app.errorhandler(405)
def not_allowed(error):
return jsonify(
{
"success": False,
"error": 405,
"message": "Method not allowed"
}), 405
@app.errorhandler(422)
def unprocessable(error):
return jsonify(
{
"success": False,
"error": 422,
"message": "Unprocessable entity"
}), 422
@app.errorhandler(500)
def server_error(error):
return jsonify(
{
"success": False,
"error": 500,
"message": "Internal server error"
}), 500
# AuthError error handler
@app.errorhandler(AuthError)
def authentification_failed(AuthError):
return jsonify({
"success": False,
"error": AuthError.status_code,
"message": AuthError.error
}), 401
return app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)