Loading django/template/context.py +9 −1 Original line number Diff line number Diff line Loading @@ -102,7 +102,15 @@ class BaseContext(object): Compares two contexts by comparing theirs 'dicts' attributes. """ if isinstance(other, BaseContext): return self.dicts[-1] == other.dicts[-1] # because dictionaries can be put in different order # we have to flatten them like in templates def flatten(dicts): flat = {} for d in dicts: flat.update(d) return flat return flatten(self.dicts) == flatten(other.dicts) # if it's not comparable return false return False Loading tests/template_tests/test_context.py +19 −1 Original line number Diff line number Diff line Loading @@ -53,4 +53,22 @@ class ContextTests(TestCase): def test_context_comparable(self): test_data = {'x': 'y', 'v': 'z', 'd': {'o': object, 'a': 'b'}} self.assertEquals(Context(test_data), Context(test_data)) self.assertEqual(Context(test_data), Context(test_data)) # Regression test for #21765 a = Context() b = Context() self.assertEqual(a, b) # update only a a.update({'a': 1}) self.assertNotEqual(a, b) # update both to check regression a.update({'c': 3}) b.update({'c': 3}) self.assertNotEqual(a, b) # make contexts equals again b.update({'a': 1}) self.assertEqual(a, b) Loading
django/template/context.py +9 −1 Original line number Diff line number Diff line Loading @@ -102,7 +102,15 @@ class BaseContext(object): Compares two contexts by comparing theirs 'dicts' attributes. """ if isinstance(other, BaseContext): return self.dicts[-1] == other.dicts[-1] # because dictionaries can be put in different order # we have to flatten them like in templates def flatten(dicts): flat = {} for d in dicts: flat.update(d) return flat return flatten(self.dicts) == flatten(other.dicts) # if it's not comparable return false return False Loading
tests/template_tests/test_context.py +19 −1 Original line number Diff line number Diff line Loading @@ -53,4 +53,22 @@ class ContextTests(TestCase): def test_context_comparable(self): test_data = {'x': 'y', 'v': 'z', 'd': {'o': object, 'a': 'b'}} self.assertEquals(Context(test_data), Context(test_data)) self.assertEqual(Context(test_data), Context(test_data)) # Regression test for #21765 a = Context() b = Context() self.assertEqual(a, b) # update only a a.update({'a': 1}) self.assertNotEqual(a, b) # update both to check regression a.update({'c': 3}) b.update({'c': 3}) self.assertNotEqual(a, b) # make contexts equals again b.update({'a': 1}) self.assertEqual(a, b)