This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_api.py
720 lines (518 loc) · 21.3 KB
/
web_api.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
"""
Handler classes that deal with data, and not webpages. These
functions can be called as web apis
"""
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext import blobstore
from google.appengine.ext import webapp as webapp2
from google.appengine.api import images
# For image serving
import cloudstorage as gcs
from PIL import Image
import time
import logging
import stripe
import json
import StringIO
from constants import admin_id, PRICE, tax_dict
from lib_auth import verify, authenticate, send_message
from lib_db import Rock, Scenario, User, ModelrParent,\
ActivityLog, ModelServedCount,\
ImageModel, EarthModel, Fluid, Item, Server, \
get_items_by_name_and_user, get_all_items_user, deep_delete,\
get_by_id
from lib_util import posterize, RGBToString
class ModelrAPI(webapp2.RequestHandler):
"""
Base class for modelr apis. Mostly a skeleton right now
"""
def verify(self):
"""
Verify that the current user is a legimate user. Returns the
user object from the database if true, otherwise returns None.
TODO: This shouldn't be done with browser cookies
"""
cookie = self.request.cookies.get('user')
if cookie is None:
return
try:
user, password = cookie.split('|')
except ValueError:
return
return verify(user, password, ModelrParent.all().get())
class dbAPI(ModelrAPI):
entity = Item
def get(self):
"""
Get the requested item(s) from the user's database
"""
self.response.headers['Content-Type'] = 'application/json'
user = self.verify()
if ("keys" in self.request.arguments()):
keys = self.request.get("keys")
items = self.entity.get(keys)
if type(items) is list:
output = json.dumps([item.json
for item in items])
else:
output = json.dumps(items.json)
elif ("all" in self.request.arguments()):
output = json.dumps([item.json
for item in
get_all_items_user(self.entity, user)])
elif ("ls" in self.request.arguments()):
output = json.dumps([item.simple_json
for item in get_all_items_user(self.entity,
user)])
elif ("name" in self.request.arguments()):
name = self.request.get("name")
item = get_items_by_name_and_user(self.entity, name, user)
output = json.dumps(item[0].json)
elif ("id" in self.request.arguments()):
item_id = int(self.request.get("id"))
item = get_by_id(self.entity, item_id, user)
output = json.dumps(item.json)
else:
self.error(502)
self.response.out.write(output)
@authenticate
def delete(self, user):
try:
key = self.request.get('key')
item = self.entity.get(key)
if item.user != user.user_id:
raise Exception
# delete item and all its children
deep_delete(item)
activity = "removed_item"
ActivityLog(user_id=user.user_id,
activity=activity,
parent=ModelrParent.all().get()).put()
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
except Exception as e:
print e
self.error(502)
class ScenarioHandler(ModelrAPI):
"""
API to the scenario database
"""
def get(self):
self.response.headers['Content-Type'] = 'application/json'
# Get the user but don't redirect. Anyone can play with public
# scenarios.
user = self.verify()
name = self.request.get('name')
if user:
scenarios = Scenario.all()
scenarios.ancestor(user)
scenarios.filter("user =", user.user_id)
scenarios.filter("name =", name)
scenarios = scenarios.fetch(1)
else:
scenarios = []
# Get Evan's default scenarios (created with the admin)
scen = Scenario.all()\
.ancestor(ModelrParent.all().get())\
.filter("user_id =", admin_id)
scen = Scenario.all().filter("name =", name).fetch(100)
if scen:
scenarios += scen
if scenarios:
logging.info(scenarios[0])
logging.info(scenarios[0].data)
scenario = scenarios[0]
self.response.out.write(scenario.data)
else:
self.response.out.write('null')
activity = "fetched_scenario"
if user:
ActivityLog(user_id=user.user_id,
activity=activity,
parent=ModelrParent.all().get()).put()
return
@authenticate
def delete(self, user):
name = self.request.get('name')
scenarios = Scenario.all()
scenarios.ancestor(user)
scenarios.filter("user =", user.user_id)
scenarios.filter("name =", name)
scenarios = scenarios.fetch(100)
for scenario in scenarios:
scenario.delete()
activity = "removed_scenario"
ActivityLog(user_id=user.user_id,
activity=activity,
parent=ModelrParent.all().get()).put()
# Output for successful post reception
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
@authenticate
def post(self, user):
# Output for successful post reception
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
name = self.request.get('name')
group = self.request.get('group')
logging.info(('name', name))
data = self.request.get('json')
logging.info(data)
scenarios = Scenario.all()
scenarios.ancestor(user)
scenarios.filter("user =", user.user_id)
scenarios.filter("name =", name)
scenarios = scenarios.fetch(1)
# Rewrite if the name exists, create new one if it doesn't
if scenarios:
scenario = scenarios[0]
else:
scenario = Scenario(parent=user)
scenario.user = user.user_id
scenario.name = name
scenario.group = group
# Save in Db
scenario.data = data.encode()
scenario.put()
activity = "modified_scenario"
ActivityLog(user_id=user.user_id,
activity=activity,
parent=ModelrParent.all().get()).put()
class RockHandler(dbAPI):
entity = Rock
@authenticate
def post(self, user):
# Adds a rock to the database, will throw an error
# if the rock name already exists
try:
name = self.request.get("name")
rocks = get_items_by_name_and_user(self.entity, name, user)
# Rewrite if the rock exists
if rocks:
# write out error message
raise
else:
rock = Rock(parent=user)
rock.user = user.user_id
# Populate the object
rock.vp = float(self.request.get('vp'))
rock.vs = float(self.request.get('vs'))
rock.rho = float(self.request.get('rho'))
rock.vp_std = float(self.request.get('vp_std'))
rock.vs_std = float(self.request.get('vs_std'))
rock.rho_std = float(self.request.get('rho_std'))
rock.porosity = float(self.request.get('porosity'))
rock.vclay = float(self.request.get('vclay'))
rock.description = self.request.get('description')
rock.name = self.request.get('name')
rock.group = self.request.get('group')
fluid_key = self.request.get("rock-fluid")
if fluid_key != "None":
rock.fluid_key = Fluid.get(str(fluid_key)).key()
# Save in the database
rock.put()
activity = "added_rock"
ActivityLog(user_id=user.user_id,
activity=activity,
parent=ModelrParent.all().get()).put()
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
except Exception as e:
# send error
print e
self.error(502)
@authenticate
def put(self, user):
# Updates a rock database object
# Get the database key from the request
try:
key = self.request.get("db_key")
rock = Rock.get(key)
# Update the rock
rock.vp = float(self.request.get('vp'))
rock.vs = float(self.request.get('vs'))
rock.rho = float(self.request.get('rho'))
rock.vp_std = float(self.request.get('vp_std'))
rock.vs_std = float(self.request.get('vs_std'))
rock.rho_std = float(self.request.get('rho_std'))
rock.porosity = float(self.request.get('porosity'))
rock.vclay = float(self.request.get('vclay'))
rock.description = self.request.get('description')
rock.name = self.request.get('name')
rock.group = self.request.get('group')
fluid_key = self.request.get("rock-fluid")
# This makes sure the fluid exists
try:
rock.fluid_key = Fluid.get(fluid_key).key()
except:
rock.fluid_key = None
rock.name = self.request.get('name')
rock.put()
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
except Exception as e:
# Write out error message
print e
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
return
class ImageModelHandler(dbAPI):
def get(self):
user = self.verify()
if "all" in self.request.arguments():
models = get_all_items_user(ImageModel, user)
data = [{"colours": [RGBToString(j[1]) for j in
Image.open(
blobstore.BlobReader(i.image.key()))
.convert('RGB').getcolors()],
"image": images.get_serving_url(i.image),
"key": str(i.key()),
"earth_models": [j.json for j in
EarthModel.all().ancestor(i)
.filter("user =",
user.user_id if user else None)
.fetch(1000)]}
for i in models]
else:
self.error(502)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(data))
@authenticate
def delete(self, user):
image_key = self.request.get("image_key")
image = ImageModel.get(image_key)
deep_delete(image)
class FluidHandler(dbAPI):
entity = Fluid
@authenticate
def post(self, user):
try:
name = self.request.get("name")
fluids = get_items_by_name_and_user(self.entity, name, user)
# Rewrite if the rock exists
if fluids:
raise Exception
else:
fluid = Fluid(parent=user)
fluid.user = user.user_id
fluid.rho_w = float(self.request.get('rho_w'))
fluid.rho_hc = float(self.request.get('rho_hc'))
fluid.kw = float(self.request.get('kw'))
fluid.khc = float(self.request.get('khc'))
fluid.sw = float(self.request.get('sw'))
fluid.name = name
fluid.description = self.request.get("description")
fluid.group = self.request.get("group")
fluid.put()
activity = "added_fluid"
ActivityLog(user_id=user.user_id,
activity=activity,
parent=ModelrParent.all().get()).put()
except Exception as e:
# Handle error
print e
self.error(502)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
@authenticate
def put(self, user):
# Get the database key from the request
try:
key = self.request.get("db_key")
fluid = Fluid.get(key)
# Update the fluid
fluid.rho_w = float(self.request.get('rho_w'))
fluid.rho_hc = float(self.request.get('rho_hc'))
fluid.kw = float(self.request.get('kw'))
fluid.khc = float(self.request.get('khc'))
fluid.sw = float(self.request.get('sw'))
fluid.description = self.request.get('description')
fluid.name = self.request.get('name')
fluid.group = self.request.get('group')
fluid.name = self.request.get('name')
fluid.put()
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
except Exception as e:
# Write out error message
print e
pass
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('All OK!!')
return
class EarthModelHandler(dbAPI):
entity = EarthModel
@authenticate
def post(self, user):
try:
data = json.loads(self.request.body)
name = data["name"]
image_key = data["image_key"]
image_model = ImageModel.get(image_key)
# See if we are overwriting
earth_model = EarthModel.all().ancestor(image_model)
earth_model = earth_model.filter('user =', user.user_id)
earth_model = earth_model.filter('name =', name).get()
if earth_model:
earth_model.data = json.dumps(data)
else:
earth_model = EarthModel(user=user.user_id,
data=json.dumps(data),
name=name,
parent=image_model)
earth_model.put()
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(earth_model.json))
except Exception as e:
# TODO Handle failure
print "KLASJFDAJLKFSDA", e
self.error(502)
@authenticate
def delete(self, user):
try:
# Get the root of the model
input_model_key = self.request.get('input_image_id')
name = self.request.get('name')
image_model = ImageModel.get(input_model_key)
model = EarthModel.all().ancestor(image_model)
model = model.filter("user =", user.user_id)
model = model.filter("name =", name).get()
if model:
model.delete()
self.response.out.write(json.dumps({'success': True}))
except Exception as e:
print e
self.response.out.write(json.dumps({'success': False}))
class StripeHandler(ModelrAPI):
'''
Handle webhook POSTs from Stripe
'''
def post(self):
event = json.loads(self.request.body)
# Get the event id and retrieve it from Stripe
# anybody can post, doing it this way is more secure
# event_id = event_json["id"]
# event = stripe.Event.retrieve(event_id)
if event["type"] == "invoice.payment_succeeded":
# For testing, change it to a known user in stripe
# and use the webhooks testings
# event["data"]["object"]["customer"] = \
# "cus_3ZL6yHJqE8DfTx"
# event["data"]["object"]["total"] = price
stripe_id = event["data"]["object"]["customer"]
amount = PRICE
event_id = event["data"]["object"]["id"]
user = User.all().ancestor(ModelrParent.all().get())
user = user.filter("stripe_id =", stripe_id).fetch(1)
# Serious issue here, we need to deal with this in a
# a clever way
if not user:
message = ("Failed to find modelr user for stripe " +
"user %s, but was invoiced by stripe "
% (stripe_id))
send_message(subject="Non-existent user invoiced",
message=message)
self.response.write("ALL OK")
return
tax = tax_dict.get(user[0].tax_code, None)
if not tax:
self.response.write("ALL OK")
return
# Tax them up
stripe.InvoiceItem.create(customer=stripe_id,
amount=int(amount * tax),
currency="usd",
description="Canadian Taxes")
self.response.write("ALL OK")
elif (event["type"] == 'customer.subscription.deleted'):
# for stripe
self.response.write("ALL OK")
stripe_id = event["data"]["object"]["customer"]
user = User.all().ancestor(ModelrParent.all().get())
user = user.filter("stripe_id =", stripe_id).get()
# This should never ever happen
if not user:
message = ("Failed to find modelr user for stripe " +
"user %s, but was invoiced by stripe "
% (stripe_id))
send_message(subject="Non-existent user canceled",
message=message)
return
user.delete()
self.response.write("ALL OK")
elif (event["type"] == 'customer.subscription.created'):
message = str(event)
send_message(subject=event["type"],
message=message)
self.response.write("All OK")
# Send an email otherwise. We can trim this down to ones we
# actually care about.
else:
# Too many hooks, too much noise. commented out
#message = str(event)
#send_message(subject=event["type"],
# message=message)
self.response.write("ALL OK")
class Upload(blobstore_handlers.BlobstoreUploadHandler,
ModelrAPI):
"""
Handles image uploads from users. Allows them to upload images to
the blobstore
"""
@authenticate
def post(self, user):
# Get the blob files
upload_files = self.get_uploads()
blob_info = upload_files[0]
# All this is in a try incase the image format isn't accepted
try:
# Read the image file
reader = blobstore.BlobReader(blob_info.key())
im = Image.open(reader, 'r')
im = im.convert('RGB').resize((350, 350))
im = posterize(im)
output = StringIO.StringIO()
im.save(output, format='PNG')
bucket = '/modelr_live_bucket/'
output_filename = (bucket + str(user.user_id) + '/2' +
str(time.time()))
gcsfile = gcs.open(output_filename, 'w')
gcsfile.write(output.getvalue())
output.close()
gcsfile.close()
# Make a blob reference
bs_file = '/gs' + output_filename
output_blob_key = blobstore.create_gs_key(bs_file)
ImageModel(parent=user,
user=user.user_id,
image=output_blob_key).put()
self.redirect('/model')
except Exception as e:
print e
self.redirect('/model?error=True')
class UpdateCreditCardHandler(ModelrAPI):
@authenticate
def post(self, user):
try:
card_token = self.request.get("card_token")
stripe_id = user.stripe_id
# Create the new credit card
customer = stripe.Customer.retrieve(stripe_id)
card = customer.sources.create(source=card_token)
# set as the default credit card
customer.default_source = card
customer.save()
self.response.write(json.dumps({"message":
"successfully updated card"}))
except stripe.InvalidRequestError as e:
self.response.write(json.dumps({"message": e.msg}))
class ModelServed(ModelrAPI):
def post(self):
models_served = ModelServedCount.all().get()
models_served.count += 1
models_served.put()
class BackendServerHandler(ModelrAPI):
def get(self):
hostname = Server.all().get().host
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps({'hostname': hostname}))