app/django/core/xheaders.py
author Todd Larsen <tlarsen@google.com>
Fri, 18 Jul 2008 18:38:41 +0000
changeset 57 e30229fd9add
parent 54 03e267d67478
permissions -rw-r--r--
HTML mockups of proposed Melange (generic), GSoC, and GHOP pages and forms. The HTML files in this directory are formatted to be viewed directly from svn via http://soc.googlecode.com/svn/trunk/mockups/. All of the paths will be relative (rooted at /svn/trunk/mockups/) and instructions will be added (in a mockups/README file) for configuring an Apache web server for previewing the pages from the svn working copy during development.

"""
Pages in Django can are served up with custom HTTP headers containing useful
information about those pages -- namely, the content type and object ID.

This module contains utility functions for retrieving and doing interesting
things with these special "X-Headers" (so called because the HTTP spec demands
that custom headers are prefixed with "X-").

Next time you're at slashdot.org, watch out for X-Fry and X-Bender. :)
"""

def populate_xheaders(request, response, model, object_id):
    """
    Adds the "X-Object-Type" and "X-Object-Id" headers to the given
    HttpResponse according to the given model and object_id -- but only if the
    given HttpRequest object has an IP address within the INTERNAL_IPS setting
    or if the request is from a logged in staff member.
    """
    from django.conf import settings
    if (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
            or (hasattr(request, 'user') and request.user.is_authenticated()
                and request.user.is_staff)):
        response['X-Object-Type'] = "%s.%s" % (model._meta.app_label, model._meta.object_name.lower())
        response['X-Object-Id'] = str(object_id)