Fixed #4506 -- Changed "regroup" template tag to use __eq__ instead of repr()

for grouping equality checking. Thanks, Brian Harring.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5484 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-06-17 07:33:18 +00:00
parent 9b397ee50d
commit 2a34fbe043
2 changed files with 28 additions and 10 deletions

View File

@ -4,6 +4,7 @@ from django.template import Node, NodeList, Template, Context, resolve_variable
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 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.template import get_library, Library, InvalidTemplateLibrary
from django.conf import settings from django.conf import settings
from django.utils.itercompat import groupby
import sys import sys
import re import re
@ -258,15 +259,10 @@ class RegroupNode(Node):
if obj_list == None: # target_var wasn't found in context; fail silently if obj_list == None: # target_var wasn't found in context; fail silently
context[self.var_name] = [] context[self.var_name] = []
return () return ()
output = [] # list of dictionaries in the format {'grouper': 'key', 'list': [list of contents]} # List of dictionaries in the format
for obj in obj_list: # {'grouper': 'key', 'list': [list of contents]}.
grouper = self.expression.resolve(obj, True) context[self.var_name] = [{'grouper':key, 'list':list(val)} for key, val in
# TODO: Is this a sensible way to determine equality? groupby(obj_list, lambda v, f=self.expression.resolve: f(v, True))]
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
return () return ()
def include_is_allowed(filepath): def include_is_allowed(filepath):

View File

@ -7,7 +7,8 @@ these implementations if necessary.
import itertools import itertools
def compat_tee(iterable): 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 Based on http://www.python.org/doc/2.3.5/lib/itertools-example.html
""" """
@ -25,7 +26,28 @@ def compat_tee(iterable):
next = iter(iterable).next next = iter(iterable).next
return gen(next), gen(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'): if hasattr(itertools, 'tee'):
tee = itertools.tee tee = itertools.tee
else: else:
tee = compat_tee tee = compat_tee
if hasattr(itertools, 'groupby'):
groupby = itertools.groupby