Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.py 1.3KB

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