| 20 | def middleware_class_from_path(middleware_path): |
| 21 | from django.core import exceptions |
| 22 | try: |
| 23 | dot = middleware_path.rindex('.') |
| 24 | except ValueError: |
| 25 | raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path |
| 26 | mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] |
| 27 | try: |
| 28 | mod = __import__(mw_module, {}, {}, ['']) |
| 29 | except ImportError, e: |
| 30 | raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e) |
| 31 | try: |
| 32 | mw_class = getattr(mod, mw_classname) |
| 33 | except AttributeError: |
| 34 | raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname) |
| 35 | return mw_class |
| 36 | middleware_class_from_path = staticmethod(middleware_class_from_path) |
| 37 | |
| 38 | |
32 | | for middleware_path in settings.MIDDLEWARE_CLASSES: |
33 | | try: |
34 | | dot = middleware_path.rindex('.') |
35 | | except ValueError: |
36 | | raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path |
37 | | mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] |
38 | | try: |
39 | | mod = __import__(mw_module, {}, {}, ['']) |
40 | | except ImportError, e: |
41 | | raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e) |
42 | | try: |
43 | | mw_class = getattr(mod, mw_classname) |
44 | | except AttributeError: |
45 | | raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname) |
| 51 | |
| 53 | # Get incoming and outgoing middleware orders and make sure they contain the same paths |
| 54 | incoming_middleware_order = settings.MIDDLEWARE_CLASSES |
| 55 | outgoing_middleware_order = getattr(settings, 'OUTBOUND_MIDDLEWARE_ORDER', None) |
| 56 | if outgoing_middleware_order: |
| 57 | if not len(incoming_middleware_order) == len(outgoing_middleware_order): |
| 58 | raise exceptions.ImproperlyConfigured, 'OUTBOUND_MIDDLEWARE_ORDER must contain the same middleware as MIDDLEWARE_CLASSES' |
| 59 | for middleware_path in incoming_middleware_order: |
| 60 | if not middleware_path in outgoing_middleware_order: |
| 61 | raise exceptions.ImproperlyConfigured, 'OUTBOUND_MIDDLEWARE_ORDER does not contain %s MIDDLEWARE_CLASSES' % middleware_path |
| 62 | else: |
| 63 | outgoing_middleware_order = list(incoming_middleware_order) |
| 64 | outgoing_middleware_order.reverse() |
| 65 | |
| 66 | instances = {} |
| 67 | for middleware_path in incoming_middleware_order: |
| 68 | mw_class = self.middleware_class_from_path(middleware_path) |