-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
238 lines (203 loc) · 7.06 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
import os
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from auth import requires_auth, AuthError
import re
import sys
import json
from models import Actor, Movie, setup_db
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__)
CORS(app)
db = setup_db(app)
@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, POST, PATCH, DELETE, OPTIONS')
return response
@app.route('/')
def home():
return jsonify('Hello World')
@app.route('/actors')
def get_actors():
actors = Actor.query.all()
actors_formatted = [actor.format() for actor in actors]
if len(actors) > 0:
return jsonify({
'success': True,
'actors': actors_formatted
})
else:
return jsonify({
'success': True,
'actors': 'Actors do not available'
})
@app.route('/movies')
def get_movies():
movies = Movie.query.all()
movies_formatted = [movie.format() for movie in movies]
if len(movies) > 0:
return jsonify({
'success': True,
'movies': movies_formatted
})
else:
return jsonify({
'success': True,
'movies': 'movies do not available'
})
@app.route('/actors', methods = ['POST'])
@requires_auth('post:actors')
def create_actor(jwt):
error = False
data = request.data
data_dictionary = json.loads(data)
try:
name = data_dictionary['name']
age = data_dictionary['age']
gender = data_dictionary['gender']
movie_id = int(data_dictionary['movie_id'])
actor = Actor(name = name, age = age, gender = gender, movie_id = movie_id)
actor.insert()
except:
error = True
Actor.rollback()
print(sys.exc_info())
if error:
abort(422)
else:
return jsonify({
'success': True,
'actor': actor.format()
})
@app.route('/movies', methods = ['POST'])
@requires_auth('post:movies')
def create_movie(jwt):
error = False
data = request.data
data_dictionary = json.loads(data)
release_date = data_dictionary['release_date']
regex_release_date = '^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$'
match = re.search(regex_release_date, release_date)
if not match:
return jsonify({
'succes': False,
'error': 422,
'message': 'Please insert a valid date in this format dd/mm/yyy'
}),422
try:
title = data_dictionary['title']
movie = Movie(title = title, release_date = release_date)
movie.insert()
except:
error = True
Movie.rollback()
print(sys.exc_info())
if error:
abort(422)
else:
return jsonify({
'success': True,
'movie': movie.format()
})
@app.route('/actors/<actor_id>', methods = ['DELETE'])
@requires_auth('delete:actors')
def delete_actor(jwt, actor_id):
error = False
try:
actor = Actor.query.filter(Actor.id == int(actor_id)).first()
actor.delete()
except:
error = True
print(sys.exc_info())
if error:
abort(404)
else:
return jsonify({
'succes': True,
'id_actor': actor.id
})
@app.route('/movies/<movie_id>', methods = ['DELETE'])
@requires_auth('delete:movies')
def delete_movie(jwt, movie_id):
error = False
try:
movie = Movie.query.filter(Movie.id == int(movie_id)).first()
movie.delete()
except:
error = True
print(sys.exc_info())
if error:
abort(404)
else:
return jsonify({
'succes': True,
'id_movie': movie.id
})
@app.route('/actors/<actor_id>', methods = ['PATCH'])
@requires_auth('update:actors')
def update_actor(jwt, actor_id):
error = False
data = request.data
data_dictionary = json.loads(data)
try:
actor = Actor.query.filter(Actor.id == int(actor_id)).first()
actor.name = data_dictionary['name']
actor.gender = data_dictionary['gender']
actor.movie_id = data_dictionary['movie_id']
actor.update()
except:
error = True
print(sys.exc_info())
if error:
abort(404)
else:
return jsonify({
'succes': True,
'id_actor': actor.id
})
@app.route('/movies/<movie_id>', methods = ['PATCH'])
@requires_auth('update:movies')
def update_movie(jwt, movie_id):
error = False
data = request.data
data_dictionary = json.loads(data)
try:
movie = Movie.query.filter(Movie.id == int(movie_id)).first()
movie.title = data_dictionary['title']
movie.release_date = data_dictionary['release_date']
movie.update()
except:
error = True
print(sys.exc_info())
if error:
abort(404)
else:
return jsonify({
'succes': True,
'id_movie': movie.id
})
@app.errorhandler(404)
def resource_not_found(error):
return jsonify({
'success': False,
'error': 404,
'message': 'Resource Not Found',
}), 404
@app.errorhandler(422)
def unprocessable(error):
return jsonify({
'success': False,
'error': 422,
'message': 'Unprocessable',
}), 422
@app.errorhandler(AuthError)
def auth_error(error):
handler = error.format()
return jsonify(handler[0]), handler[1]
return app
APP = create_app()
if __name__ == '__main__':
APP.run(host='0.0.0.0', port=os.getenv('PORT',8080), debug=True)