/shabti/templates/authplus/+package+/config/middleware.py_tmpl

https://bitbucket.org/gawel/shabti · Unknown · 74 lines · 59 code · 15 blank · 0 comment · 0 complexity · 82d996e88ad3a58436b48af25a798c76 MD5 · raw file

  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. from tw.api import make_middleware
  11. from {{package}}.config.environment import load_environment
  12. def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
  13. """Create a Pylons WSGI application and return it
  14. ``global_conf``
  15. The inherited configuration for this application. Normally from
  16. the [DEFAULT] section of the Paste ini file.
  17. ``full_stack``
  18. Whether this application provides a full WSGI stack (by default,
  19. meaning it handles its own exceptions and errors). Disable
  20. full_stack when this application is "managed" by another WSGI
  21. middleware.
  22. ``static_files``
  23. Whether this application serves its own static files; disable
  24. when another web server is responsible for serving them.
  25. ``app_conf``
  26. The application's local configuration. Normally specified in
  27. the [app:<name>] section of the Paste ini file (where <name>
  28. defaults to main).
  29. """
  30. # Configure the Pylons environment
  31. config = load_environment(global_conf, app_conf)
  32. # The Pylons WSGI app
  33. app = PylonsApp(config=config)
  34. # Routing/Session/Cache Middleware
  35. app = RoutesMiddleware(app, config['routes.map'], singleton=False)
  36. app = SessionMiddleware(app, config)
  37. # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
  38. app = make_middleware(app, {
  39. 'toscawidgets.framework' : 'pylons',
  40. 'toscawidgets.framework.default_view' : 'mako',
  41. 'toscawidgets.middleware.inject_resources' : True,
  42. })
  43. if asbool(full_stack):
  44. # Handle Python exceptions
  45. app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
  46. # Display error documents for 401, 403, 404 status codes (and
  47. # 500 when debug is disabled)
  48. if asbool(config['debug']):
  49. app = StatusCodeRedirect(app)
  50. else:
  51. app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
  52. # Establish the Registry for this application
  53. app = RegistryManager(app)
  54. if asbool(static_files):
  55. # Serve static files
  56. static_app = StaticURLParser(config['pylons.paths']['static_files'])
  57. app = Cascade([static_app, app])
  58. app.config = config
  59. return app