6 These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by |
6 These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by |
7 RequestContext. |
7 RequestContext. |
8 """ |
8 """ |
9 |
9 |
10 from django.conf import settings |
10 from django.conf import settings |
|
11 from django.middleware.csrf import get_token |
|
12 from django.utils.functional import lazy |
11 |
13 |
12 def auth(request): |
14 def auth(request): |
13 """ |
15 """ |
14 Returns context variables required by apps that use Django's authentication |
16 DEPRECATED. This context processor is the old location, and has been moved |
15 system. |
17 to `django.contrib.auth.context_processors`. |
16 |
18 |
17 If there is no 'user' attribute in the request, uses AnonymousUser (from |
19 This function still exists for backwards-compatibility; it will be removed |
18 django.contrib.auth). |
20 in Django 1.4. |
19 """ |
21 """ |
20 if hasattr(request, 'user'): |
22 import warnings |
21 user = request.user |
23 warnings.warn( |
22 else: |
24 "The context processor at `django.core.context_processors.auth` is " \ |
23 from django.contrib.auth.models import AnonymousUser |
25 "deprecated; use the path `django.contrib.auth.context_processors.auth` " \ |
24 user = AnonymousUser() |
26 "instead.", |
25 return { |
27 PendingDeprecationWarning |
26 'user': user, |
28 ) |
27 'messages': user.get_and_delete_messages(), |
29 from django.contrib.auth.context_processors import auth as auth_context_processor |
28 'perms': PermWrapper(user), |
30 return auth_context_processor(request) |
29 } |
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 def debug(request): |
50 def debug(request): |
32 "Returns context variables helpful for debugging." |
51 "Returns context variables helpful for debugging." |
33 context_extras = {} |
52 context_extras = {} |
34 if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: |
53 if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: |
77 def __init__(self, user): |
96 def __init__(self, user): |
78 self.user = user |
97 self.user = user |
79 |
98 |
80 def __getitem__(self, module_name): |
99 def __getitem__(self, module_name): |
81 return PermLookupDict(self.user, module_name) |
100 return PermLookupDict(self.user, module_name) |
82 |
101 |
83 def __iter__(self): |
102 def __iter__(self): |
84 # I am large, I contain multitudes. |
103 # I am large, I contain multitudes. |
85 raise TypeError("PermWrapper is not iterable.") |
104 raise TypeError("PermWrapper is not iterable.") |