-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo.py
624 lines (557 loc) · 22.9 KB
/
photo.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
from google.cloud import datastore
from flask import Flask, request, jsonify, Blueprint, make_response
import datetime
import json
import constants
import google.oauth2.credentials
import google_auth_oauthlib.flow
import googleapiclient.discovery
from google.auth.transport import requests
from google.oauth2 import id_token
client = datastore.Client()
bp = Blueprint('photo', __name__, url_prefix='/photos')
photo_errors = {
"400_partial":{"Error": "There is an issue with one of the attributes, no attributes sent, or additional attributes sent."},
"400_required": {"Error": "The request object is missing at least one of the required attributes, there is an issue with one of the required attributes, or additional attributes sent."},
"400_duplicate": {"Error": "The photo is already tagged with that tag_id."},
"401": {"Error": "No credentials provided or provided credentials invalid."},
"403": {"Error": "Credentials provided do not have access to this photo"},
"404": {"Error": "No photo with this photo_id exists"},
"404_put": {"Error": "No photo with this photo_id exists and/or no tag with this tag_id exists"},
"404_delete": {"Error": "No photo with this photo_id exists and/or no tag with this tag_id exists, or this photo was not tagged by this tag_id"},
"405": {"Error" : "Method Not Allowed"},
"406": {"Error": "Unsupported MIME type sent in request Accept Header or no Accept Header sent."},
"409": {"Error": "The url specified in the request object is already taken."},
"415": {"Error": "Only application/json acceptable."}
}
url_valid_characters = ["-", "_", ".", "~", "!", "*", "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]" ]
photo_attributes = ["url", "description", "date" ]
date_format = '%Y-%m-%d'
CLIENT_ID = '174243479293-stg0oqthgeor34ea8afn4j6pn93s25c8.apps.googleusercontent.com'
@bp.route('', methods=['POST','GET'])
def photos_get_post():
if request.method == 'POST':
if no_401(request.headers.get('Authorization')) == False:
res = make_response(json.dumps(photo_errors["401"]))
res.mimetype = 'application/json'
res.status_code = 401
return res
auth = request.headers.get('Authorization').split()
jwt=auth[1]
ownerID = verify_jwt(jwt)
content = request.get_json()
if request.content_type and 'application/json' not in request.content_type:
res = make_response(json.dumps(photo_errors["415"]))
res.mimetype = 'application/json'
res.status_code = 415
return res
if 'application/json' != request.headers['Accept']:
res = make_response(json.dumps(photo_errors["406"]))
res.mimetype = 'application/json'
res.status_code = 406
return res
if content == None or len(content.keys()) != 3:
print("len")
res = make_response(json.dumps(photo_errors["400_required"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
for field in content.keys():
if field not in photo_attributes:
print("missing field")
res = make_response(json.dumps(photo_errors["400_required"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
if verify_photo(content[field], field) == False:
print("verify")
res = make_response(json.dumps(photo_errors["400_required"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
query = client.query(kind=constants.photos)
query.add_filter("owner", "=", ownerID["sub"])
results = list(query.fetch())
for e in results:
if e["url"].lower() == content["url"].lower():
res = make_response(json.dumps(photo_errors["409"]))
res.mimetype = 'application/json'
res.status_code = 409
return res
new_photo = datastore.entity.Entity(key=client.key(constants.photos))
new_photo.update({
"url": content["url"],
"description": content["description"],
"date": content["date"],
"tags": [],
"owner": ownerID["sub"],
})
client.put(new_photo)
result = client.get(key=new_photo.key)
#add id, and self, and send data back to user
result["id"] = str(new_photo.key.id)
result["self"] = request.url_root + "photos/" + str(new_photo.key.id)
print("New photo added:")
print(result)
#return created photo
res = make_response(json.dumps(result))
res.mimetype = 'application/json'
res.status_code = 201
return res
elif request.method == 'GET':
if no_401(request.headers.get('Authorization')) == False:
res = make_response(json.dumps(photo_errors["401"]))
res.mimetype = 'application/json'
res.status_code = 401
return res
auth = request.headers.get('Authorization').split()
jwt=auth[1]
ownerID = verify_jwt(jwt)
content = request.get_json()
if 'application/json' != request.headers['Accept']:
res = make_response(json.dumps(photo_errors["406"]))
res.mimetype = 'application/json'
res.status_code = 406
return res
query = client.query(kind=constants.photos)
query.add_filter("owner", "=", ownerID["sub"])
q_limit = int(request.args.get('limit', '5'))
q_offset = int(request.args.get('offset', '0'))
l_iterator = query.fetch(limit= q_limit, offset=q_offset)
pages = l_iterator.pages
results = list(next(pages))
if l_iterator.next_page_token:
next_offset = q_offset + q_limit
next_url = request.base_url + "?limit=" + str(q_limit) + "&offset=" + str(next_offset)
else:
next_url = None
for e in results:
e["id"] = e.key.id
if "tags" in e.keys():
for tag in e["tags"]:
tag["self"] = request.url_root + "tags/" + tag["id"]
else:
e["tags"] = []
e["self"] = request.url_root + "photos/" + str(e.key.id)
output = {"photos": results}
if next_url:
output["next"] = next_url
query = client.query(kind=constants.photos)
query.add_filter("owner", "=", ownerID["sub"])
output["count"] = len(list(query.fetch()))
res = make_response(json.dumps(output))
res.mimetype = 'application/json'
res.status_code = 200
return res
else:
res = make_response(json.dumps(photo_errors["405"]))
res.mimetype = 'application/json'
res.status_code = 405
return res
@bp.route('/<photo_id>', methods=['GET','DELETE', 'PATCH', 'PUT'])
def photo_get_delete_patch_put(photo_id):
if request.method == 'GET':
if no_401(request.headers.get('Authorization')) == False:
res = make_response(json.dumps(photo_errors["401"]))
res.mimetype = 'application/json'
res.status_code = 401
return res
auth = request.headers.get('Authorization').split()
jwt=auth[1]
ownerID = verify_jwt(jwt)
if 'application/json' != request.headers['Accept']:
res = make_response(json.dumps(photo_errors["406"]))
res.mimetype = 'application/json'
res.status_code = 406
return res
photo = get_photo(photo_id, ownerID["sub"])
#tag_key = client.key(constants.tags, int(tag_id))
#tag = client.get(key=tag_key)
#if tag not found, return 404
if photo == None:
res = make_response(json.dumps(photo_errors["404"]))
res.mimetype = 'application/json'
res.status_code = 404
return res
if photo == -1:
res = make_response(json.dumps(photo_errors["403"]))
res.mimetype = 'application/json'
res.status_code = 403
return res
'''
tag["id"] = str(tag.key.id)
tag["self"] = request.url_root + "tags/" + str(tag.key.id)
if "photos" in tag.keys():
for photo in tag["photos"]:
photo["self"] = request.url_root + "photos/" + photo["id"]
else:
tag["photos"] = []
'''
res = make_response(json.dumps(photo))
res.mimetype = 'application/json'
res.status_code = 200
return res
elif request.method == 'PATCH':
if no_401(request.headers.get('Authorization')) == False:
res = make_response(json.dumps(photo_errors["401"]))
res.mimetype = 'application/json'
res.status_code = 401
return res
auth = request.headers.get('Authorization').split()
jwt=auth[1]
ownerID = verify_jwt(jwt)
content = request.get_json()
if request.content_type and 'application/json' not in request.content_type:
print(request.content_type)
res = make_response(json.dumps(photo_errors["415"]))
res.mimetype = 'application/json'
res.status_code = 415
return res
if 'application/json' != request.headers['Accept']:
res = make_response(json.dumps(photo_errors["406"]))
res.mimetype = 'application/json'
res.status_code = 406
return res
if content == None:
res = make_response(json.dumps(photo_errors["400_partial"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
for field in content.keys():
if field not in photo_attributes:
res = make_response(json.dumps(photo_errors["400_partial"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
if verify_photo(content[field], field) == False:
res = make_response(json.dumps(photo_errors["400_partial"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
if "url" in content.keys():
query = client.query(kind=constants.photos)
query.add_filter("owner", "=", ownerID["sub"])
results = list(query.fetch())
for e in results:
print(e)
if e["url"].lower() == content["url"].lower() and e.key.id != int(photo_id):
res = make_response(json.dumps(photo_errors["409"]))
res.mimetype = 'application/json'
res.status_code = 409
return res
photo = get_photo(photo_id, ownerID["sub"], False)
#tag_key = client.key(constants.tags, int(tag_id))
#tag = client.get(key=tag_key)
#if tag not found, return 404
if photo == None:
res = make_response(json.dumps(photo_errors["404"]))
res.mimetype = 'application/json'
res.status_code = 404
return res
if photo == -1:
res = make_response(json.dumps(photo_errors["403"]))
res.mimetype = 'application/json'
res.status_code = 403
return res
for key in content.keys():
photo[key] = content[key]
client.put(photo)
photo = get_photo(photo_id, ownerID["sub"])
'''
result = client.get(key=tag_key)
#add id, self to data sent back to user
tag["id"] = str(tag.key.id)
tag["self"] = request.url_root + "tags/" + str(tag.key.id)
if "photos" in tag.keys():
for photo in tag["photos"]:
photo["self"] = request.url_root + "photos/" + photo["id"]
else:
tag["photos"] = []
'''
res = make_response(json.dumps(photo))
res.mimetype = 'application/json'
res.status_code = 200
return res
elif request.method == 'PUT':
if no_401(request.headers.get('Authorization')) == False:
res = make_response(json.dumps(photo_errors["401"]))
res.mimetype = 'application/json'
res.status_code = 401
return res
auth = request.headers.get('Authorization').split()
jwt=auth[1]
ownerID = verify_jwt(jwt)
content = request.get_json()
if request.content_type and 'application/json' not in request.content_type:
res = make_response(json.dumps(photo_errors["415"]))
res.mimetype = 'application/json'
res.status_code = 415
return res
if 'application/json' != request.headers['Accept']:
res = make_response(json.dumps(photo_errors["406"]))
res.mimetype = 'application/json'
res.status_code = 406
return res
if content == None or len(content.keys()) != 3:
res = make_response(json.dumps(photo_errors["400_required"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
for field in content.keys():
if field not in photo_attributes:
res = make_response(json.dumps(photo_errors["400_required"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
if verify_photo(content[field], field) == False:
res = make_response(json.dumps(photo_errors["400_required"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
query = client.query(kind=constants.photos)
query.add_filter("owner", "=", ownerID["sub"])
results = list(query.fetch())
for e in results:
if e["url"].lower() == content["url"].lower() and e.key.id != int(photo_id):
res = make_response(json.dumps(photo_errors["409"]))
res.mimetype = 'application/json'
res.status_code = 409
return res
photo = get_photo(photo_id, ownerID["sub"], False)
#tag_key = client.key(constants.tags, int(tag_id))
#tag = client.get(key=tag_key)
#if tag not found, return 404
if photo == None:
res = make_response(json.dumps(photo_errors["404"]))
res.mimetype = 'application/json'
res.status_code = 404
return res
if photo == -1:
res = make_response(json.dumps(photo_errors["403"]))
res.mimetype = 'application/json'
res.status_code = 403
return res
for key in content.keys():
photo[key] = content[key]
client.put(photo)
photo = get_photo(photo_id, ownerID["sub"])
'''
result = client.get(key=tag_key)
#add id, self to data sent back to user
tag["id"] = str(tag.key.id)
tag["self"] = request.url_root + "tags/" + str(tag.key.id)
if "photos" in tag.keys():
for photo in tag["photos"]:
photo["self"] = request.url_root + "photos/" + photo["id"]
else:
tag["photos"] = []
'''
res = make_response(json.dumps(photo))
res.mimetype = 'application/json'
res.status_code = 200
return res
elif request.method == 'DELETE':
if no_401(request.headers.get('Authorization')) == False:
res = make_response(json.dumps(photo_errors["401"]))
res.mimetype = 'application/json'
res.status_code = 401
return res
auth = request.headers.get('Authorization').split()
jwt=auth[1]
ownerID = verify_jwt(jwt)
photo = get_photo(photo_id, ownerID["sub"], False)
#tag_key = client.key(constants.tags, int(tag_id))
#tag = client.get(key=tag_key)
#if tag not found, return 404
if photo == None:
res = make_response(json.dumps(photo_errors["404"]))
res.mimetype = 'application/json'
res.status_code = 404
return res
if photo == -1:
res = make_response(json.dumps(photo_errors["403"]))
res.mimetype = 'application/json'
res.status_code = 403
return res
photo_key = client.key(constants.photos, int(photo_id))
#if "tags" in photo.keys():
for t in photo["tags"]:
tag_key = client.key(constants.tags, int(t["id"]))
tag = client.get(key=tag_key)
for p in range(len(tag["photos"])):
if tag["photos"][p]["id"] == photo_id:
del tag["photos"][p]
break
client.put(tag)
print(photo_key)
client.delete(photo_key)
photo = get_photo(photo_id, ownerID["sub"], False)
print(photo)
return '',204
else:
res = make_response(json.dumps(photo_errors["405"]))
res.mimetype = 'application/json'
res.status_code = 405
return res
@bp.route('/<photo_id>/tags/<tag_id>', methods=['PUT', 'DELETE'])
def photo_put_delete_tag(photo_id, tag_id):
if request.method == 'PUT':
if no_401(request.headers.get('Authorization')) == False:
res = make_response(json.dumps(photo_errors["401"]))
res.mimetype = 'application/json'
res.status_code = 401
return res
auth = request.headers.get('Authorization').split()
jwt=auth[1]
ownerID = verify_jwt(jwt)
photo = get_photo(photo_id, ownerID["sub"], False)
tag_key = client.key(constants.tags, int(tag_id))
tag = client.get(key=tag_key)
#tag_key = client.key(constants.tags, int(tag_id))
#tag = client.get(key=tag_key)
#if tag not found, return 404
if photo == None or tag == None:
res = make_response(json.dumps(photo_errors["404_put"]))
res.mimetype = 'application/json'
res.status_code = 404
return res
if photo == -1:
res = make_response(json.dumps(photo_errors["403"]))
res.mimetype = 'application/json'
res.status_code = 403
return res
for tag_idx in photo["tags"]:
if tag_idx["id"]==tag_id:
res = make_response(json.dumps(photo_errors["400_duplicate"]))
res.mimetype = 'application/json'
res.status_code = 400
return res
photo["tags"].append({"id": tag_id, "name": tag["name"]})
print(tag.keys())
tag["photos"].append({"id":photo_id})
client.put_multi([photo, tag])
return '',204
if request.method == 'DELETE':
if no_401(request.headers.get('Authorization')) == False:
res = make_response(json.dumps(photo_errors["401"]))
res.mimetype = 'application/json'
res.status_code = 401
return res
auth = request.headers.get('Authorization').split()
jwt=auth[1]
ownerID = verify_jwt(jwt)
photo = get_photo(photo_id, ownerID["sub"], False)
tag_key = client.key(constants.tags, int(tag_id))
tag = client.get(key=tag_key)
#tag_key = client.key(constants.tags, int(tag_id))
#tag = client.get(key=tag_key)
#if tag not found, return 404
if photo == None or tag == None:
res = make_response(json.dumps(photo_errors["404_delete"]))
res.mimetype = 'application/json'
res.status_code = 404
return res
tag_found = False
if photo != -1:
for t in photo["tags"]:
if t["id"]==tag_id:
tag_found = True
break
if tag_found == False:
res = make_response(json.dumps(photo_errors["404_delete"]))
res.mimetype = 'application/json'
res.status_code = 404
return res
if photo == -1:
res = make_response(json.dumps(photo_errors["403"]))
res.mimetype = 'application/json'
res.status_code = 403
return res
for t in range(len(photo["tags"])):
if photo["tags"][t]["id"]==tag_id:
del photo["tags"][t]
break
client.put(photo)
for p in range(len(tag["photos"])):
print(tag["photos"])
print(p)
if tag["photos"][p]["id"]==photo_id:
del tag["photos"][p]
break
client.put(tag)
return '',204
def get_photo(photo_id, owner, return_to_user = True):
photo_key = client.key(constants.photos, int(photo_id))
photo = client.get(key=photo_key)
print(photo)
if photo == None:
return None
if photo["owner"]!= owner:
return -1
if return_to_user == True:
#add id, self to data sent back to user
photo["id"] = str(photo.key.id)
photo["self"] = request.url_root + "photos/" + str(photo.key.id)
if "tags" in photo.keys():
for tag in photo["tags"]:
tag["self"] = request.url_root + "tags/" + tag["id"]
else:
photo["tags"] = []
return photo
def is_accepted_characters(input_string):
return all(ord(c) >= 32 and ord(c)<=126 for c in input_string)
def verify_photo(user_input, field):
if not isinstance(user_input, str):
return False
if field == "url":
if 4 > len(user_input) or len(user_input) > 256:
print("url len")
return False
for letter in user_input:
if not letter.isalnum() and not (letter in url_valid_characters):
print("letter")
return False
elif field == "description":
if 1 > len(user_input) or len(user_input) > 256:
print("desc len")
return False
if is_accepted_characters(user_input) == False:
print("desc char")
return False
elif field == "date":
if len(user_input)!= 10:
print("date len")
return False
try:
date_obj = datetime.datetime.strptime(user_input, date_format)
except ValueError:
print("date invalid")
return False
else:
return True
def no_401(user_authorization):
if user_authorization == None:
return False
if user_authorization.startswith('Bearer '):
auth = request.headers.get('Authorization').split()
jwt=auth[1]
else:
return False
#if missing or invalid JWT
if jwt == None:
return False
ownerID = verify_jwt(jwt)
if ownerID == None or ownerID == -1:
return False
return True
def verify_jwt(jwt):
try:
req = requests.Request()
id_info = id_token.verify_oauth2_token(
jwt, req, CLIENT_ID)
return(id_info)
except ValueError:
return(None)
except:
print("Unexpected Error")
return -1