Commit 0a655dc6 by Carl Whittaker

Adding magic attribute to fetch a flattened context

parent 8eac16d8
......@@ -74,8 +74,18 @@ class View(object):
val = self.get(attr, None)
if not val and val is not 0:
raise KeyError("No such key.")
return val
raise KeyError("No such key '%s'." % attr)
return val
def __getattr__(self, attr):
if attr == 'context':
context = {}
for d in self.context_list:
context.update(d)
return context
raise AttributeError("No such attribute '%s'." % attr)
def __str__(self):
return self.render()
\ No newline at end of file
......@@ -98,5 +98,11 @@ class TestView(unittest.TestCase):
self.assertEquals(view.render(), 'derp')
def test_context_returns_a_flattened_dict(self):
view = Simple()
view.context_list = [{'one':'1'}, {'two':'2'}]
self.assertEqual(view.context, {'one': '1', 'two': '2'})
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment