Ticket #730: middleware_dict__ticket_730__rev_8168.patch

File middleware_dict__ticket_730__rev_8168.patch, 7.5 KB (added by Ben Slavin, 16 years ago)
  • django/conf/project_template/settings.py

     
    5757#     'django.template.loaders.eggs.load_template_source',
    5858)
    5959
    60 MIDDLEWARE_CLASSES = (
    61     'django.middleware.common.CommonMiddleware',
    62     'django.contrib.sessions.middleware.SessionMiddleware',
    63     'django.contrib.auth.middleware.AuthenticationMiddleware',
    64     'django.middleware.doc.XViewMiddleware',
    65 )
     60MIDDLEWARE_CLASSES = {
     61    'request': (
     62        'django.middleware.common.CommonMiddleware',
     63        'django.contrib.sessions.middleware.SessionMiddleware',
     64        'django.contrib.auth.middleware.AuthenticationMiddleware',
     65    ),
     66    'response': (
     67        'django.contrib.sessions.middleware.SessionMiddleware',
     68#        'django.middleware.http.ConditionalGetMiddleware',
     69#        'django.middleware.gzip.GZipMiddleware',
     70        'django.middleware.common.CommonMiddleware',
     71    ),
     72    'view': (
     73        'django.middleware.doc.XViewMiddleware',
     74    ),
     75}
    6676
    6777ROOT_URLCONF = '{{ project_name }}.urls'
    6878
  • django/conf/global_settings.py

     
    288288# MIDDLEWARE #
    289289##############
    290290
    291 # List of middleware classes to use.  Order is important; in the request phase,
    292 # this middleware classes will be applied in the order given, and in the
    293 # response phase the middleware will be applied in reverse order.
    294 MIDDLEWARE_CLASSES = (
    295     'django.contrib.sessions.middleware.SessionMiddleware',
    296     'django.contrib.auth.middleware.AuthenticationMiddleware',
    297 #     'django.middleware.http.ConditionalGetMiddleware',
    298 #     'django.middleware.gzip.GZipMiddleware',
    299     'django.middleware.common.CommonMiddleware',
    300     'django.middleware.doc.XViewMiddleware',
    301 )
     291# List of middleware classes to use.  Order is important; all middleware types
     292# will be applied in the order given.
     293MIDDLEWARE_CLASSES = {
     294    'request': (
     295        'django.middleware.common.CommonMiddleware',
     296        'django.contrib.sessions.middleware.SessionMiddleware',
     297        'django.contrib.auth.middleware.AuthenticationMiddleware',
     298    ),
     299    'response': (
     300        'django.contrib.sessions.middleware.SessionMiddleware',
     301#        'django.middleware.http.ConditionalGetMiddleware',
     302#        'django.middleware.gzip.GZipMiddleware',
     303        'django.middleware.common.CommonMiddleware',
     304    ),
     305    'view': (
     306        'django.middleware.doc.XViewMiddleware',
     307    ),
     308}
    302309
    303310############
    304311# SESSIONS #
  • django/core/handlers/base.py

     
    11import sys
    22
    33from django import http
     4from django.core import exceptions
    45from django.core import signals
    56from django.dispatch import dispatcher
    67from django.utils.encoding import force_unicode
     
    1718    def __init__(self):
    1819        self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None
    1920
    20     def load_middleware(self):
    21         """
    22         Populate middleware lists from settings.MIDDLEWARE_CLASSES.
     21    def middleware_class_from_path(self, middleware_path):
     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
    2336
    24         Must be called after the environment is fixed (see __call__).
    25         """
    26         from django.conf import settings
    27         from django.core import exceptions
    28         self._request_middleware = []
    29         self._view_middleware = []
    30         self._response_middleware = []
    31         self._exception_middleware = []
    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)
     37    def middleware_from_list(self, middleware_classes):
     38        mw_list = []
     39        for middleware_path in middleware_classes:
     40            mw_class = self.middleware_class_from_path(middleware_path)
    4641
    4742            try:
    4843                mw_instance = mw_class()
    4944            except exceptions.MiddlewareNotUsed:
    5045                continue
     46            else:
     47                mw_list.append(mw_instance)
     48        return mw_list
    5149
     50    def _load_middleware_legacy(self, mw_class_list):
     51        mw_list = self.middleware_from_list(mw_class_list)
     52
     53        for mw_instance in mw_list:
    5254            if hasattr(mw_instance, 'process_request'):
    5355                self._request_middleware.append(mw_instance.process_request)
    5456            if hasattr(mw_instance, 'process_view'):
     
    5860            if hasattr(mw_instance, 'process_exception'):
    5961                self._exception_middleware.insert(0, mw_instance.process_exception)
    6062
     63    def load_middleware(self):
     64        """
     65        Populate middleware lists from settings.MIDDLEWARE_CLASSES.
     66
     67        Must be called after the environment is fixed (see __call__).
     68        """
     69        from django.conf import settings
     70        self._request_middleware = []
     71        self._view_middleware = []
     72        self._response_middleware = []
     73        self._exception_middleware = []
     74
     75        MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
     76
     77        if isinstance(MIDDLEWARE_CLASSES, (tuple, list)):
     78            self._load_middleware_legacy(MIDDLEWARE_CLASSES)
     79        else:
     80            self._request_middleware = self.middleware_from_list(MIDDLEWARE_CLASSES.get('request', []))
     81            self._view_middleware = self.middleware_from_list(MIDDLEWARE_CLASSES.get('view', []))
     82            self._response_middleware = self.middleware_from_list(MIDDLEWARE_CLASSES.get('response', []))
     83            self._exception_middleware = self.middleware_from_list(MIDDLEWARE_CLASSES.get('exception', []))
     84
    6185    def get_response(self, request):
    6286        "Returns an HttpResponse object for the given HttpRequest"
    63         from django.core import exceptions, urlresolvers
     87        from django.core import urlresolvers
    6488        from django.conf import settings
    6589
    6690        # Apply request middleware
Back to Top