-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
326 lines (304 loc) · 9.46 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
import os
from flask import Flask, request, abort, jsonify, render_template
from flask_cors import CORS
from models import setup_db, Movie, Actor, ActionError, Assigning_actors_movies, Gender
from datetime import datetime
from auth import AuthError, requires_auth
from sqlalchemy.exc import IntegrityError
from endpoints_errors import verify_actor_submitted_info, no_actor_error, no_movie_error
from endpoints_errors import verify_movie_submitted_info, movie_release_date_error
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__)
setup_db(app)
CORS(app)
# CORS Headers
@app.after_request
def after_request(response):
response.headers.add(
'Access-Control-Allow-Headers',
'Content-Type, Authorization'
)
response.headers.add(
'Access-Control-Allow-Methods',
'GET, PATCH, POST, DELETE'
)
return response
@app.route('/')
def test():
return render_template('index.html')
@app.route('/actors')
@requires_auth('get:actors')
def get_actors(payload):
actors = Actor.query.order_by('id').all()
if len(actors) == 0:
raise no_actor_error
formatted_actors = [actor.format() for actor in actors]
return jsonify({
'success': True,
'actors': formatted_actors,
'total_actors': len(formatted_actors)
})
@app.route('/movies')
@requires_auth('get:movies')
def get_movies(payload):
movies = Movie.query.order_by('id').all()
if len(movies) == 0:
raise no_movie_error
formatted_movies = [movie.format() for movie in movies]
return jsonify({
'success': True,
'movies': formatted_movies,
'total_movies': len(formatted_movies)
})
@app.route('/movies/<int:movie_id>', methods=['DELETE'])
@requires_auth('delete:movies')
def delete_movie(payload, movie_id):
movie = Movie.query.get(movie_id)
if movie is None:
abort(404)
movie.delete()
return jsonify({
'success': True,
'deleted': movie_id
})
@app.route('/actors/<int:actor_id>', methods=['DELETE'])
@requires_auth('delete:actors')
def delete_actor(payload, actor_id):
actor = Actor.query.get(actor_id)
if actor is None:
abort(404)
actor.delete()
return jsonify({
'success': True,
'deleted': actor_id
})
@app.route('/actors', methods=['POST'])
@requires_auth('post:actors')
def create_actor(payload):
try:
body = request.get_json()
except:
abort(400)
name = body.get('name')
age = body.get('age')
gender = body.get('gender')
movies = body.get('movies', [])
movies = verify_actor_submitted_info(name, age, gender, movies)
if gender.lower() != 'male': gender = Gender.female
else: gender = Gender.male
new_actor = Actor(name, age, gender, movies)
attributes = vars(new_actor)
for couple in attributes.items():
if couple[1] is None:
raise ActionError({
'error': 'missing actor informations',
'description': '`%s` is required'%couple[0]
}, 422)
try:
new_actor.insert()
return jsonify({
'success': True,
'created': new_actor.id
})
except IntegrityError as e:
if 'unique constraint' in str(e.orig):
description = 'Duplicated actor name, actor `%s` alrady exists'%name
elif 'foreign key constraint' in str(e.orig):
description = 'Referenced movie[s] does not exist in the database, \
confim their ids before assign to actor'
raise ActionError({
'error': 'integrity error',
'description': description
}, 422)
except:
abort(422)
@app.route('/movies', methods=['POST'])
@requires_auth('post:movies')
def create_movie(payload):
try:
body = request.get_json()
except:
abort(400)
title = body.get('title')
release_date = body.get('release_date')
actors = body.get('actors', [])
actors = verify_movie_submitted_info(title, release_date, actors)
release_date += '00'
try:
dt_realease_date = datetime.strptime(release_date, '%d/%m/%Y %H:%M UTC%z')
except:
raise movie_release_date_error
new_movie = Movie(title, dt_realease_date, actors)
attributes = vars(new_movie)
for couple in attributes.items():
if couple[1] is None:
raise ActionError({
'error': 'missing movie informations',
'description': '`%s` is required'%couple[0]
}, 422)
try:
new_movie.insert()
return jsonify({
'success': True,
'created': new_movie.id
})
except IntegrityError as e:
if 'unique constraint' in str(e.orig):
description = 'Duplicated movie title, movie `%s` alrady exists'%title
elif 'foreign key constraint' in str(e.orig):
description = 'Referenced actor[s] does not exist in the database, \
confim their ids before assign to movie'
raise ActionError({
'error': 'integrity error',
'description': description
}, 422)
except:
abort(422)
@app.route('/actors/<int:actor_id>', methods=['PATCH'])
@requires_auth('patch:actors')
def update_actor(payload, actor_id):
actor = Actor.query.get(actor_id)
if actor is None:
abort(404)
try:
body = request.get_json()
except:
abort(400)
updated = False
name = body.get('name')
age = body.get('age')
gender = body.get('gender')
movies = body.get('movies')
actors_movies = verify_actor_submitted_info(name, age, gender, movies)
if name is not None and actor.name != name:
actor.name = name
updated = True
if age is not None and actor.age != age:
actor.age = age
updated = True
if gender is not None and actor.gender.name != gender:
actor.gender = gender
updated = True
if movies is not None and [actor_movie.movie_id for actor_movie in actor.movies] != movies:
actor.movies = actors_movies
updated = True
try:
if updated: actor.update()
return jsonify({
'success': True,
'updated': actor.format() if updated else 'unchanged'
})
except IntegrityError as e:
if 'unique constraint' in str(e.orig):
description = 'Duplicated actor name, actor `%s` alrady exists'%name
elif 'foreign key constraint' in str(e.orig):
description = 'Referenced movie[s] does not exist in the database, \
confim their ids before assign to actor'
raise ActionError({
'error': 'integrity error',
'description': description
}, 422)
except:
abort(422)
@app.route('/movies/<int:movie_id>', methods=['PATCH'])
@requires_auth('patch:movies')
def update_movie(payload, movie_id):
movie = Movie.query.get(movie_id)
if movie is None:
abort(404)
try:
body = request.get_json()
except:
abort(400)
updated = False
title = body.get('title')
release_date = body.get('release_date')
actors = body.get('actors')
actors_movies = verify_movie_submitted_info(title, release_date, actors)
if release_date is not None:
release_date += '00'
dt_realease_date = datetime.strptime(release_date, '%d/%m/%Y %H:%M UTC%z')
if dt_realease_date != movie.release_date:
movie.release_date = dt_realease_date
updated = True
if title is not None and title != movie.title:
movie.title = title
updated = True
if actors is not None and [actor_movie.actor_id for actor_movie in movie.actors] != actors:
movie.actors = actors_movies
updated = True
try:
if updated: movie.update()
return jsonify({
'success': True,
'updated': movie.format() if updated else 'unchanged'
})
except IntegrityError as e:
if 'unique constraint' in str(e.orig):
description = 'Duplicated movie title, movie `%s` alrady exists'%title
elif 'foreign key constraint' in str(e.orig):
description = 'Referenced actor[s] does not exist in the database, \
confim their ids before assign to movie'
raise ActionError({
'error': 'integrity error',
'description': description
}, 422)
except:
abort(422)
'''
Create error handlers for all expected errors
'''
@app.errorhandler(404)
def not_found(error):
return jsonify({
"success": False,
"status": 404,
"message": "resource not found"
}), 404
@app.errorhandler(422)
def unprocessable(error):
return jsonify({
"success": False,
"status": 422,
"message": "unprocessable"
}), 422
@app.errorhandler(400)
def bad_request(error):
return jsonify({
"success": False,
"status": 400,
"message": "bad request"
}), 400
@app.errorhandler(405)
def bad_request(error):
return jsonify({
"success": False,
"status": 405,
"message": "The method is not allowed for the requested URL"
}), 405
@app.errorhandler(500)
def server_error(error):
return jsonify({
"success": False,
"status": 500,
"message": "internal server error"
}), 500
@app.errorhandler(AuthError)
def handle_authError(error):
return jsonify({
'success': False,
'status': error.status_code,
'message': error.error
}), error.status_code
@app.errorhandler(ActionError)
def handle_actionError(error):
return jsonify({
'success': False,
'status': error.status_code,
'message': error.error
}), error.status_code
return app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)