-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
231 lines (195 loc) · 6.78 KB
/
main.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
import json
import webapp2
import logging
from google.appengine.ext import ndb
import ndbTools
import modelInventory
import modelUser
import modelStore
import modelSupplier
import modelPaymentMethod
import modelPurchaser
import modelSaleTransaction
import modelTest
class RestHandler(webapp2.RequestHandler):
def dispatch(self):
super(RestHandler, self).dispatch()
def SendJson(self, r):
self.response.headers['content-type'] = 'text/plain'
self.response.write(json.dumps(r))
def HandleException(self, exception):
logging.exception(exception)
self.response.write('ERROR OCCURED')
if isinstance(exception, webapp2.HTTPException):
self.response.set_status(exception.code)
else:
self.response.set_status(500)
class UserHandler(RestHandler):
def post(self):
data = json.loads(self.request.body)
user = modelUser.AddUser(data)
r = ndbTools.AsDict(user.key)
self.SendJson(r)
def get(self):
queryParams = json.loads(self.request.get("queryParams"))
print queryParams
users = modelUser.QueryUser(queryParams)
users = [ndbTools.AsDict(user.key) for user in users]
self.SendJson(users)
class UserKeyHandler(RestHandler):
def post(self, key):
r = json.loads(self.request.body)
user = ndbTools.SaveData(key, r)
#user = modelUser.SaveUser(key, r)
r = ndbTools.AsDict(user.key)
self.SendJson(r)
def get(self, key):
user = ndbTools.GetData(key)
r = ndbTools.AsDict(user.key)
self.SendJson(r)
class StoreHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
store = modelStore.AddStore(r)
r = ndbTools.AsDict(store.key)
self.SendJson(r)
def get(self):
store = modelStore.GetStore(self.request.get('key'))
r = ndb.Tools.AsDict(store.key)
self.SendJson(r)
class StoreKeyHandler(RestHandler):
def post(self, key):
r = json.loads(self.request.body)
store = ndbTools.SaveData(key, r)
r = ndbTools.AsDict(store.key)
self.SendJson
def get(self, key):
user = ndbTools.GetData(key)
r = ndbTools.AsDict(user.key)
self.SendJson(r)
class SupplierHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
supplier = modelSupplier.AddSupplier(r)
r = ndbTools.AsDict(supplier.key)
self.SendJson(r)
def get(self):
store = modelStore.GetStore(self.request.get('key'))
r = ndb.Tools.AsDict(store.key)
self.SendJson(r)
class SupplierKeyHandler(RestHandler):
def post(self, key):
r = json.loads(self.request.body)
store = ndbTools.SaveData(key, r)
r = ndbTools.AsDict(store.key)
self.SendJson
def get(self, key):
user = ndbTools.GetData(key)
r = ndbTools.AsDict(user.key)
self.SendJson(r)
class PaymentMethodHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
paymentMethod = modelPaymentMethod.AddPaymentMethod(r)
r = ndbTools.AsDict(paymentMethod.key)
self.SendJson(r)
def get(self):
#print self.request.get('key')
keys = ['agpkZXZ-ZWNvdW50chILEgVTdG9yZRiAgICAgIDFCgw', 'agpkZXZ-ZWNvdW50cigLEgVTdG9yZRiAgICAgIDFCgwLEglQdXJjaGFzZXIYgICAgICAxQkM']
for key in keys:
paymentMethod = modelPaymentMethod.GetPaymentMethod(key)
print '---'
print key
print paymentMethod
for p in paymentMethod:
print '~~~'
print p.purchaser.get().firstName
print '~~~'
#print ndbTools.AsDict(paymentMethod.key)
print '---'
#r = ndb.Tools.AsDict(store.key)
#self.SendJson(r)
class PurchaserHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
purchaser = modelPurchaser.AddPurchaser(r)
r = ndbTools.AsDict(purchaser.key)
self.SendJson(r)
def get(self):
store = modelStore.GetStore(self.request.get('key'))
r = ndb.Tools.AsDict(store.key)
self.SendJson(r)
class InventoryHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
modelInventory.AddInventory(r['inventory'], r['items'])
#paymentMethod = modelPaymentMethod.AddPaymentMethod(r)
#r = ndbTools.AsDict(paymentMethod.key)
#self.SendJson(r)
def get(self):
inventory = modelInventory.QueryInventory(self.request.get('storeKey'))
r = [ndbTools.AsDict(i.key) for i in inventory]
self.SendJson(r)
class InventoryKeyHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
modelInventory.AddInventory(r['inventory'], r['items'])
#paymentMethod = modelPaymentMethod.AddPaymentMethod(r)
#r = ndbTools.AsDict(paymentMethod.key)
#self.SendJson(r)
def get(self, key):
user = ndbTools.GetData(key)
r = ndbTools.AsDict(user.key)
self.SendJson(r)
def delete(self, itemKey):
ndbTools.DeleteData(itemKey)
class SaleTransactionHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
saleTransaction = modelSaleTransaction.AddSaleTransaction(r)
print '---'
print saleTransaction
print '---'
r = ndbTools.AsDict(saleTransaction.key)
print '###'
print r
self.SendJson(r)
def get(self):
store = modelStore.GetStore(self.request.get('key'))
r = ndb.Tools.AsDict(store.key)
self.SendJson(r)
class TestHandler(RestHandler):
def post(self):
data = json.loads(self.request.body)
print '~~~request body~~~'
print data
print '~~~~~~~~~~~~~~~~~~'
test = modelTest.AddTest(data)
print '###json###'
print test
class LookupKeyHandler(RestHandler):
def get(self, key):
print key
print 'hi'
key = ndb.Key(urlsafe=key)
entity = key.get()
if entity:
r = ndbTools.AsDict(key)
self.SendJson(r)
else:
self.HandleException(Exception())
APP = webapp2.WSGIApplication([
('/rest/lookup/key/(.*)', LookupKeyHandler),
('/rest/user', UserHandler),
('/rest/user/(.*)', UserKeyHandler),
('/rest/store', StoreHandler),
('/rest/store/(.*)', StoreKeyHandler),
('/rest/supplier', SupplierHandler),
('/rest/supplier/(.*)', SupplierKeyHandler),
('/rest/paymentMethod', PaymentMethodHandler),
('/rest/purchaser', PurchaserHandler),
('/rest/inventory', InventoryHandler),
('/rest/inventory/(.*)', InventoryKeyHandler),
('/rest/saleTransaction', SaleTransactionHandler),
('/rest/test', TestHandler)
], debug=True)