Author: mtredinnick
Date: 2007-06-17 02:33:18 -0500 (Sun, 17 Jun 2007)
New Revision: 5484

Modified:
   django/trunk/django/template/defaulttags.py
   django/trunk/django/utils/itercompat.py
Log:
Fixed #4506 -- Changed "regroup" template tag to use __eq__ instead of repr()
for grouping equality checking. Thanks, Brian Harring.


Modified: django/trunk/django/template/defaulttags.py
===================================================================
--- django/trunk/django/template/defaulttags.py 2007-06-17 07:21:09 UTC (rev 
5483)
+++ django/trunk/django/template/defaulttags.py 2007-06-17 07:33:18 UTC (rev 
5484)
@@ -4,6 +4,7 @@
 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
 from django.template import get_library, Library, InvalidTemplateLibrary
 from django.conf import settings
+from django.utils.itercompat import groupby
 import sys
 import re
 
@@ -258,15 +259,10 @@
         if obj_list == None: # target_var wasn't found in context; fail 
silently
             context[self.var_name] = []
             return ()
-        output = [] # list of dictionaries in the format {'grouper': 'key', 
'list': [list of contents]}
-        for obj in obj_list:
-            grouper = self.expression.resolve(obj, True)
-            # TODO: Is this a sensible way to determine equality?
-            if output and repr(output[-1]['grouper']) == repr(grouper):
-                output[-1]['list'].append(obj)
-            else:
-                output.append({'grouper': grouper, 'list': [obj]})
-        context[self.var_name] = output
+        # List of dictionaries in the format
+        # {'grouper': 'key', 'list': [list of contents]}.
+        context[self.var_name] = [{'grouper':key, 'list':list(val)} for key, 
val in
+            groupby(obj_list, lambda v, f=self.expression.resolve: f(v, True))]
         return ()
 
 def include_is_allowed(filepath):

Modified: django/trunk/django/utils/itercompat.py
===================================================================
--- django/trunk/django/utils/itercompat.py     2007-06-17 07:21:09 UTC (rev 
5483)
+++ django/trunk/django/utils/itercompat.py     2007-06-17 07:33:18 UTC (rev 
5484)
@@ -7,7 +7,8 @@
 import itertools
 
 def compat_tee(iterable):
-    """Return two independent iterators from a single iterable.
+    """
+    Return two independent iterators from a single iterable.
 
     Based on http://www.python.org/doc/2.3.5/lib/itertools-example.html
     """
@@ -25,7 +26,28 @@
     next = iter(iterable).next
     return gen(next), gen(next)
 
+def groupby(iterable, keyfunc=None):
+    """
+    Taken from http://docs.python.org/lib/itertools-functions.html
+    """
+    if keyfunc is None:
+        keyfunc = lambda x:x
+    iterable = iter(iterable)
+    l = [iterable.next()]
+    lastkey = keyfunc(l)
+    for item in iterable:
+        key = keyfunc(item)
+        if key != lastkey:
+            yield lastkey, l
+            lastkey = key
+            l = [item]
+        else:
+            l.append(item)
+    yield lastkey, l
+
 if hasattr(itertools, 'tee'):
     tee = itertools.tee
 else:
     tee = compat_tee
+if hasattr(itertools, 'groupby'):
+    groupby = itertools.groupby


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to