]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/wradmin/config/middleware.py
4f8a31e494cf6c13a9f56c9589162023c1d52bfd
[philipp/winterrodeln/wradmin.git] / wradmin / wradmin / 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 from wradmin.config.environment import load_environment
13
14 import authkit.authenticate
15 from authkit.permissions import ValidAuthKitUser
16
17 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
18     """Create a Pylons WSGI application and return it
19
20     ``global_conf``
21         The inherited configuration for this application. Normally from
22         the [DEFAULT] section of the Paste ini file.
23
24     ``full_stack``
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
28         middleware.
29
30     ``static_files``
31         Whether this application serves its own static files; disable
32         when another web server is responsible for serving them.
33
34     ``app_conf``
35         The application's local configuration. Normally specified in
36         the [app:<name>] section of the Paste ini file (where <name>
37         defaults to main).
38
39     """
40     # Configure the Pylons environment
41     load_environment(global_conf, app_conf)
42
43     # The Pylons WSGI app
44     app = PylonsApp()
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     # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
52
53     if asbool(full_stack):
54         # Handle Python exceptions
55         app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
56         
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)
62
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)
67         else:
68             app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
69
70     # Establish the Registry for this application
71     app = RegistryManager(app)
72
73     if asbool(static_files):
74         # Serve static files
75         static_app = StaticURLParser(config['pylons.paths']['static_files'])
76         app = Cascade([static_app, app])
77
78     return app