Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Updated Middleware Finding Code
Browse files Browse the repository at this point in the history
Improved the middleware matching process to make 
it more efficient.
  • Loading branch information
d0ugal committed Dec 21, 2011
1 parent c7fb4cc commit c94549b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
20 changes: 10 additions & 10 deletions tests/test_urlmiddleware/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class MyProcessRequestMiddleware(object):
def process_request(self, request, *args, **kwargs):
pass

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyProcessRequestMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand All @@ -139,7 +139,7 @@ def process_request(self, request, *args, **kwargs):
from django.http import HttpResponse
return HttpResponse("New Process Request Response")

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyProcessRequestMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand All @@ -160,7 +160,7 @@ class MyProcessViewMiddleware(object):
def process_view(self, request, view, view_args, view_kwargs):
pass

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyProcessViewMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand All @@ -181,7 +181,7 @@ def process_view(self, request, view, view_args, view_kwargs):
from django.http import HttpResponse
return HttpResponse("New Process View Response")

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyProcessViewMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand All @@ -204,7 +204,7 @@ class MyTemplateResnponseViewMiddleware(object):
def process_template_response(self, request, response):
return response

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyTemplateResnponseViewMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand All @@ -231,7 +231,7 @@ class MyTemplateResnponseViewMiddleware(object):
def process_template_response(self, request, response):
return new

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyTemplateResnponseViewMiddleware(), ]

with patch.object(URLMiddleware, 'get_matched_middleware', mock_match):
Expand All @@ -251,7 +251,7 @@ class MyTemplateResnponseViewMiddleware(object):
def process_response(self, request, response):
return response

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyTemplateResnponseViewMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand All @@ -275,7 +275,7 @@ def process_response(self, request, response):
from django.http import HttpResponse
return HttpResponse("New Response")

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyTemplateResnponseViewMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand All @@ -296,7 +296,7 @@ class MyTemplateResnponseViewMiddleware(object):
def process_exception(self, request, exception):
pass

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyTemplateResnponseViewMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand All @@ -318,7 +318,7 @@ def process_exception(self, request, exception):
from django.http import HttpResponse
return HttpResponse("New Response")

def mock_match(self, path):
def mock_match(self, path, middleware_method=None):
return [MyTemplateResnponseViewMiddleware(), ]

request = self.factory.get('/foo/bar/')
Expand Down
59 changes: 33 additions & 26 deletions urlmiddleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class URLMiddleware(object):
classes.
"""

def get_matched_middleware(self, path):
def get_matched_middleware(self, path, middleware_method=None):

middleware_instances = []

Expand All @@ -31,50 +31,57 @@ def get_matched_middleware(self, path):
return []

for middleware_class in middleware_matches:
if not (isclass(middleware_class) or isfunction(middleware_class) \
invalid_error = "%s is expected to be a callable that accepts" \
"no arguements." % middleware_class
if not (isclass(middleware_class) or isfunction(middleware_class)\
or callable(middleware_class)):
raise ImproperlyConfigured("%s is expected to be a callable that accepts no arguements." % middleware_class)
raise ImproperlyConfigured(invalid_error)
try:
middleware_instances.append(middleware_class())
mw_instance = middleware_class()
if middleware_method and not hasattr(mw_instance,
middleware_method):
continue
middleware_instances.append(mw_instance)
except TypeError:
raise ImproperlyConfigured("%s is expected to be a callable that accepts no arguements." % middleware_class)
raise ImproperlyConfigured(invalid_error)

return middleware_instances

def process_request(self, request):
matched_middleware = self.get_matched_middleware(request.path)
matched_middleware = self.get_matched_middleware(request.path,
'process_request')
for middleware in matched_middleware:
if hasattr(middleware, 'process_request'):
response = middleware.process_request(request)
if response:
return response
response = middleware.process_request(request)
if response:
return response

def process_view(self, request, view_func, view_args, view_kwargs):
matched_middleware = self.get_matched_middleware(request.path)
matched_middleware = self.get_matched_middleware(request.path,
'process_view')
for middleware in matched_middleware:
if hasattr(middleware, 'process_view'):
response = middleware.process_view(request, view_func, view_args, view_kwargs)
if response:
return response
response = middleware.process_view(request, view_func, view_args,
view_kwargs)
if response:
return response

def process_template_response(self, request, response):
matched_middleware = self.get_matched_middleware(request.path)
matched_middleware = self.get_matched_middleware(request.path,
'process_template_response')
for middleware in matched_middleware:
if hasattr(middleware, 'process_template_response'):
response = middleware.process_template_response(request, response)
response = middleware.process_template_response(request, response)
return response

def process_response(self, request, response):
matched_middleware = self.get_matched_middleware(request.path)
matched_middleware = self.get_matched_middleware(request.path,
'process_response')
for middleware in matched_middleware:
if hasattr(middleware, 'process_response'):
response = middleware.process_response(request, response)
response = middleware.process_response(request, response)
return response

def process_exception(self, request, exception):
matched_middleware = self.get_matched_middleware(request.path)
matched_middleware = self.get_matched_middleware(request.path,
'process_exception')
for middleware in matched_middleware:
if hasattr(middleware, 'process_exception'):
response = middleware.process_exception(request, exception)
if response:
return response
response = middleware.process_exception(request, exception)
if response:
return response

0 comments on commit c94549b

Please sign in to comment.