Last active
February 21, 2016 20:12
-
-
Save p1nesap/7a1e9051d8cebc9de0dc to your computer and use it in GitHub Desktop.
Python form with conditionals.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cgi | |
import time | |
import datetime | |
import re | |
import cgitb; #cgitb.enable() | |
print "Content-type: text/html" | |
today = datetime.date.today() | |
print today.strftime("<p style='color:#3E3535'>%A, %d %B</p>") | |
# ^^ closing triple quotes | |
form = cgi.FieldStorage(keep_blank_values=1) | |
name = form.getvalue("name") | |
mood = form.getvalue("mood") | |
if name == "": | |
print "<p>Type your first name.</p>" | |
#using localtime for span variable v v | |
localtime = time.localtime(time.time()) | |
mon = localtime[1] # MONTH | |
h = localtime[3] # HOUR | |
span = "morning" if h < 12 else "afternoon" if h < 18 else "evening" | |
#convert following to switch (intval(date('n'))) cases for 1-12 months, with repeating variables for each season. | |
if mon <= 3: | |
var1 = "winter" | |
var2 = "progress, like a squirrel building a cozy nest." | |
var3 = "cold alienation" | |
elif mon <= 5: | |
var1 = "Spring" | |
var2 = "growth, like the burgeoning flowers, grass and leaves." | |
var3 = "the mud season" | |
elif mon <= 9: | |
var1 = "summer" | |
var2 = "health and enjoyment, and soak in the long days of happiness." | |
var3 = "exasperation" | |
elif mon <= 11: | |
var1 = "autumn" | |
var2 = "contentment, and observe nature's palette of red, brown, yellow and gold." | |
var3 = "bleak and stark moods" | |
else: | |
var1 = "holiday" | |
var2 = "joy and progress, like an ambitious elf in Santa's workshop." | |
var3 = "suffering" | |
if mood == "bad" and name != "": | |
print "<div class='textright'>Stay the course through a difficult %s %s, %s; %s will give way to healing calm and joy once more. Make a plan to be around people.</div>" % (var1, span, str.capitalize(name), var3) | |
elif mood == "good" and name != "": | |
print "<div class='textright'>Camraderie, good cheer, and focus are yours this %s %s, %s. Stay on this path of %s</div>" % (var1, span, str.capitalize(name), var2) | |
elif mood == "fair" and name != "": | |
print "<div class='textright'>A subdued %s %s provides an opportunity for reflection. Take a step back to evaluate things. Possibilities will reveal themselves, %s. </div>" % (var1, span, str.capitalize(name)) | |
else: | |
print "" | |
print """ | |
<html> | |
<head> | |
<meta name="google-site-verification" content="YOUR SITE VERIFICATION KEY HERE" /> | |
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" /> | |
<title>Mood App, by Paul Mollomo. Coded in Python; served on Google App Engine.</title> | |
</head> | |
<body> | |
<h4><p>Mood App<br /><br /></p></h4> | |
<form method="post" action="/"> | |
<p>First Name: <input type="text" name="name"/></p> | |
<p>How are things?</p> | |
<p><input type="radio" name="mood" value="good">Good</p> | |
<p><input type="radio" name="mood" value="bad">Bad</p> | |
<p><input type="radio" name="mood" value="fair">Fair</p> | |
<p><input type="submit"value="Process"></p> | |
</form> | |
<p> | |
Find me on YouTube: </br> | |
<script src="https://apis.google.com/js/platform.js"></script> | |
<div class="g-ytsubscribe" data-channel="p1nesap" data-layout="full" data-count="default"></div> | |
</br> | |
</p> | |
<div id="footer"><a href="http://www.flashdrivelinux.com">Paul Mollomo</a> © 2013. Written in Python; served on Google App Engine.</div> | |
</body> | |
</html> | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment