Skip to content

Commit

Permalink
Fixed #4506 -- Changed "regroup" template tag to use __eq__ instead o…
Browse files Browse the repository at this point in the history
…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
malcolmt committed Jun 17, 2007
1 parent 9b397ee commit 2a34fbe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
14 changes: 5 additions & 9 deletions django/template/defaulttags.py
Expand Up @@ -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 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


Expand Down Expand Up @@ -258,15 +259,10 @@ def iter_render(self, context):
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):
Expand Down
24 changes: 23 additions & 1 deletion django/utils/itercompat.py
Expand Up @@ -7,7 +7,8 @@
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
""" """
Expand All @@ -25,7 +26,28 @@ def gen(next, data={}, cnt=[0]):
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

0 comments on commit 2a34fbe

Please sign in to comment.