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
12 from wradmin.config.environment import load_environment
14 import authkit.authenticate
15 from authkit.permissions import ValidAuthKitUser
17 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
18 """Create a Pylons WSGI application and return it
21 The inherited configuration for this application. Normally from
22 the [DEFAULT] section of the Paste ini file.
25 Whether this application provides a full WSGI stack (by default,
26 meaning it handles its own exceptions and errors). Disable
27 full_stack when this application is "managed" by another WSGI
31 Whether this application serves its own static files; disable
32 when another web server is responsible for serving them.
35 The application's local configuration. Normally specified in
36 the [app:<name>] section of the Paste ini file (where <name>
40 # Configure the Pylons environment
41 load_environment(global_conf, app_conf)
46 # Routing/Session/Cache Middleware
47 app = RoutesMiddleware(app, config['routes.map'])
48 app = SessionMiddleware(app, config)
49 app = CacheMiddleware(app, config)
51 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
53 if asbool(full_stack):
54 # Handle Python exceptions
55 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
57 # Authorization (not in debug mode)
58 if not config['debug']:
59 permission = ValidAuthKitUser()
60 app = authkit.authorize.middleware(app, permission)
61 app = authkit.authenticate.middleware(app, app_conf)
63 # Display error documents for 401, 403, 404 status codes (and
64 # 500 when debug is disabled)
65 if asbool(config['debug']):
66 app = StatusCodeRedirect(app)
68 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
70 # Establish the Registry for this application
71 app = RegistryManager(app)
73 if asbool(static_files):
75 static_app = StaticURLParser(config['pylons.paths']['static_files'])
76 app = Cascade([static_app, app])