Debian Codegraph @

django/contrib/admindocs/middleware.py

raw
1 from django.conf import settings
2 from django.core.exceptions import ImproperlyConfigured
3 from django.http import HttpResponse
4 from django.utils.deprecation import MiddlewareMixin
5
6 from .utils import get_view_name
7
8
9 class XViewMiddleware(MiddlewareMixin):
10 """
11 Add an X-View header to internal HEAD requests.
12 """
13
14 def process_view(self, request, view_func, view_args, view_kwargs):
15 """
16 If the request method is HEAD and either the IP is internal or the
17 user is a logged-in staff member, return a response with an x-view
18 header indicating the view function. This is used to lookup the view
19 function for an arbitrary page.
20 """
21 if not hasattr(request, "user"):
22 raise ImproperlyConfigured(
23 "The XView middleware requires authentication middleware to "
24 "be installed. Edit your MIDDLEWARE setting to insert "
25 "'django.contrib.auth.middleware.AuthenticationMiddleware'."
26 )
27 if request.method == "HEAD" and (
28 request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS
29 or (request.user.is_active and request.user.is_staff)
30 ):
31 response = HttpResponse()
32 response.headers["X-View"] = get_view_name(view_func)
33 return response