-
Notifications
You must be signed in to change notification settings - Fork 0
/
sosuserpages.py
executable file
·399 lines (336 loc) · 13.6 KB
/
sosuserpages.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
import os
from google.appengine.ext.webapp import template
from datetime import datetime, date, time, timedelta
from google.appengine.api import users
from google.appengine.api import mail
from sosdata import UsersWaitingList2
from sosdata import UserChoices
from sosdata import UsersData
from sosdata import InventoryList
from sosdata import Item
from soserrors import SOSException
import sosdata
import sos_uuid as uuid
import logging
import traceback
import pprint
from soscommon import AdminRootPage
# user code
class UserRootPage(AdminRootPage):
def verify_user(self):
if users.get_current_user():
self.url = users.create_logout_url("/")
self.url_linktext = 'Logout'
else:
self.url = users.create_login_url("/")
self.url_linktext = 'Login'
raise SOSException("You must login")
user = users.get_current_user()
email = user.email()
self.email = email
users_query = UsersData.all()
users_query.filter('email = ', email)
g_us = users_query.fetch(1)
if len(g_us) < 1:
logging.error("trying lower case %s" % (email))
email = email.lower()
users_query.filter('email = ', email)
g_us = users_query.fetch(1)
if len(g_us) < 1:
error_msg = "You are not authorized to use this service. You are currently logged into google as: |%s|. Please verify that this is the user name known to SOS farms" % (email)
logging.error(error_msg)
raise SOSException(error_msg)
self.sos_user = g_us[0]
self.google_user = user
def get_next_inv(self):
now = date.today()
inv_query = InventoryList.all()
inv_query.filter('date >= ', now)
inv = inv_query.fetch(1)
if inv == None or len(inv) < 1:
msg = "The inventory for this week is not yet ready. Please try back later"
return (None,None,msg)
item_query = Item.all()
item_query.filter('date = ', inv[0].date)
items = item_query.fetch(1000)
item_dict = {}
for i in items:
item_dict[i.name] = i
return (inv[0], item_dict, None)
def new_choice(self, inv, i):
# validate that a choice doesnt already exist this this
# item
uc = UserChoices(item=i, user=self.sos_user)
uc.date = inv.date
uc.quantity = "0"
uc.quantity_int = 0
uc.charge = "0.00"
uc.charge_float = 0.0
uc.purchased = 0
uc.id = str(uuid.uuid1())
choice_query = UserChoices.all()
choice_query.filter('item = ', i)
choice_query.filter('user = ', self.sos_user)
why_exist = choice_query.fetch(1)
if why_exist != None and len(why_exist) > 0:
logging.error("The item %s has already been selected by user %s: stack %s" % (i.name, str(self.email), pprint.pformat(traceback.extract_stack())))
# raise SOSException("The item %s has already been selected" % (i.name))
# could return the found choice here, but i don't think any
# should be found so i want to know when this happens
return why_exist[0]
uc.put()
return uc
def get_user_choice(self, inv, items):
choice_query = UserChoices.all()
choice_query.filter('date = ', inv.date)
choice_query.filter('user = ', self.sos_user)
choice_query.order('item')
user_choices = choice_query.fetch(1000)
# set them all up
kys = sorted(items.keys())
if user_choices == None or len(user_choices) < 1:
user_choices = []
for k in kys:
i = items[k]
uc = self.new_choice(inv, i)
user_choices.append(uc)
# update tmp_remaining
else:
for k in kys:
i = items[k]
found = False
for uc in user_choices:
#try:
# if uc.item != None:
# id = uc.item.id
#except datastore_errors.Error, e:
# if e.args[0] == "ReferenceProperty failed to be resolved":
# uc.item = None
# uc.put()
# else:
# raise
# why is uc.item ever allowed to be None?
#if uc.item != None and i.id == uc.item.id:
if uc.item != None and i.id == uc.item.id:
found = True
if found == False:
logging.info("the item %s was not found. user must have ordered, then the item was added, then they re-ordered %s." % (str(i), str(self.sos_user)))
uc = self.new_choice(inv, i)
user_choices.append(uc)
user_choices = sorted(user_choices, key=lambda u: u.item.name)
return user_choices
def do_page(self, inv, user_choices, printable=False):
if inv == None:
harvest_date_str = self.get_harvest_date(None)
else:
harvest_date_str = inv.date.strftime("%Y-%m-%d")
# add up costs
purch = "Not yet purchased!"
sub_total = 0.0
f_ucs = []
for uc in user_choices:
if uc.item != None:
sub_total = sub_total + float(uc.charge)
if int(uc.item.remaining) > 0 or int(uc.quantity) > 0:
f_ucs.append(uc)
if uc.purchased == 1:
purch = "You have placed your order, but you may still alter it"
tax = sub_total * sosdata.sos_get_tax_rate()
final_total = sub_total + tax
if inv.locked == 1:
fname = 'invlocked.html'
purch = "The inventory is all set"
else:
fname = 'choices.html'
if printable:
fname = "choicesprintable.htm"
len_a = len(f_ucs)
for j in range(0, len_a):
i = len_a - 1 - j
uc = f_ucs[i]
tax_str = "%6.2f" % (round(tax, 2))
sub_total_str = "%6.2f" % (round(sub_total, 2))
final_total_str = "%6.2f" % (round(final_total, 2))
template_values = {
'purch_msg': purch,
'username': self.sos_user.firstname,
'sub_total_charge': sub_total_str,
'final_total_charge': final_total_str,
'tax': tax_str,
'harvest_date': harvest_date_str,
'choices': f_ucs,
'logout_url': self.url,
'url_linktext': self.url_linktext,
}
path = os.path.join(os.path.dirname(__file__), fname)
self.response.out.write(template.render(path, template_values))
def post(self):
try:
self.verify_user()
(inv, items, msg) = self.get_next_inv()
if inv == None:
self.not_ready(msg)
else:
button_type = self.request.get('button')
logging.info("post for user |%s| button %s" % (self.email, button_type))
choices = self.get_user_choice(inv, items)
# validate we are safe
for c in choices:
q = self.request.get(c.item.id)
if q != None and q != "":
if int(q) < 0:
raise SOSException("now now now, you know you can't have less than 0")
it = c.item
rest = int(it.remaining) + int(c.quantity) - int(q)
if rest < 0:
raise SOSException("There are only %s %s remaining. You request %s, which is more than we have."
% (it.remaining, it.name, q))
check_choices = []
for c in choices:
q = self.request.get(c.item.id)
if q != None and q != "":
it = c.item
rest = int(it.remaining) + int(c.quantity) - int(q)
it.remaining = str(rest)
it.remaining_int = rest
it.put()
c.quantity = q
cost = int(q) * float(it.cost)
c.charge = "%6.2f" % (round(cost, 2))
c.charge_float = float(c.charge)
c.quantity_int = int(q)
if button_type == "Purchase":
c.purchased = 1
logging.info("user |%s| %d %s" % (self.email, c.quantity_int, it.name))
if c.item.name in check_choices:
logging.info("why is this choice already there?! ")
else:
check_choices.append(c.item.name)
c.put()
choices = self.get_user_choice(inv, items)
if button_type == "Printable":
self.do_page(inv, choices, True)
else:
self.do_page(inv, choices)
except Exception, ex:
self.error_write(ex)
raise
def get(self):
try:
self.verify_user()
(inv, items, msg) = self.get_next_inv()
if inv == None:
self.not_ready(msg)
else:
choices = self.get_user_choice(inv, items)
self.do_page(inv, choices)
except Exception, ex:
self.error_write(ex)
raise
def not_ready(self, msg):
self.url = users.create_logout_url("/")
template_values = {
'username': self.sos_user.firstname,
'logout_url': self.url,
'not_ready_msg': msg,
'url_linktext': self.url_linktext,
}
path = os.path.join(os.path.dirname(__file__), 'notready.html')
self.response.out.write(template.render(path, template_values))
class WaitingList(AdminRootPage):
def do_page(self, fname="waiting.html"):
try:
users = self.get_all_waiting()
template_values = {
'users': users,
'logout_url': self.url,
'url_linktext': self.url_linktext,
}
path = os.path.join(os.path.dirname(__file__), fname)
self.response.out.write(template.render(path, template_values))
except Exception, ex:
self.error_write(ex)
def get(self):
try:
self.verify_admin()
self.do_page()
except Exception, ex:
self.error_write(ex)
def get_all_waiting(self):
users_query = UsersWaitingList2.all()
g_us = users_query.fetch(1000)
return g_us
def post(self):
try:
self.verify_admin()
button_type = self.request.get('button')
fname = "waiting.html"
if button_type == "Remove":
users = self.get_all_waiting()
for i in users:
v = self.request.get(i.id)
if v == "on":
i.delete()
elif button_type == "Printable":
fname = "waitingprintable.html"
elif button_type == "CSV":
fname = "waitingcsv.html"
self.do_page(fname)
except Exception, ex:
self.error_write(ex)
class Register(AdminRootPage):
def write_out_reg(self):
template_values = {
'logout_url': self.url,
'url_linktext': self.url_linktext,
}
path = os.path.join(os.path.dirname(__file__), 'register.html')
self.response.out.write(template.render(path, template_values))
def get(self):
self.url = ""
self.url_linktext = ""
try:
self.write_out_reg()
except Exception, ex:
self.error_write(ex)
def post(self):
self.url = ""
self.url_linktext = ""
try:
button_type = self.request.get('button')
if button_type == "Register":
fn = self.request.get('firstname')
ln = self.request.get('lastname')
e = self.request.get('email')
a = self.request.get('address')
c = self.request.get('city')
z = self.request.get('zip')
p = self.request.get('phone')
user = UsersWaitingList2(id=str(uuid.uuid1()))
user.email = e
user.firstname = fn
user.lastname = ln
user.address = a
user.phone = p
user.zip = z
user.put()
# email to kelly
message = mail.EmailMessage(sender="[email protected]",
subject="A new user has registered")
message.body = "%s %s\n%s\n%s\n%s\n%s %s\n%s" % (fn, ln, e, p, a, c, z, p)
message.to = "[email protected]"
message.cc = "[email protected]"
message.send()
template_values = {
'firstname': fn,
'lastname': ln,
'email': e,
'address': a,
'city': c,
'phone': p,
'zip': z
}
path = os.path.join(os.path.dirname(__file__), 'thanks.html')
self.response.out.write(template.render(path, template_values))
except Exception, ex:
self.error_write(ex)