| 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 |
|---|
| 11 |
|
|---|
| 12 |
def auth(request): |
|---|
| 13 |
""" |
|---|
| 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 |
} |
|---|
| 30 |
|
|---|
| 31 |
def debug(request): |
|---|
| 32 |
"Returns context variables helpful for debugging." |
|---|
| 33 |
context_extras = {} |
|---|
| 34 |
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: |
|---|
| 35 |
context_extras['debug'] = True |
|---|
| 36 |
from django.db import connection |
|---|
| 37 |
context_extras['sql_queries'] = connection.queries |
|---|
| 38 |
return context_extras |
|---|
| 39 |
|
|---|
| 40 |
def i18n(request): |
|---|
| 41 |
from django.utils import translation |
|---|
| 42 |
|
|---|
| 43 |
context_extras = {} |
|---|
| 44 |
context_extras['LANGUAGES'] = settings.LANGUAGES |
|---|
| 45 |
context_extras['LANGUAGE_CODE'] = translation.get_language() |
|---|
| 46 |
context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi() |
|---|
| 47 |
|
|---|
| 48 |
return context_extras |
|---|
| 49 |
|
|---|
| 50 |
def media(request): |
|---|
| 51 |
""" |
|---|
| 52 |
Adds media-related context variables to the context. |
|---|
| 53 |
|
|---|
| 54 |
""" |
|---|
| 55 |
return {'MEDIA_URL': settings.MEDIA_URL} |
|---|
| 56 |
|
|---|
| 57 |
def request(request): |
|---|
| 58 |
return {'request': request} |
|---|
| 59 |
|
|---|
| 60 |
# PermWrapper and PermLookupDict proxy the permissions system into objects that |
|---|
| 61 |
# the template system can understand. |
|---|
| 62 |
|
|---|
| 63 |
class PermLookupDict(object): |
|---|
| 64 |
def __init__(self, user, module_name): |
|---|
| 65 |
self.user, self.module_name = user, module_name |
|---|
| 66 |
|
|---|
| 67 |
def __repr__(self): |
|---|
| 68 |
return str(self.user.get_all_permissions()) |
|---|
| 69 |
|
|---|
| 70 |
def __getitem__(self, perm_name): |
|---|
| 71 |
return self.user.has_perm("%s.%s" % (self.module_name, perm_name)) |
|---|
| 72 |
|
|---|
| 73 |
def __nonzero__(self): |
|---|
| 74 |
return self.user.has_module_perms(self.module_name) |
|---|
| 75 |
|
|---|
| 76 |
class PermWrapper(object): |
|---|
| 77 |
def __init__(self, user): |
|---|
| 78 |
self.user = user |
|---|
| 79 |
|
|---|
| 80 |
def __getitem__(self, module_name): |
|---|
| 81 |
return PermLookupDict(self.user, module_name) |
|---|