Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added missing context_processors module from [1773]

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1774 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b28e5e413332ac2becb9f475367783b94db889fc 1 parent 49fd163
@adrianholovaty adrianholovaty authored
Showing with 58 additions and 0 deletions.
  1. +58 −0 django/core/context_processors.py
View
58 django/core/context_processors.py
@@ -0,0 +1,58 @@
+"""
+A set of request processors that return dictionaries to be merged into a
+template context. Each function takes the request object as its only parameter
+and returns a dictionary to add to the context.
+
+These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by
+DjangoContext.
+"""
+
+from django.conf.settings import DEBUG, INTERNAL_IPS, LANGUAGES, LANGUAGE_CODE
+
+def auth(request):
+ """
+ Returns context variables required by apps that use Django's authentication
+ system.
+ """
+ return {
+ 'user': request.user,
+ 'messages': request.user.get_and_delete_messages(),
+ 'perms': PermWrapper(request.user),
+ }
+
+def debug(request):
+ "Returns context variables helpful for debugging."
+ context_extras = {}
+ if DEBUG and request.META.get('REMOTE_ADDR') in INTERNAL_IPS:
+ context_extras['debug'] = True
+ from django.core import db
+ context_extras['sql_queries'] = db.db.queries
+ return context_extras
+
+def i18n(request):
+ context_extras = {}
+ context_extras['LANGUAGES'] = LANGUAGES
+ if hasattr(request, 'LANGUAGE_CODE'):
+ context_extras['LANGUAGE_CODE'] = request.LANGUAGE_CODE
+ else:
+ context_extras['LANGUAGE_CODE'] = LANGUAGE_CODE
+ return context_extras
+
+# PermWrapper and PermLookupDict proxy the permissions system into objects that
+# the template system can understand.
+
+class PermLookupDict:
+ def __init__(self, user, module_name):
+ self.user, self.module_name = user, module_name
+ def __repr__(self):
+ return str(self.user.get_permission_list())
+ def __getitem__(self, perm_name):
+ return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
+ def __nonzero__(self):
+ return self.user.has_module_perms(self.module_name)
+
+class PermWrapper:
+ def __init__(self, user):
+ self.user = user
+ def __getitem__(self, module_name):
+ return PermLookupDict(self.user, module_name)
Please sign in to comment.
Something went wrong with that request. Please try again.