This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
64 lines (51 loc) · 1.38 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
import re
import cgi
import logging
import basherc
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
# ---
class MainPage(webapp.RequestHandler):
def get(self):
quotes = db.GqlQuery("SELECT * FROM Quote ORDER BY date_record DESC")
mt = basherc.MiniTemplateRenderer(self.response)
mt.produce_index({ 'quotes': quotes })
# ---
class ViewQuote(webapp.RequestHandler):
def get(self):
# extract the quote's ID
reg = re.compile('^/quote/(.+)$')
m = reg.match(self.request.path)
try:
if m:
# attempts to load a quote from the key value
# will raise an error if the key is invalid
quote = db.get(m.group(1))
# if the quote was not found
if not quote:
raise Error
else:
template_values = {
'quotes': [quote],
'lonelymode': True
}
mt = basherc.MiniTemplateRenderer(self.response)
mt.produce_index(template_values)
# we were unable to extract a key from the URI
else:
raise Error
# if somethings happens, lets's throw a 404
except:
self.error(404)
mt = basherc.MiniTemplateRenderer(self.response)
mt.fail()
# ---
application = webapp.WSGIApplication(
[('/', MainPage),
(('/quote/\w+'), ViewQuote)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()