Django

Code

root/django/trunk/django/template/context.py

Revision 10209, 3.5 kB (checked in by gwilson, 3 months ago)

Fixed #10079 -- Moved settings import inside the function that uses it so that the module can be imported without needing settings, thanks kcarnold and mcroydon.

  • Property svn:eol-style set to native
Line 
1 from django.core.exceptions import ImproperlyConfigured
2 from django.utils.importlib import import_module
3
4 _standard_context_processors = None
5
6 class ContextPopException(Exception):
7     "pop() has been called more times than push()"
8     pass
9
10 class Context(object):
11     "A stack container for variable context"
12     def __init__(self, dict_=None, autoescape=True):
13         dict_ = dict_ or {}
14         self.dicts = [dict_]
15         self.autoescape = autoescape
16
17     def __repr__(self):
18         return repr(self.dicts)
19
20     def __iter__(self):
21         for d in self.dicts:
22             yield d
23
24     def push(self):
25         d = {}
26         self.dicts = [d] + self.dicts
27         return d
28
29     def pop(self):
30         if len(self.dicts) == 1:
31             raise ContextPopException
32         return self.dicts.pop(0)
33
34     def __setitem__(self, key, value):
35         "Set a variable in the current context"
36         self.dicts[0][key] = value
37
38     def __getitem__(self, key):
39         "Get a variable's value, starting at the current context and going upward"
40         for d in self.dicts:
41             if key in d:
42                 return d[key]
43         raise KeyError(key)
44
45     def __delitem__(self, key):
46         "Delete a variable from the current context"
47         del self.dicts[0][key]
48
49     def has_key(self, key):
50         for d in self.dicts:
51             if key in d:
52                 return True
53         return False
54
55     __contains__ = has_key
56
57     def get(self, key, otherwise=None):
58         for d in self.dicts:
59             if key in d:
60                 return d[key]
61         return otherwise
62
63     def update(self, other_dict):
64         "Like dict.update(). Pushes an entire dictionary's keys and values onto the context."
65         if not hasattr(other_dict, '__getitem__'):
66             raise TypeError('other_dict must be a mapping (dictionary-like) object.')
67         self.dicts = [other_dict] + self.dicts
68         return other_dict
69
70 # This is a function rather than module-level procedural code because we only
71 # want it to execute if somebody uses RequestContext.
72 def get_standard_processors():
73     from django.conf import settings
74     global _standard_context_processors
75     if _standard_context_processors is None:
76         processors = []
77         for path in settings.TEMPLATE_CONTEXT_PROCESSORS:
78             i = path.rfind('.')
79             module, attr = path[:i], path[i+1:]
80             try:
81                 mod = import_module(module)
82             except ImportError, e:
83                 raise ImproperlyConfigured('Error importing request processor module %s: "%s"' % (module, e))
84             try:
85                 func = getattr(mod, attr)
86             except AttributeError:
87                 raise ImproperlyConfigured('Module "%s" does not define a "%s" callable request processor' % (module, attr))
88             processors.append(func)
89         _standard_context_processors = tuple(processors)
90     return _standard_context_processors
91
92 class RequestContext(Context):
93     """
94     This subclass of template.Context automatically populates itself using
95     the processors defined in TEMPLATE_CONTEXT_PROCESSORS.
96     Additional processors can be specified as a list of callables
97     using the "processors" keyword argument.
98     """
99     def __init__(self, request, dict=None, processors=None):
100         Context.__init__(self, dict)
101         if processors is None:
102             processors = ()
103         else:
104             processors = tuple(processors)
105         for processor in get_standard_processors() + processors:
106             self.update(processor(request))
Note: See TracBrowser for help on using the browser.