Ticket #4506: RegroupNode-groupby.patch

File RegroupNode-groupby.patch, 1.5 KB (added by (removed), 8 years ago)

switch to doing eq comparisons, drop the duplicate grouping logic instead using itertools.groupby

  • django/template/defaulttags.py

    === modified file 'django/template/defaulttags.py'
     
    44from 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
    55from django.template import get_library, Library, InvalidTemplateLibrary
    66from django.conf import settings
     7from itertools import groupby
    78import sys
    89import re
    910
     
    248249        if obj_list == None: # target_var wasn't found in context; fail silently
    249250            context[self.var_name] = []
    250251            return ''
    251         output = [] # list of dictionaries in the format {'grouper': 'key', 'list': [list of contents]}
    252         for obj in obj_list:
    253             grouper = self.expression.resolve(obj, True)
    254             # TODO: Is this a sensible way to determine equality?
    255             if output and repr(output[-1]['grouper']) == repr(grouper):
    256                 output[-1]['list'].append(obj)
    257             else:
    258                 output.append({'grouper': grouper, 'list': [obj]})
    259         context[self.var_name] = output
     252        # list of dictionaries in the format {'grouper': 'key', 'list': [list of contents]}
     253        context[self.var_name] = [{'grouper':key, 'list':list(val)} for key, val in
     254            groupby(obj_list, lambda v, f=self.expression.resolve: f(v, True))]
    260255        return ''
    261256
    262257def include_is_allowed(filepath):
Back to Top