Code

Ticket #4933: xheaders.py.diff

File xheaders.py.diff, 1.7 KB (added by Ludvig Ericson <ludvig.ericson@…>, 7 years ago)
Line 
1Index: django/core/xheaders.py
2===================================================================
3--- django/core/xheaders.py     (revision 5724)
4+++ django/core/xheaders.py     (working copy)
5@@ -1,3 +1,5 @@
6+from django.conf import settings
7+
8 """
9 Pages in Django can are served up with custom HTTP headers containing useful
10 information about those pages -- namely, the content type and object ID.
11@@ -4,7 +6,7 @@
12 
13 This module contains utility functions for retrieving and doing interesting
14 things with these special "X-Headers" (so called because the HTTP spec demands
15-that custom headers are prefxed with "X-").
16+that custom headers are prefixed with "X-".)
17 
18 Next time you're at slashdot.org, watch out for X-Fry and X-Bender. :)
19 """
20@@ -16,7 +18,20 @@
21     given HttpRequest object has an IP address within the INTERNAL_IPS setting
22     or if the request is from a logged in staff member.
23     """
24-    from django.conf import settings
25-    if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or (hasattr(request, 'user') and request.user.is_authenticated() and request.user.is_staff):
26-        response['X-Object-Type'] = "%s.%s" % (model._meta.app_label, model._meta.object_name.lower())
27+    is_internal_ip = is_staff = False
28+
29+    try:
30+        is_internal_ip = request.META['REMOTE_ADDR'] in settings.INTERNAL_IPS
31+    except KeyError:
32+        pass
33+
34+    try:
35+        is_staff = request.user.is_authenticated() and request.user.is_staff
36+    except AttributeError:
37+        pass
38+
39+    if is_internal_ip or is_staff:
40+        response['X-Object-Type'] = u"%s.%s" % (
41+            model._meta.app_label,
42+            model._meta.object_name.lower())
43         response['X-Object-Id'] = str(object_id)