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