-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
455 lines (420 loc) · 15.8 KB
/
service.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from hashlib import sha256
from uuid import uuid4
import random
import string
import json
import math
import time
from datetime import datetime, timedelta
from conn_db import *
from flask import Flask \
, render_template \
, redirect \
, url_for \
, request
app = Flask(__name__)
@app.route('/', methods=['GET'])
def start():
return redirect(url_for('register_form'))
@app.route('/register', methods=['GET'])
def register_form():
return render_template('register.html')
@app.route('/register', methods=['POST'])
def register():
name = request.form['name']
if not name:
return render_template('fail_registration.html', reason='Empty login not allowed.')
password = request.form['password']
if len(password) < 6:
return render_template('fail_registration.html', reason='Password is too short')
email = request.form['email']
phone = request.form['phone']
if user_exist(phone):
return render_template('fail_registration.html', reason='User already exists.'.format(phone))
insert_user(name, password, phone, email)
return render_template('registration_ok.html', name=request.form['name'])
@app.route('/films/', methods=['GET'])
def get_films():
try:
per_page = int(request.args.get('per_page', 20))
if per_page < 20 or per_page > 100:
raise Exception()
page = int(request.args.get('page', 0))
lendb = len_db()
if lendb % per_page > 0:
b = 1
else:
b = 0
if page < 0 or page > lendb / per_page + b:
raise Exception()
except:
return '', 400
items = []
items = films_from_db(page, per_page)
return json.dumps({
'items': items,
'per_page': per_page,
'page': page,
'page_count': math.ceil(lendb / per_page + b)
}, indent=4),200, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/me/', methods=['GET'])
def me():
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
row = get_me(access_token)
return json.dumps({
'name': row.name.decode(encoding='ISO-8859-1', errors='strict'),
'email': row.mail,
'phone': row.phone
}, indent=4), 200, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/films/<id>', methods=['GET'])
def get_film(id):
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
row = film_by_id(id)
print row
if row == 0:
return '', 404
else:
time.struct_time=row.duration
return json.dumps({
'id': row.id,
'title': row.title.decode(encoding='ISO-8859-1', errors='strict'),
'duration': time.struct_time,
'year': row.year,
'genre': row.genre.decode(encoding='ISO-8859-1', errors='strict'),
'kinopoisk_rating': float(row.kinopoisk_rating)
}, indent=4), 200, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/directors/', methods=['GET'])
def get_directors():
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
try:
per_page = int(request.args.get('per_page', 20))
if per_page < 20 or per_page > 100:
raise Exception()
page = int(request.args.get('page', 0))
lendb = len_db_dirs()
print lendb
if lendb % per_page > 0:
b = 1
else:
b = 0
if page < 0 or page > lendb / per_page + b:
raise Exception()
except:
return '', 400
items = []
items = directors_from_db(page, per_page)
return json.dumps({
'items': items,
'per_page': per_page,
'page': page,
'page_count': math.ceil(lendb / per_page + b)
}, indent=4), 200, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/directors/<id>', methods=['GET'])
def get_director(id):
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
row = director_by_id(id)
if row == 0:
return '', 404
else:
return json.dumps({
'id_director': row.id_director,
'name': row.name.decode(encoding='ISO-8859-1', errors='strict'),
'birthday': row.birthday,
'country of birth': row.country
}, indent=4), 200, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/films/<id>', methods=['POST'])
def post_film(id):
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
try:
id_director = request.args.get('id_director')
title = request.args.get('title')
duration = request.args.get('duration')
year = request.args.get('year')
genre = request.args.get('genre')
kinopoisk_rating = request.args.get('kinopoisk_rating')
if id_director is None or year is None or kinopoisk_rating is None:
raise Exception()
if duration is None or title is None or genre is None:
raise Exception()
except:
return '', 400
i = insert_film(id, id_director, title, duration, year, genre, kinopoisk_rating)
if i == 0:
return '', 400
s = '/films/{'+id+'}'
return json.dumps({
'Location': s
}), 201, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/directors/<id>', methods=['POST'])
def post_director(id):
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
try:
name = request.args.get('name')
birthday = request.args.get('birthday')
country = request.args.get('country')
if name is None or birthday is None or country is None:
raise Exception()
except:
return '', 400
i = insert_director(id, name, birthday, country)
if i == 0:
return '', 400
s = '/directors/{'+id+'}'
return json.dumps({
'Location': s
}), 201, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/directors/<id>', methods=['PUT'])
def put_director(id):
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
row = director_by_id(id)
if row == 0:
return '', 404
try:
name = request.args.get('name')
birthday = request.args.get('birthday')
country = request.args.get('country')
if name is None and birthday is None and country is None:
raise Exception()
except:
return '', 400
if name is not None:
i = update_director(id, 'name', name)
if birthday is not None:
i = update_director(id, 'birthday', birthday)
if country is not None:
i = update_director(id, 'country', country)
if i == 0:
return '', 400
s = '/directors/{'+id+'}'
return json.dumps({
'Location': s
}), 200, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/films/<id>', methods=['PUT'])
def put_film(id):
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
row = film_by_id(id)
if row == 0:
return '', 404
try:
id_director = request.args.get('id_director')
title = request.args.get('title')
duration = request.args.get('duration')
year = request.args.get('year')
genre = request.args.get('genre')
kinopoisk_rating = request.args.get('kinopoisk_rating')
if id_director is None and year is None and kinopoisk_rating is None and duration is None and title is None and genre is None:
raise Exception()
except:
return '', 400
if id_director is not None:
i = update_film(id, 'id_director', id_director)
if title is not None:
i = update_film(id, 'title', title)
if duration is not None:
i = update_film(id, 'duration', duration)
if year is not None:
i = update_film(id, 'year', year)
if genre is not None:
i = update_film(id, 'genre', genre)
if kinopoisk_rating is not None:
i = update_film(id, 'kinopoisk_rating', kinopoisk_rating)
if i == 0:
return '', 400
s = '/films/{'+id+'}'
return json.dumps({
'Location': s
}), 200, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/films/<id>', methods=['DELETE'])
def delete_film(id):
access_token = request.headers.get('Authorization', '')[len('OAUTH-TOKEN '):]
print access_token
i = expired_check1(access_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
if i==1:
return json.dumps({'error': 'expired_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8'}
row = film_by_id(id)
if row == 0:
return '', 404
del_film(id)
s = '/films/{'+id+'}'
return json.dumps({
'Location': s
}), 200, {
'Content-Type': 'application/json;charset=UTF-8',
}
@app.route('/oauth/authorize', methods=['GET'])
def authorize():
response_type = request.args.get('response_type')
client_id = request.args.get('client_id')
state = request.args.get('state')
if client_id is None:
return render_template('fail_authorize.html', reason='Require client_id.')
if client_exist(client_id)==0:
return render_template('fail_authorize.html', reason='client_id is invalid.')
s = read_redirect(client_id)
if response_type is None:
return redirect(s.decode(encoding='ISO-8859-1', errors='strict') + '?error=invalid_request' +
('' if state is None else '&state=' + state), code=302)
if response_type != 'code':
return redirect(s.decode(encoding='ISO-8859-1', errors='strict') + '?error=unsupported_response_type' +
('' if state is None else '&state=' + state), code=302)
return render_template('authorize.html', state=state,
client_id=client_id,
client_name='Films')
@app.route('/oauth/authorize', methods=['POST'])
def authorize_films():
client_id = str(request.form.get('client_id'))
phone = str(request.form.get('phone'))
password = str(request.form.get('password'))
state = str(request.form.get('state'))
if user_pass_check(phone, password) == 0:
return redirect(read_redirect(client_id) + '?error=access_denied' + ('' if state is None else '&state=' + state), code=302)
code = ''.join(random.choice(string.lowercase) for i in range(10))
code_insert(code, phone)
s = read_redirect(client_id)
return redirect(s + ' ?code=' + code + ('' if state is None else '&state=' + state), code=302)
@app.route('/oauth/token', methods=['POST'])
def get_token():
try:
type = request.args.get('type')
client_id = request.args.get('client_id')
secret_id = request.args.get('secret_id')
except KeyError:
return json.dumps({'error': 'invalid_request'}), 400, {
'Content-Type': 'application/json;charset=UTF-8',
}
if client_id is None or type is None or secret_id is None:
return json.dumps({'error': 'invalid_request'}), 400, {
'Content-Type': 'application/json;charset=UTF-8',
}
if client_exist(client_id)==0 or client_secret_check(client_id,secret_id)==0:
return json.dumps({'error': 'invalid_client'}), 400, {
'Content-Type': 'application/json;charset=UTF-8',
}
if type == 'code':
code = request.args.get('code')
phone = code_check(code)
if phone == 0:
return json.dumps({'error': 'invalid_grant'}), 400, {
'Content-Type': 'application/json;charset=UTF-8',
}
elif type == 'refresh_token':
try:
refresh_token = request.args.get('refresh_token')
except KeyError:
return json.dumps({'error': 'invalid_request'}), 400, {
'Content-Type': 'application/json;charset=UTF-8',
}
phone = refresh_token_check(refresh_token)
if phone == 0:
return json.dumps({'error': 'invalid_grant'}), 400, {
'Content-Type': 'application/json;charset=UTF-8',
}
i = expired_check(refresh_token)
if i==0:
return json.dumps({'error': 'invalid_token'}), 400, {
'Content-Type': 'application/json;charset=UTF-8',
}
expired_refresh(refresh_token)
return '', 200
access_token = ''.join(random.choice(string.lowercase) for i in range(30))
expire_time = datetime.now() + timedelta(minutes=10)
refresh_token = ''.join(random.choice(string.lowercase) for i in range(30))
insert_token(phone, access_token, expire_time, refresh_token)
return json.dumps({
'access_token': access_token,
'token_type': 'bearer',
'expires_in': 300,
'refresh_token': refresh_token,
}), 200, {
'Content-Type': 'application/json;charset=UTF-8',
'Cache-Control': 'no-store',
'Pragma': 'no-cache',
}
if __name__ == '__main__':
app.run(port=5100, debug=True)