|
0
|
1 |
""" |
|
|
2 |
A set of request processors that return dictionaries to be merged into a |
|
|
3 |
template context. Each function takes the request object as its only parameter |
|
|
4 |
and returns a dictionary to add to the context. |
|
|
5 |
|
|
|
6 |
These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by |
|
|
7 |
RequestContext. |
|
|
8 |
""" |
|
|
9 |
|
|
|
10 |
from django.conf import settings |
|
29
|
11 |
from django.middleware.csrf import get_token |
|
|
12 |
from django.utils.functional import lazy |
|
0
|
13 |
|
|
|
14 |
def auth(request): |
|
|
15 |
""" |
|
29
|
16 |
DEPRECATED. This context processor is the old location, and has been moved |
|
|
17 |
to `django.contrib.auth.context_processors`. |
|
0
|
18 |
|
|
29
|
19 |
This function still exists for backwards-compatibility; it will be removed |
|
|
20 |
in Django 1.4. |
|
0
|
21 |
""" |
|
29
|
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() } |
|
0
|
49 |
|
|
|
50 |
def debug(request): |
|
|
51 |
"Returns context variables helpful for debugging." |
|
|
52 |
context_extras = {} |
|
|
53 |
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: |
|
|
54 |
context_extras['debug'] = True |
|
|
55 |
from django.db import connection |
|
|
56 |
context_extras['sql_queries'] = connection.queries |
|
|
57 |
return context_extras |
|
|
58 |
|
|
|
59 |
def i18n(request): |
|
|
60 |
from django.utils import translation |
|
|
61 |
|
|
|
62 |
context_extras = {} |
|
|
63 |
context_extras['LANGUAGES'] = settings.LANGUAGES |
|
|
64 |
context_extras['LANGUAGE_CODE'] = translation.get_language() |
|
|
65 |
context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi() |
|
|
66 |
|
|
|
67 |
return context_extras |
|
|
68 |
|
|
|
69 |
def media(request): |
|
|
70 |
""" |
|
|
71 |
Adds media-related context variables to the context. |
|
|
72 |
|
|
|
73 |
""" |
|
|
74 |
return {'MEDIA_URL': settings.MEDIA_URL} |
|
|
75 |
|
|
|
76 |
def request(request): |
|
|
77 |
return {'request': request} |
|
|
78 |
|
|
|
79 |
# PermWrapper and PermLookupDict proxy the permissions system into objects that |
|
|
80 |
# the template system can understand. |
|
|
81 |
|
|
|
82 |
class PermLookupDict(object): |
|
|
83 |
def __init__(self, user, module_name): |
|
|
84 |
self.user, self.module_name = user, module_name |
|
|
85 |
|
|
|
86 |
def __repr__(self): |
|
|
87 |
return str(self.user.get_all_permissions()) |
|
|
88 |
|
|
|
89 |
def __getitem__(self, perm_name): |
|
|
90 |
return self.user.has_perm("%s.%s" % (self.module_name, perm_name)) |
|
|
91 |
|
|
|
92 |
def __nonzero__(self): |
|
|
93 |
return self.user.has_module_perms(self.module_name) |
|
|
94 |
|
|
|
95 |
class PermWrapper(object): |
|
|
96 |
def __init__(self, user): |
|
|
97 |
self.user = user |
|
|
98 |
|
|
|
99 |
def __getitem__(self, module_name): |
|
|
100 |
return PermLookupDict(self.user, module_name) |
|
29
|
101 |
|
|
0
|
102 |
def __iter__(self): |
|
|
103 |
# I am large, I contain multitudes. |
|
|
104 |
raise TypeError("PermWrapper is not iterable.") |