]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/config/middleware.py
Rendering root URL is now done by Genshi.
[philipp/winterrodeln/wradmin.git] / wradmin / config / middleware.py
1 """Pylons middleware initialization"""
2 from beaker.middleware import 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.middleware import ErrorHandler, StatusCodeRedirect
8 from pylons.wsgiapp import PylonsApp
9 from routes.middleware import RoutesMiddleware
10
11 from wradmin.config.environment import load_environment
12
13 import authkit.authenticate
14 from authkit.permissions import ValidAuthKitUser
15
16 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
17     """Create a Pylons WSGI application and return it
18
19     ``global_conf``
20         The inherited configuration for this application. Normally from
21         the [DEFAULT] section of the Paste ini file.
22
23     ``full_stack``
24         Whether this application provides a full WSGI stack (by default,
25         meaning it handles its own exceptions and errors). Disable
26         full_stack when this application is "managed" by another WSGI
27         middleware.
28
29     ``static_files``
30         Whether this application serves its own static files; disable
31         when another web server is responsible for serving them.
32
33     ``app_conf``
34         The application's local configuration. Normally specified in
35         the [app:<name>] section of the Paste ini file (where <name>
36         defaults to main).
37
38     """
39     # Configure the Pylons environment
40     config = load_environment(global_conf, app_conf)
41
42     # The Pylons WSGI app
43     app = PylonsApp(config=config)
44
45     # Routing/Session Middleware
46     app = RoutesMiddleware(app, config['routes.map'])
47     app = SessionMiddleware(app, config)
48
49     # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
50
51     if asbool(full_stack):
52         # Handle Python exceptions
53         app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
54         
55         # Authorization (not in debug mode)
56         if not config['debug']:
57             permission = ValidAuthKitUser()
58             app = authkit.authorize.middleware(app, permission)
59             app = authkit.authenticate.middleware(app, app_conf)
60
61         # Display error documents for 401, 403, 404 status codes (and
62         # 500 when debug is disabled)
63         if asbool(config['debug']):
64             app = StatusCodeRedirect(app)
65         else:
66             app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
67
68     # Establish the Registry for this application
69     app = RegistryManager(app)
70
71     if asbool(static_files):
72         # Serve static files
73         static_app = StaticURLParser(config['pylons.paths']['static_files'])
74         app = Cascade([static_app, app])
75     app.config = config
76     return app