-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
332 lines (301 loc) · 10.3 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
import os
import sys
from flask import Flask, request, abort, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from models import Actor, Movie, db_drop_and_create_all, setup_db, db
from auth import AuthError, requires_auth
class Already_Exists_Error(Exception):
def __init__(self, error, status_code):
self.error = error
self.status_code = status_code
ITEMS_PER_PAGE = 10
# defining a function to paginate items for a page
def paginate_items(request, selection):
page = request.args.get('page', 1, type=int)
start = (page - 1) * ITEMS_PER_PAGE
end = start + ITEMS_PER_PAGE
items = [item.format() for item in selection]
return items[start:end]
# create and configure the app
def create_app(test_config=None):
app = Flask(__name__)
setup_db(app)
CORS(app, resources={r"/api/*": {"origins": "*"}})
'''
uncomment the following line to initialize the database
!! NOTE drops all tables and start the database from scratch
should be run the first time running the app
'''
#db_drop_and_create_all(db)
@app.after_request
def after_request(response):
response.headers.add(
'Access-Control-Allow-Headers',
'Content-Type, Authorization')
response.headers.add(
'Access-Control-Allow-Headers',
'GET, POST, PATCH, DELETE, OPTIONS')
return response
# login
@app.route("/login")
def login():
return render_template("login.html")
@app.route("/login_results")
def login_results():
return render_template("login_results.html")
# get all actors
@app.route("/actors")
@requires_auth("get:actors")
def get_actors(payload):
# get all actors and return them formatted
selection = Actor.query.order_by(Actor.id).all()
actors = paginate_items(request, selection)
actors_count = Actor.query.count()
return jsonify({
"success": True,
"actors": actors,
"actors_count": actors_count
})
# get all movies
@app.route("/movies")
@requires_auth("get:movies")
def get_movies(payload):
# get all movies and return them formatted
selection = Movie.query.order_by(Movie.id).all()
movies = paginate_items(request, selection)
movies_count = Movie.query.count()
return jsonify({
"success": True,
"movies": movies,
"movies_count": movies_count
})
# post a new actor
@app.route("/actors", methods=['POST'])
@requires_auth("post:actors")
def add_actor(payload):
# get the request data
data = request.get_json()
if data is None:
abort(400)
try:
name = data['name']
age = data.get('age')
gender = data.get('gender')
# check if actor already exists in database
the_actor = Actor.query.filter(Actor.name == name).one_or_none()
if the_actor is not None:
raise Already_Exists_Error({
"code": "Conflict",
"description": "actor already exists in database"
}, 409)
# insert the actor to the database
actor = Actor(name=name, age=age, gender=gender)
actor.insert()
# return success with the added actor data
the_actor = Actor.query.filter(
Actor.name == actor.name).one_or_none()
actor_id = the_actor.id
return jsonify({
"success": True,
"created": actor_id
})
except Exception as e:
print(e)
if isinstance(e, Already_Exists_Error):
raise Already_Exists_Error({
"code": "Conflict",
"description": "actor already exists in database"
}, 409)
else:
abort(422)
# post a new movie
@app.route('/movies', methods=['POST'])
@requires_auth("post:movies")
def add_movie(payload):
# get the request data
data = request.get_json()
if data is None:
abort(400)
try:
title = data['title']
release_date = data.get('release_date')
the_movie = Movie.query.filter(Movie.title == title).one_or_none()
# check if movie already exists in database
if the_movie is not None:
raise Already_Exists_Error({
"code": "Conflict",
"description": "movie already exists in database"
}, 409)
# insert the movie to the database
movie = Movie(title=title, release_date=release_date)
movie.insert()
# return success with the added movie data
the_movie = Movie.query.filter(Movie.title == title).one_or_none()
movie_id = the_movie.id
return jsonify({
"success": True,
"created": movie_id
})
except Exception as e:
print(e)
if isinstance(e, Already_Exists_Error):
raise Already_Exists_Error({
"code": "Conflict",
"description": "movie already exists in database"
}, 409)
else:
abort(422)
# update an existing actor data
@app.route('/actors/<int:actor_id>', methods=['PATCH'])
@requires_auth("patch:actors")
def update_actor(payload, actor_id):
# check if the requested actor exists
actor = Actor.query.filter(Actor.id == actor_id).one_or_none()
if actor is None:
abort(404)
# get the request data
data = request.get_json()
if data is None:
abort(400)
try:
name = data.get('name')
age = data.get('age')
gender = data.get('gender')
# update the actor information
if name is not None:
actor.name = name
if age is not None:
actor.age = age
if gender is not None:
actor.gender = gender
actor.update()
updated_actor = Actor.query.filter(
Actor.id == actor_id).one_or_none()
return jsonify({
"success": True,
"actor": updated_actor.format()
})
except Exception as e:
print(e)
abort(422)
# update an existing movie data
@app.route("/movies/<int:movie_id>", methods=['PATCH'])
@requires_auth("patch:movies")
def update_movie(payload, movie_id):
# get the request data
data = request.get_json()
if data is None:
abort(400)
# check if the requested movie exists
movie = Movie.query.filter(Movie.id == movie_id).one_or_none()
if movie is None:
abort(404)
try:
title = data.get('title')
release_date = data.get('release_date')
# update the movie information
if title is not None:
movie.title = title
if release_date is not None:
movie.release_date = release_date
movie.update()
updated_movie = Movie.query.filter(
Movie.id == movie_id).one_or_none()
return jsonify({
"success": True,
"movie": updated_movie.format()
})
except Exception as e:
print(e)
abort(422)
# delete an actor
@app.route("/actors/<int:actor_id>", methods=['DELETE'])
@requires_auth("delete:actors")
def delete_actor(payload, actor_id):
# check if the actor exists in the database
actor = Actor.query.filter(Actor.id == actor_id).one_or_none()
if actor is None:
abort(404)
try:
# delete actor from database
actor.delete()
return jsonify({
"success": True,
"deleted": actor.id
})
except Exception as e:
print(e)
abort(e)
# delete a movie
@app.route("/movies/<int:movie_id>", methods=['DELETE'])
@requires_auth("delete:movies")
def delete_movie(payload, movie_id):
# check if the movie exists in the database
movie = Movie.query.filter(Movie.id == movie_id).one_or_none()
if movie is None:
abort(404)
try:
# delete movie from database
movie.delete()
return jsonify({
"success": True,
"deleted": movie_id
})
except Exception as e:
print(e)
abort(e)
# Error Handling
@app.errorhandler(422)
def unprocessable(error):
return jsonify({
"success": False,
"error": 422,
"message": "unprocessable"
}), 422
@app.errorhandler(400)
def bad_request(error):
return jsonify({
"success": False,
"error": 400,
"message": "bad request"
}), 400
@app.errorhandler(405)
def method_not_allowed(error):
return jsonify({
"success": False,
"error": 405,
"message": "method not allowed"
}), 405
@app.errorhandler(500)
def internal_server_error(error):
return jsonify({
"success": False,
"error": 500,
"message": "internal server error"
}), 500
@app.errorhandler(404)
def not_found(error):
return jsonify({
"success": False,
"error": 404,
"message": "resource not found"
}), 404
@app.errorhandler(AuthError)
def Auth_error(error):
return jsonify({
"success": False,
"error": error.status_code,
"message": error.error
}), error.status_code
@app.errorhandler(Already_Exists_Error)
def Already_Exists_error(error):
return jsonify({
"success": False,
"error": 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)