Ticket #7299: django_xview_middleware.diff
File django_xview_middleware.diff, 2.6 KB (added by , 16 years ago) |
---|
-
django/middleware/doc.py
12 12 indicating the view function. This is used by the documentation module 13 13 to lookup the view function for an arbitrary page. 14 14 """ 15 if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or ( request.user.is_authenticated() and request.user.is_staff)):15 if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or (hasattr(request, 'user') and request.user.is_authenticated() and request.user.is_staff)): 16 16 response = http.HttpResponse() 17 17 response['X-View'] = "%s.%s" % (view_func.__module__, view_func.__name__) 18 18 return response -
tests/regressiontests/middleware/tests.py
3 3 from django.test import TestCase 4 4 from django.http import HttpRequest 5 5 from django.middleware.common import CommonMiddleware 6 from django.middleware.doc import XViewMiddleware 7 from django.views.generic.simple import direct_to_template 6 8 from django.conf import settings 9 from django.conf import global_settings 7 10 8 11 class CommonMiddlewareTest(TestCase): 9 12 def _get_request(self, path): … … 89 92 self.assertEquals( 90 93 r['Location'], 91 94 'http://testserver/middleware/needsquoting%23/') 95 96 97 class XViewMiddlewareTest(TestCase): 98 def tearDown(self): 99 settings.INTERNAL_IPS = global_settings.INTERNAL_IPS 100 101 def test_internal_ips(self): 102 """ 103 Tests that request method is HEAD and the IP is internal. 104 """ 105 settings.INTERNAL_IPS = ('127.0.0.1',) 106 request = HttpRequest() 107 request.method, request.path = 'HEAD', "/middleware/xview" 108 109 request.META = { 110 'SERVER_NAME': 'testserver', 111 'SERVER_PORT': 80, 112 'REMOTE_ADDR': settings.INTERNAL_IPS[0], 113 } 114 response = XViewMiddleware().process_view(request, direct_to_template, (), {}) 115 self.assertEquals(response.status_code, 200) 116 self.assertEquals(response['X-View'], 'django.views.generic.simple.direct_to_template') 117 118 # No INTERNAL_IPS 119 request.META['REMOTE_ADDR'] = '127.0.0.2' 120 response = XViewMiddleware().process_view(request, direct_to_template, (), {}) 121 self.assert_(response is None)