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) |
| 40 | # (middleware_path, weight) tuples in MIDDLEWARE_CLASSES, |
| 41 | # heavier items processed first in outbound methods |
| 42 | # OR just backwards-compatible strings |
| 43 | middleware_list = None |
| 44 | if isinstance(settings.MIDDLEWARE_CLASSES[0], tuple): |
| 45 | # uniqueness test |
| 46 | key_set = list(set([mw[1] for mw in settings.MIDDLEWARE_CLASSES])) |
| 47 | if len(key_set) != len(settings.MIDDLEWARE_CLASSES): |
| 48 | raise ImproperlyConfigured("Middleware outbound ordering " |
| 49 | "values have to be unique.") |
| 50 | |
| 51 | middleware_list = _middleware_list_from_iterable([(mw[1], mw[0]) |
| 52 | for mw in settings.MIDDLEWARE_CLASSES]) |
| 53 | else: |
| 54 | middleware_list = _middleware_list_from_iterable(enumerate( |
| 55 | settings.MIDDLEWARE_CLASSES)) |
| 56 | |
| 57 | if not middleware_list: |
| 58 | return |
| 59 | |
| 60 | # inbound |
| 61 | for _, mw_instance in middleware_list: |
| 214 | def _middleware_list_from_iterable(middleware_iterable): |
| 215 | middleware_list = [] |
| 216 | for middleware_tuple in middleware_iterable: |
| 217 | middleware_path = middleware_tuple[1] |
| 218 | try: |
| 219 | dot = middleware_path.rindex('.') |
| 220 | except ValueError: |
| 221 | raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path |
| 222 | mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] |
| 223 | try: |
| 224 | mod = __import__(mw_module, {}, {}, ['']) |
| 225 | except ImportError, e: |
| 226 | raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e) |
| 227 | try: |
| 228 | mw_class = getattr(mod, mw_classname) |
| 229 | except AttributeError: |
| 230 | raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname) |
| 231 | |
| 232 | try: |
| 233 | mw_instance = mw_class() |
| 234 | except exceptions.MiddlewareNotUsed: |
| 235 | continue |
| 236 | middleware_list.append((middleware_tuple[0], mw_instance)) |
| 237 | return middleware_list |
| 238 | |