-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.py
122 lines (91 loc) · 3.1 KB
/
items.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
import datetime
import json
import webapp2
from google.appengine.ext import ndb
class Purchaser(ndb.Model):
purchaser = ndb.StringProperty()
quantity = ndb.IntegerProperty()
is_purchased = ndb.BooleanProperty()
class Item(ndb.Model):
quantity = ndb.IntegerProperty()
recipients = ndb.StringProperty(repeated=True)
description = ndb.StringProperty()
notes = ndb.StringProperty()
photos = ndb.StringProperty(repeated=True)
comments = ndb.StringProperty(repeated=True)
purchasers = ndb.StructuredProperty(Purchaser, repeated=True)
created = ndb.DateProperty(auto_now_add=True)
def to_jsonable(self):
out = self.to_dict()
out['created'] = str(self.created)
out['key'] = self.key.urlsafe()
return out
def serve_items_json(response, item_to_include=None, key_to_exclude=None):
response.headers['Content-Type'] = 'application/json'
items = []
for item in Item.query():
if ((not item_to_include or item.key != item_to_include.key) and
item.key != key_to_exclude):
items.append(item.to_jsonable())
if item_to_include:
items.append(item_to_include.to_jsonable())
response.out.write(json.dumps({
'items': items,
}))
class DeleteItem(webapp2.RequestHandler):
def post(self):
key = self.request.get('key')
if not key:
# TODO: Make this a 404
self.response.out.write('"key" field is required.')
return
ndb_key = ndb.Key(urlsafe=key)
ndb_key.delete()
# Appengine queries are only eventually consistent,
# so we need to exclude the item we just deleted.
serve_items_json(self.response, key_to_exclude=ndb_key)
class SaveItem(webapp2.RequestHandler):
def post(self):
raw_data = self.request.get('data')
if not raw_data:
# TODO: Make this a 404
self.response.out.write('"data" field is required.')
return
item = Item()
data = json.loads(raw_data)
key = data.get('key')
if key:
ndb_key = ndb.Key(urlsafe=key)
old_item = ndb_key.get()
if not old_item.created:
old_item.created = datetime.date(2016, 1, 1)
if old_item:
item = old_item
quantity_string = data.get('quantity', 0)
try:
quantity = int(quantity_string)
except ValueError as e:
quantity = 0
created = data.get('created')
if created:
parts = created.split('-')
item.created = datetime.date(int(parts[0]), int(parts[1]), int(parts[2]))
item.quantity = quantity
item.recipients = data.get('recipients', [])
item.description = data.get('description', '')
item.notes = data.get('notes', '')
item.photos = data.get('photos', [])
item.comments = data.get('comments', [])
item.purchasers = data.get('purchasers', [])
item.put()
# Include the item we just saved since reading all the items is only
# eventually consistent, but we know it wrote successfully.
serve_items_json(self.response, item_to_include=item)
class Items(webapp2.RequestHandler):
def get(self):
serve_items_json(self.response)
app = webapp2.WSGIApplication([
('/delete-item', DeleteItem),
('/save-item', SaveItem),
('/items', Items),
])