-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtutor.py
78 lines (53 loc) · 2.42 KB
/
tutor.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
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 set_cookie(self, name, value):
"""Function to set an http cookie"""
self.response.headers.add_header('Set-Cookie', '%s=%s; Path=/' % (name, value))
def get_cookie(self, name):
"""Function to get the value of a named parameter of an http cookie"""
return self.request.cookies.get(name)
class TutorPage(BaseHandler):
def get(self):
user = users.get_current_user()
if user:
logout_url = users.create_logout_url('home.html')
disciple = Disciple.get_current(user)
template_values = {
'logout_url': logout_url
}
self.set_cookie('current_lesson', str(disciple.tutor_current_lesson))
self.write_template('tutor.html', **template_values)
else:
loginURL = users.create_login_url(self.request.uri)
self.redirect(loginURL)
def post(self):
current_lesson = self.request.get('current_lesson')
max_lesson = self.request.get('max_lesson')
user = users.get_current_user()
disciple = Disciple.get_current(user)
disciple.tutor_max_lesson = int(max_lesson)
disciple.tutor_current_lesson = current_lesson
disciple.put()
self.set_cookie('current_lesson', str(disciple.tutor_current_lesson))
### ROUTER
app = webapp2.WSGIApplication([('/tutor/?', TutorPage)],
debug=True)