]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/config/middleware.py
2d0769a040f7135827618532ea400e7f4de829c3
[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, StaticJavascripts, \
9     StatusCodeRedirect
10 from pylons.wsgiapp import PylonsApp
11 from routes.middleware import RoutesMiddleware
12 from paste import httpexceptions
13
14 from tw.api import make_middleware
15 import authkit.authorize
16
17 from bluechips.config.environment import load_environment
18
19 from bluechips.lib.permissions import BlueChipUser, DummyAuthenticate
20
21 def make_app(global_conf, full_stack=True, **app_conf):
22     """Create a Pylons WSGI application and return it
23
24     ``global_conf``
25         The inherited configuration for this application. Normally from
26         the [DEFAULT] section of the Paste ini file.
27
28     ``full_stack``
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.
33
34     ``app_conf``
35         The application's local configuration. Normally specified in the
36         [app:<name>] section of the Paste ini file (where <name>
37         defaults to main).
38     """
39     # Configure the Pylons environment
40     load_environment(global_conf, app_conf)
41
42     # The Pylons WSGI app
43     app = PylonsApp()
44     
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)
49     
50     # Routing/Session/Cache Middleware
51     app = RoutesMiddleware(app, config['routes.map'])
52     app = SessionMiddleware(app, config)
53     app = CacheMiddleware(app, config)
54     
55     app = make_middleware(app, {
56             'toscawidgets.framework': 'pylons',
57             'toscawidgets.framework.default_view': 'mako',
58             'toscawidgets.middleware.inject_resources': True
59             })
60     
61     if asbool(full_stack):
62         # Handle Python exceptions
63         app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
64
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)
69         else:
70             app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
71
72     # Establish the Registry for this application
73     app = RegistryManager(app)
74
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])
79     return app