/shabti/templates/authplus/+package+/config/middleware.py_tmpl
Unknown | 74 lines | 59 code | 15 blank | 0 comment | 0 complexity | 82d996e88ad3a58436b48af25a798c76 MD5 | raw file
1"""Pylons middleware initialization"""
2from beaker.middleware import SessionMiddleware
3from paste.cascade import Cascade
4from paste.registry import RegistryManager
5from paste.urlparser import StaticURLParser
6from paste.deploy.converters import asbool
7from pylons.middleware import ErrorHandler, StatusCodeRedirect
8from pylons.wsgiapp import PylonsApp
9from routes.middleware import RoutesMiddleware
10from tw.api import make_middleware
11
12from {{package}}.config.environment import load_environment
13
14def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
15 """Create a Pylons WSGI application and return it
16
17 ``global_conf``
18 The inherited configuration for this application. Normally from
19 the [DEFAULT] section of the Paste ini file.
20
21 ``full_stack``
22 Whether this application provides a full WSGI stack (by default,
23 meaning it handles its own exceptions and errors). Disable
24 full_stack when this application is "managed" by another WSGI
25 middleware.
26
27 ``static_files``
28 Whether this application serves its own static files; disable
29 when another web server is responsible for serving them.
30
31 ``app_conf``
32 The application's local configuration. Normally specified in
33 the [app:<name>] section of the Paste ini file (where <name>
34 defaults to main).
35
36 """
37 # Configure the Pylons environment
38 config = load_environment(global_conf, app_conf)
39
40 # The Pylons WSGI app
41 app = PylonsApp(config=config)
42
43 # Routing/Session/Cache Middleware
44 app = RoutesMiddleware(app, config['routes.map'], singleton=False)
45 app = SessionMiddleware(app, config)
46
47 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
48 app = make_middleware(app, {
49 'toscawidgets.framework' : 'pylons',
50 'toscawidgets.framework.default_view' : 'mako',
51 'toscawidgets.middleware.inject_resources' : True,
52 })
53
54 if asbool(full_stack):
55 # Handle Python exceptions
56 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
57
58 # Display error documents for 401, 403, 404 status codes (and
59 # 500 when debug is disabled)
60 if asbool(config['debug']):
61 app = StatusCodeRedirect(app)
62 else:
63 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
64
65 # Establish the Registry for this application
66 app = RegistryManager(app)
67
68 if asbool(static_files):
69 # Serve static files
70 static_app = StaticURLParser(config['pylons.paths']['static_files'])
71 app = Cascade([static_app, app])
72 app.config = config
73 return app
74