]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/config/middleware.py
set user settings title on user controller
[bluechips.git] / bluechips / config / middleware.py
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
12 import authkit.authorize
13
14 from bluechips.config.environment import load_environment
15
16 from bluechips.lib.permissions import BlueChipUser, DummyAuthenticate
17
18 def make_app(global_conf, full_stack=True, **app_conf):
19     """Create a Pylons WSGI application and return it
20
21     ``global_conf``
22         The inherited configuration for this application. Normally from
23         the [DEFAULT] section of the Paste ini file.
24
25     ``full_stack``
26         Whether or not this application provides a full WSGI stack (by
27         default, meaning it handles its own exceptions and errors).
28         Disable full_stack when this application is "managed" by
29         another WSGI middleware.
30
31     ``app_conf``
32         The application's local configuration. Normally specified in the
33         [app:<name>] section of the Paste ini file (where <name>
34         defaults to main).
35     """
36     # Configure the Pylons environment
37     load_environment(global_conf, app_conf)
38
39     # The Pylons WSGI app
40     app = PylonsApp()
41     
42     # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
43     app = authkit.authorize.middleware(app, BlueChipUser())
44     app = DummyAuthenticate(app, app_conf)
45     
46     # Routing/Session/Cache Middleware
47     app = RoutesMiddleware(app, config['routes.map'])
48     app = SessionMiddleware(app, config)
49     app = CacheMiddleware(app, config)
50     
51     if asbool(full_stack):
52         # Handle Python exceptions
53         app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
54
55         # Display error documents for 401, 403, 404 status codes (and
56         # 500 when debug is disabled)
57         status_codes = [400, 401, 403, 404]
58         if not asbool(config.get('debug')):
59             status_codes.append(500)
60         app = StatusCodeRedirect(app, status_codes)
61
62     # Establish the Registry for this application
63     app = RegistryManager(app)
64
65     # Static files (If running in production, and Apache or another web 
66     # server is handling this static content, remove the following 3 lines)
67     static_app = StaticURLParser(config['pylons.paths']['static_files'])
68     app = Cascade([static_app, app])
69     return app