-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
165 lines (134 loc) · 4.52 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
try:
import json
except:
from django.utils import simplejson as json
import cache
import markdown
import os
import re
import time
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
### Models
class Answer(db.Model):
project = db.StringProperty()
name = db.StringProperty()
class Index(webapp.RequestHandler):
def get(self, project):
path = os.path.join(os.path.dirname(__file__), 'index.html')
values = {
'project': project
}
index = template.render(path, values)
self.response.out.write(index)
class Presenter(webapp.RequestHandler):
def get(self, project):
path = os.path.join(os.path.dirname(__file__), 'presenter.html')
values = {
'project': project
}
index = template.render(path, values)
self.response.out.write(index)
class Answers(webapp.RequestHandler):
def get(self, project):
path = os.path.join(os.path.dirname(__file__), 'answers.html')
values = {
'project': project
}
index = template.render(path, values)
self.response.out.write(index)
class OpenAnswer(webapp.RequestHandler):
def post(self, project):
project = self.request.get('project')
name = self.request.get('name')
answer = Answer(project = project, name = name)
answer.put()
self.response.out.write('OK')
class ClearAnswers(webapp.RequestHandler):
def post(self, project):
project = self.request.get('project')
q = db.GqlQuery("SELECT * FROM Answer where project = :1", project)
results = q.fetch(50)
for p in results:
p.delete()
self.response.out.write('OK')
class ReadAnswer(webapp.RequestHandler):
def get(self, project):
name = self.request.get('name')
q = db.GqlQuery("SELECT * FROM Answer where project = :1 AND name = :2", project, name)
results = q.fetch(1)
# Has an answer been opened up?
if len(results) > 0:
path = os.path.join(os.path.dirname(__file__), 'slides', name + '.markdown')
# Split out answer.
file = open(path, 'r')
parts = file.read().split('### Answer')
if len(parts) > 1:
self.response.out.write(parts[1])
file.close()
class Toc(webapp.RequestHandler):
def get(self, project):
path = os.path.join(os.path.dirname(__file__), 'toc.html')
values = {
'project': project
}
toc = template.render(path, values)
self.response.out.write(toc)
class Slide(webapp.RequestHandler):
def get(self, name):
path = os.path.join(os.path.dirname(__file__), 'slides', name + '.markdown')
# Split into code and an instructional slide.
file = open(path, 'r')
parts = file.read().split('### ')
output = {}
# The slide is always the first component.
# Apply markdown to the slide component.
output['slide'] = markdown.markdown('### ' + parts[1])
# Syntax highlighting.
output['slide'] = output['slide'].replace('<pre', '<pre class="prettyprint"')
for part in parts:
# First line of each part is the name.
lines = part.split('\n')
key = lines[0].lower()
output[key] = '\n'.join(lines[1:]).strip()
# Set default HTML if none is provided.
if 'html' not in output:
output['html'] = '<div id="map_canvas" style="height: 100%;"></div>'
# Auto linking of the API reference.
output['slide'] = re.sub(r'\{([A-Za-z]+)\}',
'<code><a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#\\1" target="\\1">\\1</a></code>', output['slide'])
# Avoid exposing answers.
if 'answer' in output:
output['answer'] = True;
file.close()
slide = json.dumps(output)
self.response.out.write(slide)
# HACK
class Plus(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'plus.html')
values = {
'callback': self.request.get('callback')
}
plus = template.render(path, values)
self.response.out.write(plus)
application = webapp.WSGIApplication([
('/slide/(.*)', Slide),
('/toc/(.*)', Toc),
('/answers/edit/(.*)', Answers),
('/answers/open(.*)', OpenAnswer),
('/answers/clear(.*)', ClearAnswers),
('/answers/read/(.*)', ReadAnswer),
('/presenter/(.*)', Presenter),
('/plus', Plus),
('/(.*)', Index),
], debug=True)
# Uncomment to enable aggressive caching.
# application = cache.CacheMiddleware(application)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()