1 """Pylons middleware initialization"""
2 from beaker.middleware import CacheMiddleware, SessionMiddleware
3 from paste.cascade import Cascade
4 from paste.registry import RegistryManager
5 from paste.urlparser import StaticURLParser
6 from paste.deploy.converters import asbool
7 from pylons import config
8 from pylons.middleware import ErrorHandler, StaticJavascripts, \
10 from pylons.wsgiapp import PylonsApp
11 from routes.middleware import RoutesMiddleware
12 from paste import httpexceptions
14 from tw.api import make_middleware
15 import authkit.authorize
17 from bluechips.config.environment import load_environment
19 from bluechips.lib.permissions import BlueChipUser, DummyAuthenticate
21 def make_app(global_conf, full_stack=True, **app_conf):
22 """Create a Pylons WSGI application and return it
25 The inherited configuration for this application. Normally from
26 the [DEFAULT] section of the Paste ini file.
29 Whether or not this application provides a full WSGI stack (by
30 default, meaning it handles its own exceptions and errors).
31 Disable full_stack when this application is "managed" by
32 another WSGI middleware.
35 The application's local configuration. Normally specified in the
36 [app:<name>] section of the Paste ini file (where <name>
39 # Configure the Pylons environment
40 load_environment(global_conf, app_conf)
45 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
46 app = authkit.authorize.middleware(app, BlueChipUser())
47 app = DummyAuthenticate(app, app_conf)
48 app = httpexceptions.make_middleware(app, global_conf)
50 # Routing/Session/Cache Middleware
51 app = RoutesMiddleware(app, config['routes.map'])
52 app = SessionMiddleware(app, config)
53 app = CacheMiddleware(app, config)
55 app = make_middleware(app, {
56 'toscawidgets.framework': 'pylons',
57 'toscawidgets.framework.default_view': 'mako',
58 'toscawidgets.middleware.inject_resources': True
61 if asbool(full_stack):
62 # Handle Python exceptions
63 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
65 # Display error documents for 401, 403, 404 status codes (and
66 # 500 when debug is disabled)
67 if asbool(config['debug']):
68 app = StatusCodeRedirect(app)
70 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
72 # Establish the Registry for this application
73 app = RegistryManager(app)
75 # Static files (If running in production, and Apache or another web
76 # server is handling this static content, remove the following 3 lines)
77 static_app = StaticURLParser(config['pylons.paths']['static_files'])
78 app = Cascade([static_app, app])