comparison roundup/cgi/templating.py @ 6414:3dbf1bc5e567

issue2551120 - The sorted method of MultilinkHTMLProperty crashes ... if the given property is unset for an element of the list. Crash fixed. New feature NoneFirst added to method to make unset values sort at start or end of sorted list. Current testing framework for this code is insuffient for testing change. Committing without automated test because it solves a crash.
author John Rouillard <rouilj@ieee.org>
date Mon, 17 May 2021 15:25:17 -0400
parents e29d5f4e0af4
children 9c57f2814597
comparison
equal deleted inserted replaced
6413:7b1b6dffc7ed 6414:3dbf1bc5e567
2610 """ 2610 """
2611 l = self._value[:] 2611 l = self._value[:]
2612 l.reverse() 2612 l.reverse()
2613 return self.viewableGenerator(l) 2613 return self.viewableGenerator(l)
2614 2614
2615 def sorted(self, property, reverse=False): 2615 def sorted(self, property, reverse=False, NoneFirst=False):
2616 """ Return this multilink sorted by the given property """ 2616 """ Return this multilink sorted by the given property
2617
2618 Set Nonefirst to True to sort None/unset property
2619 before a property with a valid value.
2620
2621 """
2622
2623 # use 2 if NoneFirst is False to sort None last
2624 # 0 to sort to sort None first
2625 # 1 is used to sort the integer values.
2626 NoneCode = (2,0)[NoneFirst]
2627 def keyfunc(v):
2628 # Return tuples made of (group order (int), base python
2629 # type) to sort function.
2630 # Do not return v[property] as that returns an HTMLProperty
2631 # type/subtype that throws an exception when sorting
2632 # python type (int. str ...) against None.
2633 val = v[property]._value
2634 if val:
2635 return (1, val) # val should be base python type
2636 elif val is None:
2637 return (NoneCode, None)
2638
2617 value = list(self.__iter__()) 2639 value = list(self.__iter__())
2618 value.sort(key=lambda a:a[property], reverse=reverse) 2640 value.sort(key=keyfunc, reverse=reverse)
2619 return value 2641 return value
2620 2642
2621 def __contains__(self, value): 2643 def __contains__(self, value):
2622 """ Support the "in" operator. We have to make sure the passed-in 2644 """ Support the "in" operator. We have to make sure the passed-in
2623 value is a string first, not a HTMLProperty. 2645 value is a string first, not a HTMLProperty.

Roundup Issue Tracker: http://roundup-tracker.org/