2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
|
|
|
_standard_context_processors = None
|
|
|
|
|
|
|
|
class ContextPopException(Exception):
|
|
|
|
"pop() has been called more times than push()"
|
|
|
|
pass
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class Context(object):
|
2006-05-02 09:31:56 +08:00
|
|
|
"A stack container for variable context"
|
2007-11-14 20:58:53 +08:00
|
|
|
def __init__(self, dict_=None, autoescape=True):
|
2006-05-02 09:31:56 +08:00
|
|
|
dict_ = dict_ or {}
|
|
|
|
self.dicts = [dict_]
|
2007-11-14 20:58:53 +08:00
|
|
|
self.autoescape = autoescape
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.dicts)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for d in self.dicts:
|
|
|
|
yield d
|
|
|
|
|
|
|
|
def push(self):
|
2007-12-03 07:57:22 +08:00
|
|
|
d = {}
|
|
|
|
self.dicts = [d] + self.dicts
|
|
|
|
return d
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def pop(self):
|
|
|
|
if len(self.dicts) == 1:
|
|
|
|
raise ContextPopException
|
2007-12-03 07:57:22 +08:00
|
|
|
return self.dicts.pop(0)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
"Set a variable in the current context"
|
|
|
|
self.dicts[0][key] = value
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
"Get a variable's value, starting at the current context and going upward"
|
|
|
|
for d in self.dicts:
|
2007-04-26 21:30:48 +08:00
|
|
|
if key in d:
|
2006-05-02 09:31:56 +08:00
|
|
|
return d[key]
|
2006-07-04 11:21:44 +08:00
|
|
|
raise KeyError(key)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
"Delete a variable from the current context"
|
|
|
|
del self.dicts[0][key]
|
|
|
|
|
|
|
|
def has_key(self, key):
|
|
|
|
for d in self.dicts:
|
2007-04-26 21:30:48 +08:00
|
|
|
if key in d:
|
2006-05-02 09:31:56 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2007-09-11 22:04:40 +08:00
|
|
|
__contains__ = has_key
|
2007-02-10 17:02:00 +08:00
|
|
|
|
2006-07-04 11:21:44 +08:00
|
|
|
def get(self, key, otherwise=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
for d in self.dicts:
|
2007-04-26 21:30:48 +08:00
|
|
|
if key in d:
|
2006-05-02 09:31:56 +08:00
|
|
|
return d[key]
|
|
|
|
return otherwise
|
|
|
|
|
|
|
|
def update(self, other_dict):
|
|
|
|
"Like dict.update(). Pushes an entire dictionary's keys and values onto the context."
|
|
|
|
self.dicts = [other_dict] + self.dicts
|
2007-12-03 07:57:22 +08:00
|
|
|
return other_dict
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# This is a function rather than module-level procedural code because we only
|
|
|
|
# want it to execute if somebody uses RequestContext.
|
|
|
|
def get_standard_processors():
|
|
|
|
global _standard_context_processors
|
|
|
|
if _standard_context_processors is None:
|
|
|
|
processors = []
|
|
|
|
for path in settings.TEMPLATE_CONTEXT_PROCESSORS:
|
|
|
|
i = path.rfind('.')
|
|
|
|
module, attr = path[:i], path[i+1:]
|
|
|
|
try:
|
2006-10-31 04:50:27 +08:00
|
|
|
mod = __import__(module, {}, {}, [attr])
|
2006-05-02 09:31:56 +08:00
|
|
|
except ImportError, e:
|
2007-12-23 03:32:54 +08:00
|
|
|
raise ImproperlyConfigured('Error importing request processor module %s: "%s"' % (module, e))
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
|
|
|
func = getattr(mod, attr)
|
|
|
|
except AttributeError:
|
2007-12-23 03:32:54 +08:00
|
|
|
raise ImproperlyConfigured('Module "%s" does not define a "%s" callable request processor' % (module, attr))
|
2006-05-02 09:31:56 +08:00
|
|
|
processors.append(func)
|
|
|
|
_standard_context_processors = tuple(processors)
|
|
|
|
return _standard_context_processors
|
|
|
|
|
|
|
|
class RequestContext(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, processors=None):
|
|
|
|
Context.__init__(self, dict)
|
|
|
|
if processors is None:
|
|
|
|
processors = ()
|
|
|
|
else:
|
|
|
|
processors = tuple(processors)
|
|
|
|
for processor in get_standard_processors() + processors:
|
|
|
|
self.update(processor(request))
|