Ticket #4933: xheaders.py.diff

File xheaders.py.diff, 1.7 KB (added by Ludvig Ericson <ludvig.ericson@…>, 17 years ago)
  • django/core/xheaders.py

     
     1from django.conf import settings
     2
    13"""
    24Pages in Django can are served up with custom HTTP headers containing useful
    35information about those pages -- namely, the content type and object ID.
     
    46
    57This module contains utility functions for retrieving and doing interesting
    68things with these special "X-Headers" (so called because the HTTP spec demands
    7 that custom headers are prefxed with "X-").
     9that custom headers are prefixed with "X-".)
    810
    911Next time you're at slashdot.org, watch out for X-Fry and X-Bender. :)
    1012"""
     
    1618    given HttpRequest object has an IP address within the INTERNAL_IPS setting
    1719    or if the request is from a logged in staff member.
    1820    """
    19     from django.conf import settings
    20     if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or (hasattr(request, 'user') and request.user.is_authenticated() and request.user.is_staff):
    21         response['X-Object-Type'] = "%s.%s" % (model._meta.app_label, model._meta.object_name.lower())
     21    is_internal_ip = is_staff = False
     22
     23    try:
     24        is_internal_ip = request.META['REMOTE_ADDR'] in settings.INTERNAL_IPS
     25    except KeyError:
     26        pass
     27
     28    try:
     29        is_staff = request.user.is_authenticated() and request.user.is_staff
     30    except AttributeError:
     31        pass
     32
     33    if is_internal_ip or is_staff:
     34        response['X-Object-Type'] = u"%s.%s" % (
     35            model._meta.app_label,
     36            model._meta.object_name.lower())
    2237        response['X-Object-Id'] = str(object_id)
Back to Top