-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
522 lines (476 loc) · 14.1 KB
/
forms.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
import re
import time
from models import *
from alerts import *
import jinja2
import pytz
import urllib2
import webapp2
from google.appengine.ext import ndb
### Handler stuff copied over from main file ###
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir), autoescape=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
def initialize(self, *a, **kw):
webapp2.RequestHandler.initialize(self, *a, **kw)
### Real code starts here ###
class AlertsForm(Handler):
def render_alerts_form(
self,
email="",
alert_type="",
phone="",
carrier="",
start1="",
start2="",
start3="",
end1="",
end2="",
end3="",
days="",
time="",
email_error="",
phone_error="",
carrier_error="",
start1_error="",
start2_error="",
start3_error="",
end1_error="",
end2_error="",
end3_error="",
days_error="",
time_error="",
email_sel="",
text_sel="",
both_sel=""
):
stations = ndb.gql("SELECT station_id, name, coordinates \
FROM StationInfo \
ORDER BY name ASC")
stations_dict = dict()
stations_map = []
for s in stations:
stations_dict[s.station_id] = s.name
stations_map.append([str(s.name), s.coordinates.lat, s.coordinates.lon])
carriers_list = ["AT&T", "T-Mobile", "Verizon", "Qwest", "Sprint", "Virgin Mobile", "Nextel", "Alltel", "Metro PCS", "Powertel", "Boost Mobile", "Suncom", "Tracfone", "U.S. Cellular"]
self.render(
'alerts.html',
email=email,
alert_type=alert_type,
phone=phone,
carrier=carrier,
start1=start1,
start2=start2,
start3=start3,
end1=end1,
end2=end2,
end3=end3,
days=days,
time=time,
email_error=email_error,
phone_error=phone_error,
carrier_error=carrier_error,
start1_error=start1_error,
start2_error=start2_error,
start3_error=start3_error,
end1_error=end1_error,
end2_error=end2_error,
end3_error=end3_error,
days_error=days_error,
time_error=time_error,
email_sel=email_sel,
text_sel=text_sel,
both_sel=both_sel,
stations_dict=stations_dict,
stations_map=stations_map,
carriers_list=carriers_list
)
def get(self):
self.render_alerts_form()
def post(self):
self.email = self.request.get('email')
self.alert_type = self.request.get('alert_type')
def alertSelected(alert_type):
self.email_sel, self.text_sel, self.both_sel = '', '', ''
if alert_type=='email':
self.email_sel='selected'
elif alert_type=='textmsg':
self.text_sel='selected'
elif alert_type=='both':
self.both_sel='selected'
alertSelected(self.alert_type)
self.phone = self.request.get('phone')
self.carrier = self.request.get('carrier')
self.start1 = self.request.get('start1')
self.start2 = self.request.get('start2')
self.start3 = self.request.get('start3')
self.end1 = self.request.get('end1')
self.end2 = self.request.get('end2')
self.end3 = self.request.get('end3')
self.su = self.request.get('su')
self.mo = self.request.get('mo')
self.tu = self.request.get('tu')
self.we = self.request.get('we')
self.th = self.request.get('th')
self.fr = self.request.get('fr')
self.sa = self.request.get('sa')
def generate_days():
candidates=[self.su, self.mo, self.tu, self.we, self.th, self.fr, self.sa]
finalists=[]
for c in candidates:
if c!='':
finalists.append(c)
return finalists
days = generate_days()
self.time = self.request.get('time')
### Validation ###
email_error = ""
valid_email = verifyEmail(self.email)
valid_phone = verifyPhone(self.alert_type, self.phone)
valid_carrier = verifyCarrier(self.alert_type, self.carrier)
valid_days = verifyDays(days)
valid_time = verifyTime(self.time)
valid_stations = verifyStations(self.start1, self.start2, self.start3, self.end1, self.end2, self.end3)
if not (valid_email and valid_phone and valid_carrier and valid_days and valid_time and valid_stations):
email_error = ""
phone_error = ""
carrier_error = ""
days_error = ""
time_error = ""
start1_error, start2_error, start3_error, end1_error, end2_error, end3_error = "", "", "", "", "", ""
if valid_email != True:
email_error = emailError(self.email)
if valid_phone != True:
phone_error = phoneError(self.alert_type, self.phone)
if valid_carrier != True:
carrier_error = carrierError(self.alert_type, self.carrier)
if valid_days != True:
days_error = daysError(days)
if valid_time != True:
time_error = timeError(self.time)
if valid_stations != True:
start1_error, start2_error, start3_error, end1_error, end2_error, end3_error = stationsError(self.start1, self.start2, self.start3, self.end1, self.end2, self.end3)
self.render_alerts_form(
email=self.email,
phone=self.phone,
carrier=self.carrier,
start1=self.start1,
start2=self.start2,
start3=self.start3,
end1=self.end1,
end2=self.end2,
end3=self.end3,
email_error=email_error,
phone_error=phone_error,
carrier_error=carrier_error,
start1_error=start1_error,
start2_error=start2_error,
start3_error=start3_error,
end1_error=end1_error,
end2_error=end2_error,
end3_error=end3_error,
time=self.time,
days=days,
days_error=days_error,
time_error=time_error,
email_sel=self.email_sel,
text_sel=self.text_sel,
both_sel=self.both_sel
)
else:
self.done()
def done(self, *a, **kw):
raise NotImplementedError
class AlertsPage(AlertsForm):
def done(self):
def generate_days():
candidates=[self.su, self.mo, self.tu, self.we, self.th, self.fr, self.sa]
finalists=[]
for c in candidates:
if c!='':
c = int(c)
finalists.append(c)
return finalists
days = generate_days()
time = generate_time(self.time)
a = Alert(
email = self.email,
time = time,
days = days
)
properties = dict(
email=self.email,
phone=self.phone,
carrier=self.carrier,
start1=self.start1,
start2=self.start2,
start3=self.start3,
end1=self.end1,
end2=self.end2,
end3=self.end3
)
for k, v in properties.iteritems():
if v!='':
if 'start' in k or 'end' in k:
int_v = int(v)
setattr(a, k, int_v)
elif 'phone' in k:
decimal_only = re.compile(r'[^\d]+')
clean_v = decimal_only.sub('',v)
int_v = int(clean_v)
setattr(a, k, int_v)
else:
setattr(a, k, v)
a_key = a.put()
alert_id = a_key.id()
sendConfirmEmail(
to=self.email,
alert_id = alert_id
)
self.redirect("/awaitconfirm")
class AwaitConfirmPage(Handler):
def get(self):
self.render('awaitconfirm.html')
class ConfirmAlertPage(Handler):
def getStationNames(self, station_id_1, station_id_2, station_id_3):
names_list=[]
s = StationInfo.query(StationInfo.station_id == int(station_id_1)).get()
names_list.append(s.name)
if station_id_2!='':
s = StationInfo.query(StationInfo.station_id == int(station_id_2)).get()
names_list.append(s.name)
if station_id_3!='':
s = StationInfo.query(StationInfo.station_id == int(station_id_3)).get()
names_list.append(s.name)
if len(names_list)==3:
names_list.insert(1, ', ')
names_list.insert(len(names_list)-1, ', and ')
elif len(names_list)==2:
names_list.insert(1, ' and ')
station_names = ''.join(names_list)
return station_names
def getDays(self, days):
days_list = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
i=0
names_list = []
while i<=6:
if i in days:
if len(names_list)!=0:
names_list.append(', ')
names_list.append(days_list[i])
i+=1
if len(names_list)==2:
names_list.insert(1,' and ')
elif len(names_list)>2:
names_list.insert(len(names_list)-1, ' and ')
days = ''.join(names_list)
return days
def render_confirm_alerts_page(
self,
alert_id
):
a = Alert.get_by_id(int(alert_id))
a.confirmed = True
a.put()
properties = ['carrier', 'days', 'email', 'end1', 'end2', 'end3', 'phone', 'start1', 'start2', 'start3', 'time']
values = {}
for p in properties:
if getattr(a, p)!=None:
values[p] = getattr(a, p)
else:
values[p] = ''
email = values['email']
phone = values['phone']
carrier = values['carrier']
start_names = self.getStationNames(values['start1'], values['start2'], values['start3'])
end_names = self.getStationNames(values['end1'], values['end2'], values['end3'])
days = self.getDays(values['days'])
time = datetime.time.strftime(values['time'], '%I:%M %p')
self.render(
'confirmalerts.html',
email=email,
phone=phone,
carrier=carrier,
start_names=start_names,
end_names=end_names,
days=days,
time=time,
alert_id=alert_id
)
def get(self, alert_id):
self.render_confirm_alerts_page(alert_id)
class CancelAlertPage(ConfirmAlertPage):
def render_cancel_alerts_page(
self,
alert_id
):
a = Alert.get_by_id(int(alert_id))
a.confirmed = False
a.put()
properties = ['carrier', 'days', 'email', 'end1', 'end2', 'end3', 'phone', 'start1', 'start2', 'start3', 'time']
values = {}
for p in properties:
if getattr(a, p)!=None:
values[p] = getattr(a, p)
else:
values[p] = ''
email = values['email']
phone = values['phone']
carrier = values['carrier']
start_names = self.getStationNames(values['start1'], values['start2'], values['start3'])
end_names = self.getStationNames(values['end1'], values['end2'], values['end3'])
days = self.getDays(values['days'])
time = datetime.time.strftime(values['time'], '%I:%M %p')
self.render(
'cancelalerts.html',
email=email,
phone=phone,
carrier=carrier,
start_names=start_names,
end_names=end_names,
days=days,
time=time,
alert_id=alert_id
)
def get(self, alert_id):
self.render_cancel_alerts_page(alert_id)
########## This is where the utils go ##########
##### Email validation #####
EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
def verifyEmail(email):
if EMAIL_RE.match(email):
return True
def emailError(email):
if not EMAIL_RE.match(email):
email_error = "That's not a valid email."
return email_error
##### Phone validation #####
def verifyPhone(alert_type, phone):
if alert_type=='email':
return True
else:
phone_digitizer = re.compile(r'[^\d]+') #only digits allowed, no other characters
new_phone = phone_digitizer.sub('', phone)
if len(str(new_phone))==10:
return True
def phoneError(alert_type, phone):
if phone=='':
if alert_type!='email':
phone_error = "Phone number field cannot be blank if you've elected to receive text message alerts."
else:
phone_error = "Make sure you've elected an email-only alerts, or fill out this field."
else:
phone_digitizer = re.compile(r'[^\d]+') #only digits allowed, no other characters
new_phone = phone_digitizer.sub('', phone)
if len(str(new_phone))!=10:
phone_error="Please enter a valid 10-digit phone number."
else:
phone_error="Something else went wrong."
return phone_error
##### Carrier validation #####
def verifyCarrier(alert_type, carrier):
if alert_type=='email':
return True
else:
if carrier!='':
return True
def carrierError(alert_type, carrier):
if alert_type=='email':
carrier_error = ''
elif carrier=='':
carrier_error = 'Please select your wireless carrier from the list.'
else:
carrier_error = 'There was some other kind of error with the carrier selection.'
return carrier_error
##### Days validation #####
def verifyDays(days):
if len(days)>0:
return True
def daysError(days):
if len(days)==0:
days_error = 'Select at least one day of the week on which to receive this alert'
else:
days_error = 'There was some other error with the days chosen or not chosen'
return days_error
##### Time validation #####
def generate_time(time):
logging.debug(time)
time_array = time.split(":")
logging.debug(time_array)
hour = int(time_array[0])
logging.debug(hour)
try:
minute = int(time_array[1])
time = datetime.time(hour, minute, 0)
logging.debug(time)
except:
logging.debug('this is not something I can parse into a minute :(')
logging.debug(time_array[1])
minute = int(time_array[1])
time = datetime.time(hour, minute, 0)
logging.debug(time)
return time
def verifyTime(time):
if (time!=None and time!=''):
try:
time_check = generate_time(time)
return True
except:
logging.debug('A time was submitted in an invalid format:')
logging.debug(time)
return False
def timeError(time):
if (time==None or time==''):
time_error = 'Please enter the time of day to receive the alert.'
else:
try:
time_check = generate_time(time)
logging.debug(time_check)
time_error = ''
except:
logging.debug('A time was submitted in an invalid format:')
logging.debug(time)
time_error = 'Please enter a valid time in the 24-hour format, e.g., 17:30 for 5:30 PM.'
return time_error
##### Stations validation ######
def verifyStations(start1, start2, start3, end1, end2, end3):
stations = [start1, start2, start3, end1, end2, end3]
#start by making sure that start1 and end1 are filled in
if (start1!='' and end1!=''):
#now check for invalid stations
invalid_stations = 0
for s in stations:
if s!='':
n = StationInfo.query(StationInfo.station_id == int(s)).get()
if n==None:
invalid_stations+=1
else:
name = n.name
if invalid_stations==0:
return True
def stationsError(start1, start2, start3, end1, end2, end3):
stations = [start1, start2, start3, end1, end2, end3]
errors = ['', '', '', '', '', '']
#first make sure start1 and end1 are populated
if (start1=='' or end1==''):
if start1=='':
errors[0] = 'Select at least one station as your first choice to start from.'
if end1=='':
errors[3] = 'Select at least one station as your first choice to dock at the end of your journey.'
#if the above is not an issue, then move on to checking for stations with invalid IDs
else:
i=0
while i <6:
if stations[i]!='':
n = StationInfo.query(StationInfo.station_id == int(stations[i])).get()
if n==None:
errors[i]='No station found with this ID.'
i+=1
return errors[0], errors[1], errors[2], errors[3], errors[4], errors[5]