-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshoppinglist.py
55 lines (42 loc) · 1.58 KB
/
shoppinglist.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
import os
# import jinja2 lib
import jinja2
# import regex lib
import webapp2
__author__ = "Harry Staley <staleyh@gmail.com>"
__version__ = "1.0"
# sets the locaiton of the templates folder contained in the home of this file.
TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')
# envokes the jinja2 envronment pointing it to the location of the templates.
# with user input markup automatically escaped
JINJA_ENV = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_DIR))
class Handler(webapp2.RequestHandler):
"""
Handler class for all functions in this app for rendering
any templates.
"""
def write(self, *a, **kw):
""" displays the respective function, parameters, ect. """
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
""" Gets the template and passes it with paramanters. """
tmp = JINJA_ENV.get_template(template)
return tmp.render(params)
def render(self, template, **kw):
""" Calls render_str and displays the template. """
self.write(self.render_str(template, **kw))
class ShoppintListHandler(Handler):
"""
Takes input from Handler and renders the markup
defined.
"""
def get(self):
"""
Passes markup from defined file and passes it to the renderer.
"""
items = self.request.get_all("food")
# pases the value in item variable into the jinja2 template
self.render("shopping_list.html", items=items)
WSGI_APP = webapp2.WSGIApplication([
('/shoppinglist', ShoppintListHandler)
], debug=True)