-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
258 lines (200 loc) · 6.4 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
import os
from flask import Flask, request, abort, jsonify
from models import setup_db, Movies, Actors
from flask_cors import CORS
from auth.auth import requires_auth, AuthError
def create_app(test_config=None):
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')
response.headers.add(
'Access-Control-Allow-Methods',
'GET,PUT,POST,DELETE,OPTIONS')
return response
@app.route('/')
def get_greeting():
excited = os.environ.get('EXCITED')
print(excited)
greeting = "Hello"
if excited == "true":
greeting = greeting + "!!!!!"
return greeting
@app.route('/coolkids')
def be_cool():
return "Be cool, man, be coooool! You're almost a FSND grad!"
@app.route('/actors', methods=['GET'])
@requires_auth('GET:actors')
def retrieve_actors(payload):
all_actors = Actors.query.order_by(Actors.id).all()
actors = [actor.format() for actor in all_actors]
if len(all_actors) == 0:
abort(404)
return jsonify({
'success': True,
'actors': actors
})
@app.route('/movies', methods=['GET'])
@requires_auth('GET:movies')
def retrieve_movies(payload):
print("yesy")
all_movie = Movies.query.order_by(Movies.id).all()
movies =[movie.format() for movie in all_movie]
if len(all_movie) == 0:
abort(404)
return jsonify({
'success': True,
'movies': movies
})
@app.route('/actors', methods=['POST'])
@requires_auth('POST:actors')
def create_actors(payload):
body = request.get_json()
new_name = body.get('name', None)
new_age = body.get('age', None)
new_gender = body.get('gender', None)
try:
if not body:
abort(400)
actor = Actors(name=new_name, age=new_age, gender=new_gender)
actor.insert()
return jsonify({
'success': True,
'created': actor.id
})
except Exception as e:
print(e)
abort(422)
@app.route('/movies', methods=['POST'])
@requires_auth('POST:movies')
def create_movies(payload):
body = request.get_json()
new_title = body.get('title', None)
new_date = body.get('release_date', None)
try:
if not body:
abort(400)
movie = Movies(title=new_title, release_date=new_date)
movie.insert()
return jsonify({
'success': True,
'created': movie.id
})
except Exception as e:
print(e)
abort(422)
@app.route('/movies/<int:Movies_id>', methods=['DELETE'])
@requires_auth('DELETE:movies')
def delete_movies(payload, Movies_id):
try:
movie = Movies.query.filter(
Movies.id == Movies_id).one_or_none()
if Movies is None:
abort(404)
movie.delete()
return jsonify({
'success': True,
'deleted': Movies_id
})
except Exception as e:
print(e)
abort(422)
@app.route('/actors/<int:Actors_id>', methods=['DELETE'])
@requires_auth('DELETE:actors')
def delete_actors(payload, Actors_id):
try:
actor = Actors.query.filter(
Actors.id == Actors_id).one_or_none()
if actor is None:
abort(404)
actor.delete()
return jsonify({
'success': True,
'deleted': Actors_id
})
except Exception as e:
print(e)
abort(422)
@app.route('/actors/<int:Actors_id>', methods=['PATCH'])
@requires_auth('PATCH:actors')
def update_actor(payload, Actors_id):
body = request.get_json()
try:
actor = Actors.query.filter(Actors.id == Actors_id).one_or_none()
if actor is None:
abort(404)
if 'name' in body and 'age' in body:
actor.name = body.get('name')
actor.age = body.get('age')
actor.update()
return jsonify({
'success': True,
})
except BaseException:
abort(400)
@app.route('/movies/<int:Movies_id>', methods=['PATCH'])
@requires_auth('PATCH:movies')
def update_movie(payload, Movies_id):
body = request.get_json()
try:
movie = Movies.query.filter(Movies.id == Movies_id).one_or_none()
if movie is None:
abort(404)
if 'title' in body and 'release_date' in body:
movie.title = body.get('title')
movie.release_date = body.get('release_date')
movie.update()
return jsonify({
'success': True,
})
except BaseException:
abort(400)
# Error Handling
@app.errorhandler(422)
def unprocessable(error):
return jsonify({
"success": False,
"error": 422,
"message": "unprocessable"
}), 422
@app.errorhandler(404)
def not_found(error):
return jsonify({
"success": False,
"error": 404,
"message": "resource not found"
}), 404
@app.errorhandler(500)
def internal_server_error(error):
return jsonify({
"success": False,
"error": 500,
"message": 'Internal Server Error'
}), 500
@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": 'Unathorized'
}), 401
@app.errorhandler(AuthError)
def process_AuthError(error):
response = jsonify(error.error)
response.status_code = error.status_code
return response
return app
app = create_app()
if __name__ == '__main__':
app.run()