~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/core/context_processors.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
"""
9
9
 
10
10
from django.conf import settings
 
11
from django.middleware.csrf import get_token
 
12
from django.utils.functional import lazy
11
13
 
12
14
def auth(request):
13
15
    """
14
 
    Returns context variables required by apps that use Django's authentication
15
 
    system.
16
 
 
17
 
    If there is no 'user' attribute in the request, uses AnonymousUser (from
18
 
    django.contrib.auth).
19
 
    """
20
 
    if hasattr(request, 'user'):
21
 
        user = request.user
22
 
    else:
23
 
        from django.contrib.auth.models import AnonymousUser
24
 
        user = AnonymousUser()
25
 
    return {
26
 
        'user': user,
27
 
        'messages': user.get_and_delete_messages(),
28
 
        'perms': PermWrapper(user),
29
 
    }
 
16
    DEPRECATED. This context processor is the old location, and has been moved
 
17
    to `django.contrib.auth.context_processors`.
 
18
 
 
19
    This function still exists for backwards-compatibility; it will be removed
 
20
    in Django 1.4.
 
21
    """
 
22
    import warnings
 
23
    warnings.warn(
 
24
        "The context processor at `django.core.context_processors.auth` is " \
 
25
        "deprecated; use the path `django.contrib.auth.context_processors.auth` " \
 
26
        "instead.",
 
27
        PendingDeprecationWarning
 
28
    )
 
29
    from django.contrib.auth.context_processors import auth as auth_context_processor
 
30
    return auth_context_processor(request)
 
31
 
 
32
def csrf(request):
 
33
    """
 
34
    Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
 
35
    it has not been provided by either a view decorator or the middleware
 
36
    """
 
37
    def _get_val():
 
38
        token = get_token(request)
 
39
        if token is None:
 
40
            # In order to be able to provide debugging info in the
 
41
            # case of misconfiguration, we use a sentinel value
 
42
            # instead of returning an empty dict.
 
43
            return 'NOTPROVIDED'
 
44
        else:
 
45
            return token
 
46
    _get_val = lazy(_get_val, str)
 
47
 
 
48
    return {'csrf_token': _get_val() }
30
49
 
31
50
def debug(request):
32
51
    "Returns context variables helpful for debugging."
79
98
 
80
99
    def __getitem__(self, module_name):
81
100
        return PermLookupDict(self.user, module_name)
82
 
        
 
101
 
83
102
    def __iter__(self):
84
103
        # I am large, I contain multitudes.
85
104
        raise TypeError("PermWrapper is not iterable.")