Ticket #4933: xheaders.py.diff
File xheaders.py.diff, 1.7 KB (added by , 17 years ago) |
---|
-
django/core/xheaders.py
1 from django.conf import settings 2 1 3 """ 2 4 Pages in Django can are served up with custom HTTP headers containing useful 3 5 information about those pages -- namely, the content type and object ID. … … 4 6 5 7 This module contains utility functions for retrieving and doing interesting 6 8 things with these special "X-Headers" (so called because the HTTP spec demands 7 that custom headers are pref xed with "X-").9 that custom headers are prefixed with "X-".) 8 10 9 11 Next time you're at slashdot.org, watch out for X-Fry and X-Bender. :) 10 12 """ … … 16 18 given HttpRequest object has an IP address within the INTERNAL_IPS setting 17 19 or if the request is from a logged in staff member. 18 20 """ 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()) 22 37 response['X-Object-Id'] = str(object_id)