-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate1.py
71 lines (57 loc) · 2 KB
/
validate1.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
import webapp2
import random
import logging
import cgi
form="""
<form method="post">
<label>Month</label><input name="Month" value="%(month)s">
<label>Day</label> <input name="Day" value="%(day)s">
<label>Year</label> <input name="Year" value="%(year)s">
<input type="submit">
<div style="color: red">%(error)s</div>
</form>
"""
def valid_day(day):
if day<32 and day>0:
return True
return False
def valid_month(month):
months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"]
month=month[:3]
for x in range(0, 11):
if months[x]==month:
return True
return False
def valid_year(year):
if(year>1900 and year<2017):
return True
return False
def escape_html(s):
return cgi.escape(s, quote = True)
class MainPage(webapp2.RequestHandler):
def write_form(self, error="", month="", day="", year=""):
month=escape_html(month)
self.response.headers['Content-Type']='text/html'
self.response.write("When is your birthday?")
self.response.write(form % {"error": error, "month": month,"day": day, "year": year})
def get(self):
self.response.headers['Content-Type']='text/html'
self.write_form()
def post(self):
month = self.request.get("Month")
#self.response.write(month)
day = self.request.get("Day")
#self.response.write(day)
year = self.request.get("Year")
#self.response.write(year)
if valid_day(int(day)) and valid_month(month) and valid_year(int(year)):
self.redirect("/success")
else:
self.write_form("That is not correct", month, day, year)
class SuccessPage(webapp2.RequestHandler):
def get(self):
self.response.write("Thanks! That's a totally valid day !!!")
application = webapp2.WSGIApplication([
('/', MainPage), # maps the URL '/' to MainPage
('/success', SuccessPage)
], debug=True)