Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20404 -- Added a keys() method to ContextList.

It's useful to be able to list all the (flattened) keys of a
ContextList, to help you figure out why the variable that's supposed
to be there is not.

No .values() or .items() added as the definition for those aren't clear.

The patch is Chris Wilson's patch from pull request 1065 with some
modifications by committer.
  • Loading branch information...
commit fa7cb4ef3cc5f4c3514857bd25b43d31815eba94 1 parent 5090c7b
Chris Wilson authored May 13, 2013 akaariai committed June 01, 2013
10  django/test/utils.py
@@ -60,6 +60,16 @@ def __contains__(self, key):
60 60
             return False
61 61
         return True
62 62
 
  63
+    def keys(self):
  64
+        """
  65
+        Flattened keys of subcontexts.
  66
+        """
  67
+        keys = set()
  68
+        for subcontext in self:
  69
+            for dict in subcontext:
  70
+                keys |= set(dict.keys())
  71
+        return keys
  72
+
63 73
 
64 74
 def instrumented_test_render(self, context):
65 75
     """
18  tests/test_client_regress/tests.py
@@ -6,9 +6,8 @@
6 6
 
7 7
 import os
8 8
 
9  
-from django.conf import settings
10 9
 from django.core.urlresolvers import reverse
11  
-from django.template import (TemplateDoesNotExist, TemplateSyntaxError,
  10
+from django.template import (TemplateSyntaxError,
12 11
     Context, Template, loader)
13 12
 import django.template.context
14 13
 from django.test import Client, TestCase
@@ -897,6 +896,21 @@ def test_inherited_context(self):
897 896
         except KeyError as e:
898 897
             self.assertEqual(e.args[0], 'does-not-exist')
899 898
 
  899
+    def test_contextlist_keys(self):
  900
+        c1 = Context()
  901
+        c1.update({'hello': 'world', 'goodbye': 'john'})
  902
+        c1.update({'hello': 'dolly', 'dolly': 'parton'})
  903
+        c2 = Context()
  904
+        c2.update({'goodbye': 'world', 'python': 'rocks'})
  905
+        c2.update({'goodbye': 'dolly'})
  906
+
  907
+        l = ContextList([c1, c2])
  908
+        # None, True and False are builtins of BaseContext, and present
  909
+        # in every Context without needing to be added.
  910
+        self.assertEqual(set(['None', 'True', 'False', 'hello', 'goodbye',
  911
+                              'python', 'dolly']),
  912
+                         l.keys())
  913
+
900 914
     def test_15368(self):
901 915
         # Need to insert a context processor that assumes certain things about
902 916
         # the request instance. This triggers a bug caused by some ways of

0 notes on commit fa7cb4e

Please sign in to comment.
Something went wrong with that request. Please try again.