Fixed #14506 -- Added an assertion to XViewMiddleware about the dependency on the authentication middleware. Thanks, vanschelven.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16496 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-07-03 17:56:25 +00:00
parent c29e45491e
commit 43cb78fafd
3 changed files with 15 additions and 8 deletions

View File

@ -12,6 +12,10 @@ class XViewMiddleware(object):
indicating the view function. This is used by the documentation module indicating the view function. This is used by the documentation module
to lookup the view function for an arbitrary page. to lookup the view function for an arbitrary page.
""" """
assert hasattr(request, 'user'), (
"The XView middleware requires authentication middleware to be "
"installed. Edit your MIDDLEWARE_CLASSES setting to insert "
"'django.contrib.auth.middleware.AuthenticationMiddleware'.")
if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or
(request.user.is_active and request.user.is_staff)): (request.user.is_active and request.user.is_staff)):
response = http.HttpResponse() response = http.HttpResponse()

View File

@ -80,6 +80,7 @@ View metadata middleware
Sends custom ``X-View`` HTTP headers to HEAD requests that come from IP Sends custom ``X-View`` HTTP headers to HEAD requests that come from IP
addresses defined in the :setting:`INTERNAL_IPS` setting. This is used by addresses defined in the :setting:`INTERNAL_IPS` setting. This is used by
Django's :doc:`automatic documentation system </ref/contrib/admin/admindocs>`. Django's :doc:`automatic documentation system </ref/contrib/admin/admindocs>`.
Depends on :class:`~django.contrib.auth.middleware.AuthenticationMiddleware`.
GZIP middleware GZIP middleware
--------------- ---------------

View File

@ -1,24 +1,26 @@
from django.http import HttpResponse from django.http import HttpResponse
from django.middleware.doc import XViewMiddleware
from django.template import Template, Context from django.template import Template, Context
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.test import TestCase, RequestFactory from django.test import TestCase, RequestFactory
from django.utils.decorators import decorator_from_middleware from django.utils.decorators import decorator_from_middleware
xview_dec = decorator_from_middleware(XViewMiddleware) class ProcessViewMiddleware(object):
def process_view(self, request, view_func, view_args, view_kwargs):
pass
process_view_dec = decorator_from_middleware(ProcessViewMiddleware)
@xview_dec @process_view_dec
def xview(request): def process_view(request):
return HttpResponse() return HttpResponse()
class ClassXView(object): class ClassProcessView(object):
def __call__(self, request): def __call__(self, request):
return HttpResponse() return HttpResponse()
class_xview = xview_dec(ClassXView()) class_process_view = process_view_dec(ClassProcessView())
class FullMiddleware(object): class FullMiddleware(object):
@ -52,13 +54,13 @@ class DecoratorFromMiddlewareTests(TestCase):
""" """
Test a middleware that implements process_view. Test a middleware that implements process_view.
""" """
xview(self.rf.get('/')) process_view(self.rf.get('/'))
def test_callable_process_view_middleware(self): def test_callable_process_view_middleware(self):
""" """
Test a middleware that implements process_view, operating on a callable class. Test a middleware that implements process_view, operating on a callable class.
""" """
class_xview(self.rf.get('/')) class_process_view(self.rf.get('/'))
def test_full_dec_normal(self): def test_full_dec_normal(self):
""" """