web/lib/django/middleware/doc.py
author ymh <ymh.work@gmail.com>
Thu, 05 Aug 2010 17:28:09 +0200
changeset 50 012451a812f1
parent 38 77b6da96e6f1
permissions -rw-r--r--
Merge with a2711e44ba5de8b1675d7e0ee6aaa4a6c56a9b46
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
from django.conf import settings
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from django import http
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
class XViewMiddleware(object):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
    Adds an X-View header to internal HEAD requests -- used by the documentation system.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
    def process_view(self, request, view_func, view_args, view_kwargs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
        """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
        If the request method is HEAD and either the IP is internal or the
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
        user is a logged-in staff member, quickly return with an x-header
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
        indicating the view function.  This is used by the documentation module
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
        to lookup the view function for an arbitrary page.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
        """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
        if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
                                         (request.user.is_active and request.user.is_staff)):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
            response = http.HttpResponse()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
            response['X-View'] = "%s.%s" % (view_func.__module__, view_func.__name__)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
            return response