Skip to content

Instantly share code, notes, and snippets.

@Sumaxi
Last active December 22, 2015 02:09
Show Gist options
  • Save Sumaxi/6401111 to your computer and use it in GitHub Desktop.
Save Sumaxi/6401111 to your computer and use it in GitHub Desktop.
Creating ( In Python ) a fully working google apps engine Birthday engine. Anyone that needed help on Udacity can look here.
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):
user_month = valid_month(self.request.get('month'))
user_day = valid_day(self.request.get('day'))
user_year = valid_year(self.request.get('year'))
if not (user_month and user_day and user_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)
@nirabpudasaini
Copy link

This code will crash on submission of invalid date. It is because you are passing the boolean values that you are using to check if dates are valid or not. I have made a fork of your code and made updates, you can update the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment