diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2005-12-24 04:39:59 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2005-12-24 04:39:59 +0000 |
| commit | 49fd163a95074c07a23f2ccf9e23aebf5bee0bb2 (patch) | |
| tree | 78ca0957bfc75f190e34d218c7d0eba33afb5d49 /django/core | |
| parent | d9269055c941bd864049207441d7955ec65760fe (diff) | |
Fixed #925 -- Added TEMPLATE_CONTEXT_PROCESSORS, which lets you specify processesors for DjangoContext. Thanks, Luke Plant
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1773 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core')
| -rw-r--r-- | django/core/extensions.py | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/django/core/extensions.py b/django/core/extensions.py index 6bd24c4dbf..c03f63addc 100644 --- a/django/core/extensions.py +++ b/django/core/extensions.py @@ -2,11 +2,34 @@ # of MVC. In other words, these functions/classes introduce controlled coupling # for convenience's sake. -from django.core.exceptions import Http404, ObjectDoesNotExist +from django.core.exceptions import Http404, ImproperlyConfigured, ObjectDoesNotExist from django.core.template import Context, loader -from django.conf.settings import DEBUG, INTERNAL_IPS +from django.conf.settings import TEMPLATE_CONTEXT_PROCESSORS from django.utils.httpwrappers import HttpResponse +_standard_context_processors = None + +# This is a function rather than module-level procedural code because we only +# want it to execute if somebody uses DjangoContext. +def get_standard_processors(): + global _standard_context_processors + if _standard_context_processors is None: + processors = [] + for path in TEMPLATE_CONTEXT_PROCESSORS: + i = path.rfind('.') + module, attr = path[:i], path[i+1:] + try: + mod = __import__(module, '', '', [attr]) + except ImportError, e: + raise ImproperlyConfigured, 'Error importing request processor module %s: "%s"' % (module, e) + try: + func = getattr(mod, attr) + except AttributeError: + raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable request processor' % (module, attr) + processors.append(func) + _standard_context_processors = tuple(processors) + return _standard_context_processors + def render_to_response(*args, **kwargs): return HttpResponse(loader.render_to_string(*args, **kwargs)) load_and_render = render_to_response # For backwards compatibility. @@ -25,24 +48,19 @@ def get_list_or_404(mod, **kwargs): class DjangoContext(Context): """ - This subclass of template.Context automatically populates 'user' and - 'messages' in the context. + This subclass of template.Context automatically populates itself using + the processors defined in TEMPLATE_CONTEXT_PROCESSORS. + Additional processors can be specified as a list of callables + using the "processors" keyword argument. """ - def __init__(self, request, dict=None): + def __init__(self, request, dict=None, processors=None): Context.__init__(self, dict) - self['user'] = request.user - self['messages'] = request.user.get_and_delete_messages() - self['perms'] = PermWrapper(request.user) - from django.conf import settings - self['LANGUAGES'] = settings.LANGUAGES - if hasattr(request, 'LANGUAGE_CODE'): - self['LANGUAGE_CODE'] = request.LANGUAGE_CODE + if processors is None: + processors = () else: - self['LANGUAGE_CODE'] = settings.LANGUAGE_CODE - if DEBUG and request.META.get('REMOTE_ADDR') in INTERNAL_IPS: - self['debug'] = True - from django.core import db - self['sql_queries'] = db.db.queries + processors = tuple(processors) + for processor in get_standard_processors() + processors: + self.update(processor(request)) # PermWrapper and PermLookupDict proxy the permissions system into objects that # the template system can understand. |
