-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
87 lines (67 loc) · 3.12 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import jinja2
import os
import webapp2
from google.appengine.api import users
from models.disciple import Disciple
template_directory = os.path.join(os.path.dirname(__file__), 'templates')
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_directory), autoescape=True)
def render_template(template, **template_values):
"""Renders the given template with the given template_values"""
# retrieve the html template
t = jinja_environment.get_template(template)
# render the html template with th given dictionary
return t.render(template_values)
### HANDLERS
class BaseHandler(webapp2.RequestHandler):
"""Represents a handler which contains functions necessary for multiple handlers"""
def write_template(self, template, **template_values):
"""Function to write out the given template with the given
template_values"""
self.response.out.write(render_template(template, **template_values))
def write_static_template(self, template, **template_values):
"""Wraps the template given with the static.html, and then presents that"""
t = jinja_environment.get_template(template)
template_values['content'] = t.render(template_values)
self.write_template("static.html", **template_values)
class Introduction(BaseHandler):
"""Meant to serve the generic pages to those who may not know about the site, not user-specific"""
def get(self, *args):
specific_page_requested = len(args) > 0
user = users.get_current_user()
template_values = {}
if user:
logout_url = users.create_logout_url('home.html')
disciple = Disciple.get_current(user)
template_values['logout_url'] = logout_url;
if specific_page_requested:
try:
self.write_static_template(args[0], **template_values)
except:
self.error(404)
self.write_static_template("error.html", **template_values)
elif user:
self.redirect('/main')
else:
loginURL = users.create_login_url(self.request.uri)
template_values['loginURL'] = loginURL
self.write_static_template('home.html', **template_values)
class Main(BaseHandler):
"""User's main page. they come here to see which lesson to go to next, to browse words, etc."""
def get(self, *args):
user = users.get_current_user()
template_values = {}
if user:
logout_url = users.create_logout_url('home.html')
disciple = Disciple.get_current(user)
template_values['logout_url'] = logout_url;
self.write_template('main.html', **template_values)
else:
loginURL = users.create_login_url(self.request.uri)
self.redirect(loginURL)
### ROUTER
app = webapp2.WSGIApplication([
('/main(.*)', Main),
('/?', Introduction),
('/(.+)', Introduction)],
debug=True)