Mercurial > p > roundup > code
diff 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 |
line wrap: on
line diff
--- a/roundup/cgi/templating.py Mon May 17 20:52:27 2021 +0200 +++ b/roundup/cgi/templating.py Mon May 17 15:25:17 2021 -0400 @@ -2612,10 +2612,32 @@ l.reverse() return self.viewableGenerator(l) - def sorted(self, property, reverse=False): - """ Return this multilink sorted by the given property """ + def sorted(self, property, reverse=False, NoneFirst=False): + """ Return this multilink sorted by the given property + + Set Nonefirst to True to sort None/unset property + before a property with a valid value. + + """ + + # use 2 if NoneFirst is False to sort None last + # 0 to sort to sort None first + # 1 is used to sort the integer values. + NoneCode = (2,0)[NoneFirst] + def keyfunc(v): + # Return tuples made of (group order (int), base python + # type) to sort function. + # Do not return v[property] as that returns an HTMLProperty + # type/subtype that throws an exception when sorting + # python type (int. str ...) against None. + val = v[property]._value + if val: + return (1, val) # val should be base python type + elif val is None: + return (NoneCode, None) + value = list(self.__iter__()) - value.sort(key=lambda a:a[property], reverse=reverse) + value.sort(key=keyfunc, reverse=reverse) return value def __contains__(self, value):
