Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4506 -- Changed "regroup" template tag to use __eq__ instead o…

…f repr()

for grouping equality checking. Thanks, Brian Harring.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5484 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2a34fbe043d5c4ba789e866a912646aea1bb807c 1 parent 9b397ee
Malcolm Tredinnick authored June 17, 2007
14  django/template/defaulttags.py
@@ -4,6 +4,7 @@
4 4
 from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
5 5
 from django.template import get_library, Library, InvalidTemplateLibrary
6 6
 from django.conf import settings
  7
+from django.utils.itercompat import groupby
7 8
 import sys
8 9
 import re
9 10
 
@@ -258,15 +259,10 @@ def iter_render(self, context):
258 259
         if obj_list == None: # target_var wasn't found in context; fail silently
259 260
             context[self.var_name] = []
260 261
             return ()
261  
-        output = [] # list of dictionaries in the format {'grouper': 'key', 'list': [list of contents]}
262  
-        for obj in obj_list:
263  
-            grouper = self.expression.resolve(obj, True)
264  
-            # TODO: Is this a sensible way to determine equality?
265  
-            if output and repr(output[-1]['grouper']) == repr(grouper):
266  
-                output[-1]['list'].append(obj)
267  
-            else:
268  
-                output.append({'grouper': grouper, 'list': [obj]})
269  
-        context[self.var_name] = output
  262
+        # List of dictionaries in the format
  263
+        # {'grouper': 'key', 'list': [list of contents]}.
  264
+        context[self.var_name] = [{'grouper':key, 'list':list(val)} for key, val in
  265
+            groupby(obj_list, lambda v, f=self.expression.resolve: f(v, True))]
270 266
         return ()
271 267
 
272 268
 def include_is_allowed(filepath):
24  django/utils/itercompat.py
@@ -7,7 +7,8 @@
7 7
 import itertools
8 8
 
9 9
 def compat_tee(iterable):
10  
-    """Return two independent iterators from a single iterable.
  10
+    """
  11
+    Return two independent iterators from a single iterable.
11 12
 
12 13
     Based on http://www.python.org/doc/2.3.5/lib/itertools-example.html
13 14
     """
@@ -25,7 +26,28 @@ def gen(next, data={}, cnt=[0]):
25 26
     next = iter(iterable).next
26 27
     return gen(next), gen(next)
27 28
 
  29
+def groupby(iterable, keyfunc=None):
  30
+    """
  31
+    Taken from http://docs.python.org/lib/itertools-functions.html
  32
+    """
  33
+    if keyfunc is None:
  34
+        keyfunc = lambda x:x
  35
+    iterable = iter(iterable)
  36
+    l = [iterable.next()]
  37
+    lastkey = keyfunc(l)
  38
+    for item in iterable:
  39
+        key = keyfunc(item)
  40
+        if key != lastkey:
  41
+            yield lastkey, l
  42
+            lastkey = key
  43
+            l = [item]
  44
+        else:
  45
+            l.append(item)
  46
+    yield lastkey, l
  47
+
28 48
 if hasattr(itertools, 'tee'):
29 49
     tee = itertools.tee
30 50
 else:
31 51
     tee = compat_tee
  52
+if hasattr(itertools, 'groupby'):
  53
+    groupby = itertools.groupby

0 notes on commit 2a34fbe

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