-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (44 loc) · 1.62 KB
/
main.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
"""
Built as a Google App Engine Demo for Cloud Saturday
"""
import webapp2
import jinja2
import os
from datetime import date
from google.appengine.ext import ndb
from google.appengine.api import users
from google.appengine.api import mail
class Choice(ndb.Model):
"""Lunch choice"""
choice_text = ndb.StringProperty()
user = ndb.StringProperty()
create_date = ndb.DateProperty(auto_now_add=True)
class Main(webapp2.RequestHandler):
def get(self):
variables = {}
today = Choice.query(Choice.create_date==date.today())
variables['choices'] = today
template = JINJA_ENVIRONMENT.get_template("templates/home.html")
self.response.write(template.render(variables))
def post(self):
choice_text = self.request.get("choice")
user = self.request.get("name")
choice = Choice(choice_text=choice_text, user=user)
choice.put()
self.redirect("/")
class EmailCron(webapp2.RequestHandler):
def get(self):
todays_choices = Choice.query(Choice.create_date==date.today())
body = "Hey There! Here are today's choices for lunch <br />"
for choice in todays_choices:
body += "Choice: %s by %s <br />" % (choice.choice_text, choice.user)
mail.send_mail("[email protected]", "[email protected]", "Lunch Orders for Today", html=body)
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True
)
app = webapp2.WSGIApplication([
('/', Main),
('/email', EmailCron),
], debug=True)