Skip to content

Instantly share code, notes, and snippets.

@nirabpudasaini
Forked from Sumaxi/Birthday Plugin
Last active August 29, 2015 14:02
Show Gist options
  • Save nirabpudasaini/bee03b23daa5d7834b63 to your computer and use it in GitHub Desktop.
Save nirabpudasaini/bee03b23daa5d7834b63 to your computer and use it in GitHub Desktop.
import webapp2
import cgi
form="""
<form method="post">
What is your birthday?
<br>
<label>Month <input type="text" name="month" value="%(month)s">
</label>
<label>Day <input type="text" name="day" value="%(day)s">
</label>
<label>Year <input type="text" name="year" value="%(year)s">
</label>
<div style="color: red">%(error)s</div>
<input type="submit">
</form>
"""
class MainPage(webapp2.RequestHandler):
def write_form(self, error="", month="", day="", year=""):
self.response.out.write(form % {"error": error,
"month": escape_html(month),
"day": escape_html(day),
"year": escape_html(year)})
def get(self):
self.write_form()
def post(self):
bool_month = valid_month(self.request.get('month'))
bool_day = valid_day(self.request.get('day'))
bool_year = valid_year(self.request.get('year'))
user_month = self.request.get('month')
user_day = self.request.get('day')
user_year = self.request.get('year')
if not (bool_month and bool_day and bool_year):
self.write_form("Not Valid",user_month, user_day, user_year)
else:
self.redirect("/thanks")
class ThanksHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write("Thanks! That's a totally valid date! cough not day. DATE")
months = ['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(),m) for m in months)
def valid_month(month):
if month:
#cap_month = month.capitalize()
#if cap_month in months:
# return cap_month
short_month = month[:3].lower()
return month_abbvs.get(short_month)
def valid_day(day):
if day and day.isdigit():
day = int(day)
if day > 0 and day <= 31:
return day
def valid_year(year):
if year and year.isdigit():
year = int(year)
if year > 1900 and year <2020:
return year
def escape_html(s):
return cgi.escape(s, quote = True)
app = webapp2.WSGIApplication([('/',MainPage), ('/thanks', ThanksHandler)], debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment