-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
267 lines (189 loc) · 6.92 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
import os
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from models import setup_db, Actor, Movie
from config import pagination
from auth import requires_auth, AuthError
ROWS_PER_PAGE = pagination['example']
app = Flask(__name__)
db = SQLAlchemy(app)
def get_error_message(error, default_text):
try:
return error.description['message']
except:
return default_text
def paginate_results(request, selection):
page = request.args.get('page', 1, type=int)
start = (page - 1) * ROWS_PER_PAGE
end = start + ROWS_PER_PAGE
objects_formatted = [object_name.format() for object_name in selection]
return objects_formatted[start:end]
@app.route('/actors', methods=['GET'])
@requires_auth('read:actors')
def get_actors(payload):
"""Returns paginated actors object
"""
selection = Actor.query.all()
actors_paginated = paginate_results(request, selection)
if len(actors_paginated) == 0:
abort(404, {'message': 'no actors found in database.'})
return jsonify({
'success': True,
'actors': actors_paginated
})
@app.route('/actors', methods=['POST'])
@requires_auth('create:actors')
def insert_actors(payload):
body = request.get_json()
if not body:
abort(400, {'message': 'request does not contain a valid JSON body.'})
name = body.get('name', None)
age = body.get('age', None)
gender = body.get('gender', 'Other')
if not name:
abort(422, {'message': 'no name provided.'})
if not age:
abort(422, {'message': 'no age provided.'})
new_actor = (Actor(
name = name,
age = age,
gender = gender
))
new_actor.insert()
return jsonify({
'success': True,
'created': new_actor.id
})
@app.route('/actors/<actor_id>', methods=['PATCH'])
@requires_auth('edit:actors')
def edit_actors(payload, actor_id):
body = request.get_json()
if not actor_id:
abort(400, {'message': 'please append an actor id to the request url.'})
if not body:
abort(400, {'message': 'request does not contain a valid JSON body.'})
actor_to_update = Actor.query.filter(Actor.id == actor_id).one_or_none()
if not actor_to_update:
abort(404, {'message': 'Actor with id {} not found in database.'.format(actor_id)})
name = body.get('name', actor_to_update.name)
age = body.get('age', actor_to_update.age)
gender = body.get('gender', actor_to_update.gender)
actor_to_update.name = name
actor_to_update.age = age
actor_to_update.gender = gender
actor_to_update.update()
return jsonify({
'success': True,
'updated': actor_to_update.id,
'actor' : [actor_to_update.format()]
})
@app.route('/actors/<actor_id>', methods=['DELETE'])
@requires_auth('delete:actors')
def delete_actors(payload, actor_id):
if not actor_id:
abort(400, {'message': 'please append an actor id to the request url.'})
actor_to_delete = Actor.query.filter(Actor.id == actor_id).one_or_none()
if not actor_to_delete:
abort(404, {'message': 'Actor with id {} not found in database.'.format(actor_id)})
actor_to_delete.delete()
return jsonify({
'success': True,
'deleted': actor_id
})
@app.route('/movies', methods=['GET'])
@requires_auth('read:movies')
def get_movies(payload):
selection = Movie.query.all()
movies_paginated = paginate_results(request, selection)
if len(movies_paginated) == 0:
abort(404, {'message': 'no movies found in database.'})
return jsonify({
'success': True,
'movies': movies_paginated
})
@app.route('/movies', methods=['POST'])
@requires_auth('create:movies')
def insert_movies(payload):
body = request.get_json()
if not body:
abort(400, {'message': 'request does not contain a valid JSON body.'})
title = body.get('title', None)
release_date = body.get('release_date', None)
if not title:
abort(422, {'message': 'no title provided.'})
if not release_date:
abort(422, {'message': 'no "release_date" provided.'})
new_movie = (Movie(
title = title,
release_date = release_date
))
new_movie.insert()
return jsonify({
'success': True,
'created': new_movie.id
})
@app.route('/movies/<movie_id>', methods=['PATCH'])
@requires_auth('edit:movies')
def edit_movies(payload, movie_id):
body = request.get_json()
if not movie_id:
abort(400, {'message': 'please append an movie id to the request url.'})
if not body:
abort(400, {'message': 'request does not contain a valid JSON body.'})
movie_to_update = Movie.query.filter(Movie.id == movie_id).one_or_none()
if not movie_to_update:
abort(404, {'message': 'Movie with id {} not found in database.'.format(movie_id)})
title = body.get('title', movie_to_update.title)
release_date = body.get('release_date', movie_to_update.release_date)
movie_to_update.title = title
movie_to_update.release_date = release_date
movie_to_update.update()
return jsonify({
'success': True,
'edited': movie_to_update.id,
'movie' : [movie_to_update.format()]
})
@app.route('/movies/<movie_id>', methods=['DELETE'])
@requires_auth('delete:movies')
def delete_movies(payload, movie_id):
if not movie_id:
abort(400, {'message': 'please append an movie id to the request url.'})
movie_to_delete = Movie.query.filter(Movie.id == movie_id).one_or_none()
if not movie_to_delete:
abort(404, {'message': 'Movie with id {} not found in database.'.format(movie_id)})
movie_to_delete.delete()
return jsonify({
'success': True,
'deleted': movie_id
})
@app.errorhandler(422)
def unprocessable(error):
return jsonify({
"success": False,
"error": 422,
"message": get_error_message(error,"unprocessable")
}), 422
@app.errorhandler(400)
def bad_request(error):
return jsonify({
"success": False,
"error": 400,
"message": get_error_message(error, "bad request")
}), 400
@app.errorhandler(404)
def ressource_not_found(error):
return jsonify({
"success": False,
"error": 404,
"message": get_error_message(error, "resource not found")
}), 404
@app.errorhandler(AuthError)
def authentification_failed(AuthError):
return jsonify({
"success": False,
"error": AuthError.status_code,
"message": AuthError.error['description']
}), AuthError.status_code
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)