-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp.py
79 lines (57 loc) · 2.11 KB
/
app.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
from collections import defaultdict
import web
import config
import model as m
VERSION = "0.0.1"
urls = (
r'/', 'Index',
)
app = web.application(urls, globals())
# Allow session to be reloadable in development mode.
if web.config.get('_session') is None:
session = web.session.Session(app, web.session.DiskStore('sessions'),
initializer={'flash': defaultdict(list)})
web.config._session = session
else:
session = web.config._session
def flash(group, message):
session.flash[group].append(message)
def flash_messages(group=None):
if not hasattr(web.ctx, 'flash'):
web.ctx.flash = session.flash
session.flash = defaultdict(list)
if group:
return web.ctx.flash.get(group, [])
else:
return web.ctx.flash
render = web.template.render('templates/',
base='base',
cache=config.cache)
t_globals = web.template.Template.globals
t_globals['datestr'] = web.datestr
t_globals['app_version'] = lambda: VERSION + ' - ' + config.env
t_globals['flash_messages'] = flash_messages
t_globals['render'] = lambda t, *args: render._template(t)(*args)
class Index:
def GET(self):
flash("success", """Welcome! Application code lives in app.py,
models in model.py, tests in test.py, and seed data in seed.py.""")
return render.index()
# Set a custom internal error message
def internalerror():
msg = """
An internal server error occurred. Please try your request again by
hitting back on your web browser. You can also <a href="/"> go back
to the main page.</a>
"""
return web.internalerror(msg)
# Setup the application's error handler
app.internalerror = web.debugerror if web.config.debug else internalerror
if config.email_errors.to_address:
app.internalerror = web.emailerrors(config.email_errors.to_address,
app.internalerror,
config.email_errors.from_address)
# Adds a wsgi callable for uwsgi
application = app.wsgifunc()
if __name__ == "__main__":
app.run()