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, StatusCodeRedirect
9 from pylons.wsgiapp import PylonsApp
10 from routes.middleware import RoutesMiddleware
11 from paste import httpexceptions
13 from tw.api import make_middleware
14 import authkit.authorize
16 from bluechips.config.environment import load_environment
18 from bluechips.lib.permissions import BlueChipUser, DummyAuthenticate
20 def make_app(global_conf, full_stack=True, **app_conf):
21 """Create a Pylons WSGI application and return it
24 The inherited configuration for this application. Normally from
25 the [DEFAULT] section of the Paste ini file.
28 Whether or not this application provides a full WSGI stack (by
29 default, meaning it handles its own exceptions and errors).
30 Disable full_stack when this application is "managed" by
31 another WSGI middleware.
34 The application's local configuration. Normally specified in the
35 [app:<name>] section of the Paste ini file (where <name>
38 # Configure the Pylons environment
39 load_environment(global_conf, app_conf)
44 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
45 app = authkit.authorize.middleware(app, BlueChipUser())
46 app = DummyAuthenticate(app, app_conf)
47 app = httpexceptions.make_middleware(app, global_conf)
49 # Routing/Session/Cache Middleware
50 app = RoutesMiddleware(app, config['routes.map'])
51 app = SessionMiddleware(app, config)
52 app = CacheMiddleware(app, config)
54 app = make_middleware(app, {
55 'toscawidgets.framework': 'pylons',
56 'toscawidgets.framework.default_view': 'mako',
57 'toscawidgets.middleware.inject_resources': True
60 if asbool(full_stack):
61 # Handle Python exceptions
62 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
64 # Display error documents for 401, 403, 404 status codes (and
65 # 500 when debug is disabled)
66 if asbool(config['debug']):
67 app = StatusCodeRedirect(app)
69 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
71 # Establish the Registry for this application
72 app = RegistryManager(app)
74 # Static files (If running in production, and Apache or another web
75 # server is handling this static content, remove the following 3 lines)
76 static_app = StaticURLParser(config['pylons.paths']['static_files'])
77 app = Cascade([static_app, app])