Django

Code

root/django/branches/gis/django/template/context.py

Revision 8215, 3.4 kB (checked in by jbronn, 4 months ago)

gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.

  • Property svn:eol-style set to native
Line 
1 from django.conf import settings
2 from django.core.exceptions import ImproperlyConfigured
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     global _standard_context_processors
74     if _standard_context_processors is None:
75         processors = []
76         for path in settings.TEMPLATE_CONTEXT_PROCESSORS:
77             i = path.rfind('.')
78             module, attr = path[:i], path[i+1:]
79             try:
80                 mod = __import__(module, {}, {}, [attr])
81             except ImportError, e:
82                 raise ImproperlyConfigured('Error importing request processor module %s: "%s"' % (module, e))
83             try:
84                 func = getattr(mod, attr)
85             except AttributeError:
86                 raise ImproperlyConfigured('Module "%s" does not define a "%s" callable request processor' % (module, attr))
87             processors.append(func)
88         _standard_context_processors = tuple(processors)
89     return _standard_context_processors
90
91 class RequestContext(Context):
92     """
93     This subclass of template.Context automatically populates itself using
94     the processors defined in TEMPLATE_CONTEXT_PROCESSORS.
95     Additional processors can be specified as a list of callables
96     using the "processors" keyword argument.
97     """
98     def __init__(self, request, dict=None, processors=None):
99         Context.__init__(self, dict)
100         if processors is None:
101             processors = ()
102         else:
103             processors = tuple(processors)
104         for processor in get_standard_processors() + processors:
105             self.update(processor(request))
Note: See TracBrowser for help on using the browser.