Order unsorted Selection by index definition

issue11169
review387771003
This commit is contained in:
Cédric Krier
2022-02-15 00:06:57 +01:00
parent 36d5bce9bc
commit 2495c0cbdd
5 changed files with 37 additions and 1 deletions
+1
View File
@@ -1,3 +1,4 @@
* Order not sorted Selection by index definition
* Add optional column on tree view
* Use dictionary as domain on Reference field
* Include model, field and column in import data error message
+5
View File
@@ -543,6 +543,11 @@ A string field with limited values to choose from.
If true, the choices will be sorted by human-readable value. Default value
is ``True``.
.. note::
If it is ``False``, search results ordered by the field will use the
index of the selection instead of the human-readable name.
..
.. attribute:: Selection.selection_change_with
A set of field names. If this attribute is set, the client will call the
+6 -1
View File
@@ -132,11 +132,16 @@ class Selection(SelectionMixin, Field):
else:
selections = []
column = self.sql_column(table)
if not self.sort:
else_ = len(selections) + 1
selections = ((k, i) for i, (k, v) in enumerate(selections))
else:
else_ = column
whens = []
for key, value in selections:
whens.append((column == key, value))
if whens:
return [Case(*whens, else_=column)]
return [Case(*whens, else_=else_)]
else:
return [column]
+6
View File
@@ -19,6 +19,12 @@ class Selection(ModelSQL):
'Static Selection')
dyn_select_static_string = dyn_select_static.translated(
'dyn_select_static')
unsorted_select = fields.Selection([
(None, ""),
('first', "First"),
('second', "Second"),
('last', "Last"),
], "Unsorted Selection", sort=False)
@fields.depends('select')
def get_selection(self):
+19
View File
@@ -172,6 +172,25 @@ class FieldSelectionTestCase(unittest.TestCase):
self.assertListEqual(values, ['3', '2', '1', '0x3', '0x2', '0x1'])
@with_transaction()
def test_search_order_unsorted(self):
"Test search order with unsorted selection"
pool = Pool()
Selection = pool.get('test.selection')
Selection.create([{
'unsorted_select': 'second',
}, {
'unsorted_select': 'last',
}, {
'unsorted_select': 'first',
}])
records = Selection.search([], order=[('unsorted_select', 'ASC')])
values = [r.unsorted_select for r in records]
self.assertListEqual(values, ['first', 'second', 'last'])
@with_transaction()
def test_string(self):
"Test string selection"