Django

Code

Changeset 5484

Show
Ignore:
Timestamp:
06/17/07 02:33:18 (1 year ago)
Author:
mtredinnick
Message:

Fixed #4506 -- Changed "regroup" template tag to use eq instead of repr()
for grouping equality checking. Thanks, Brian Harring.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/defaulttags.py

    r5482 r5484  
    55from django.template import get_library, Library, InvalidTemplateLibrary 
    66from django.conf import settings 
     7from django.utils.itercompat import groupby 
    78import sys 
    89import re 
     
    259260            context[self.var_name] = [] 
    260261            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))] 
    270266        return () 
    271267 
  • django/trunk/django/utils/itercompat.py

    r4265 r5484  
    88 
    99def compat_tee(iterable): 
    10     """Return two independent iterators from a single iterable. 
     10    """ 
     11    Return two independent iterators from a single iterable. 
    1112 
    1213    Based on http://www.python.org/doc/2.3.5/lib/itertools-example.html 
     
    2627    return gen(next), gen(next) 
    2728 
     29def 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 
    2848if hasattr(itertools, 'tee'): 
    2949    tee = itertools.tee 
    3050else: 
    3151    tee = compat_tee 
     52if hasattr(itertools, 'groupby'): 
     53    groupby = itertools.groupby