Skip to content

Commit 7406257

Browse files
committed
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.
1 parent 2c17713 commit 7406257

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

CHANGES.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ Fixed:
9898
- issue2551078 - Fix traceback caused when putting two id's into a
9999
Link html field. A ValueError is raised. Handle exception and return
100100
value. hyperdb.py now reports 'you may only enter ID values for
101-
property ...' to the user.
101+
property ...' to the user. (John Rouillard)
102+
- issue2551120 - The sorted method of MultilinkHTMLProperty crashes,
103+
if the given property is unset for an element of the list. Crash
104+
fixed. New feature NoneFirst added to method to make unset values
105+
sort at start or end of sorted list. (John Rouillard)
102106

103107
Features:
104108
- issue2550522 - Add 'filter' command to command-line

doc/customizing.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3292,7 +3292,17 @@ sorted only on Multilink properties - produce a list of the linked
32923292
python:context.files.sorted('creation', reverse=True)
32933293

32943294
Will list the files by upload date in reverse order from
3295-
the prior example.
3295+
the prior example. If the property can be unset, you can
3296+
use the ``NoneFirst`` parameter to sort the None/Unset
3297+
values at the front or the end of the list. For example::
3298+
3299+
python:context.files.sorted('creation', NoneFirst=True)
3300+
3301+
will sort files by creation date with files missing a
3302+
creation date at the start of the list. The default for
3303+
``NoneFirst`` is False so these files will sort at the end
3304+
by default. (Note creation date is never unset, but you
3305+
get the idea.)
32963306
reverse only on Multilink properties - produce a list of the linked
32973307
items in reverse order
32983308
isset returns True if the property has been set to a value

roundup/cgi/templating.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,10 +2612,32 @@ def reverse(self):
26122612
l.reverse()
26132613
return self.viewableGenerator(l)
26142614

2615-
def sorted(self, property, reverse=False):
2616-
""" Return this multilink sorted by the given property """
2615+
def sorted(self, property, reverse=False, NoneFirst=False):
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+
26172639
value = list(self.__iter__())
2618-
value.sort(key=lambda a:a[property], reverse=reverse)
2640+
value.sort(key=keyfunc, reverse=reverse)
26192641
return value
26202642

26212643
def __contains__(self, value):

0 commit comments

Comments
 (0)