|
1 from django.core.exceptions import ImproperlyConfigured |
|
2 from django.utils.importlib import import_module |
|
3 |
|
4 # Cache of actual callables. |
|
5 _standard_context_processors = None |
|
6 # We need the CSRF processor no matter what the user has in their settings, |
|
7 # because otherwise it is a security vulnerability, and we can't afford to leave |
|
8 # this to human error or failure to read migration instructions. |
|
9 _builtin_context_processors = ('django.core.context_processors.csrf',) |
|
10 |
|
11 class ContextPopException(Exception): |
|
12 "pop() has been called more times than push()" |
|
13 pass |
|
14 |
|
15 class BaseContext(object): |
|
16 def __init__(self, dict_=None): |
|
17 dict_ = dict_ or {} |
|
18 self.dicts = [dict_] |
|
19 |
|
20 def __repr__(self): |
|
21 return repr(self.dicts) |
|
22 |
|
23 def __iter__(self): |
|
24 for d in reversed(self.dicts): |
|
25 yield d |
|
26 |
|
27 def push(self): |
|
28 d = {} |
|
29 self.dicts.append(d) |
|
30 return d |
|
31 |
|
32 def pop(self): |
|
33 if len(self.dicts) == 1: |
|
34 raise ContextPopException |
|
35 return self.dicts.pop() |
|
36 |
|
37 def __setitem__(self, key, value): |
|
38 "Set a variable in the current context" |
|
39 self.dicts[-1][key] = value |
|
40 |
|
41 def __getitem__(self, key): |
|
42 "Get a variable's value, starting at the current context and going upward" |
|
43 for d in reversed(self.dicts): |
|
44 if key in d: |
|
45 return d[key] |
|
46 raise KeyError(key) |
|
47 |
|
48 def __delitem__(self, key): |
|
49 "Delete a variable from the current context" |
|
50 del self.dicts[-1][key] |
|
51 |
|
52 def has_key(self, key): |
|
53 for d in self.dicts: |
|
54 if key in d: |
|
55 return True |
|
56 return False |
|
57 |
|
58 def __contains__(self, key): |
|
59 return self.has_key(key) |
|
60 |
|
61 def get(self, key, otherwise=None): |
|
62 for d in reversed(self.dicts): |
|
63 if key in d: |
|
64 return d[key] |
|
65 return otherwise |
|
66 |
|
67 class Context(BaseContext): |
|
68 "A stack container for variable context" |
|
69 def __init__(self, dict_=None, autoescape=True, current_app=None): |
|
70 self.autoescape = autoescape |
|
71 self.current_app = current_app |
|
72 self.render_context = RenderContext() |
|
73 super(Context, self).__init__(dict_) |
|
74 |
|
75 def update(self, other_dict): |
|
76 "Like dict.update(). Pushes an entire dictionary's keys and values onto the context." |
|
77 if not hasattr(other_dict, '__getitem__'): |
|
78 raise TypeError('other_dict must be a mapping (dictionary-like) object.') |
|
79 self.dicts.append(other_dict) |
|
80 return other_dict |
|
81 |
|
82 class RenderContext(BaseContext): |
|
83 """ |
|
84 A stack container for storing Template state. |
|
85 |
|
86 RenderContext simplifies the implementation of template Nodes by providing a |
|
87 safe place to store state between invocations of a node's `render` method. |
|
88 |
|
89 The RenderContext also provides scoping rules that are more sensible for |
|
90 'template local' variables. The render context stack is pushed before each |
|
91 template is rendered, creating a fresh scope with nothing in it. Name |
|
92 resolution fails if a variable is not found at the top of the RequestContext |
|
93 stack. Thus, variables are local to a specific template and don't affect the |
|
94 rendering of other templates as they would if they were stored in the normal |
|
95 template context. |
|
96 """ |
|
97 def __iter__(self): |
|
98 for d in self.dicts[-1]: |
|
99 yield d |
|
100 |
|
101 def has_key(self, key): |
|
102 return key in self.dicts[-1] |
|
103 |
|
104 def get(self, key, otherwise=None): |
|
105 d = self.dicts[-1] |
|
106 if key in d: |
|
107 return d[key] |
|
108 return otherwise |
|
109 |
|
110 # This is a function rather than module-level procedural code because we only |
|
111 # want it to execute if somebody uses RequestContext. |
|
112 def get_standard_processors(): |
|
113 from django.conf import settings |
|
114 global _standard_context_processors |
|
115 if _standard_context_processors is None: |
|
116 processors = [] |
|
117 collect = [] |
|
118 collect.extend(_builtin_context_processors) |
|
119 collect.extend(settings.TEMPLATE_CONTEXT_PROCESSORS) |
|
120 for path in collect: |
|
121 i = path.rfind('.') |
|
122 module, attr = path[:i], path[i+1:] |
|
123 try: |
|
124 mod = import_module(module) |
|
125 except ImportError, e: |
|
126 raise ImproperlyConfigured('Error importing request processor module %s: "%s"' % (module, e)) |
|
127 try: |
|
128 func = getattr(mod, attr) |
|
129 except AttributeError: |
|
130 raise ImproperlyConfigured('Module "%s" does not define a "%s" callable request processor' % (module, attr)) |
|
131 processors.append(func) |
|
132 _standard_context_processors = tuple(processors) |
|
133 return _standard_context_processors |
|
134 |
|
135 class RequestContext(Context): |
|
136 """ |
|
137 This subclass of template.Context automatically populates itself using |
|
138 the processors defined in TEMPLATE_CONTEXT_PROCESSORS. |
|
139 Additional processors can be specified as a list of callables |
|
140 using the "processors" keyword argument. |
|
141 """ |
|
142 def __init__(self, request, dict=None, processors=None, current_app=None): |
|
143 Context.__init__(self, dict, current_app=current_app) |
|
144 if processors is None: |
|
145 processors = () |
|
146 else: |
|
147 processors = tuple(processors) |
|
148 for processor in get_standard_processors() + processors: |
|
149 self.update(processor(request)) |