From 2495c0cbdd83d574a649a09c02f23d1473218e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 15 Feb 2022 00:06:57 +0100 Subject: [PATCH] Order unsorted Selection by index definition issue11169 review387771003 --- CHANGELOG | 1 + doc/ref/models/fields.rst | 5 +++++ trytond/model/fields/selection.py | 7 ++++++- trytond/tests/field_selection.py | 6 ++++++ trytond/tests/test_field_selection.py | 19 +++++++++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 1fbfba68b1..7c16bd1c4b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/doc/ref/models/fields.rst b/doc/ref/models/fields.rst index 846b121a32..5b84506877 100644 --- a/doc/ref/models/fields.rst +++ b/doc/ref/models/fields.rst @@ -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 diff --git a/trytond/model/fields/selection.py b/trytond/model/fields/selection.py index 459c789d3c..4711204575 100644 --- a/trytond/model/fields/selection.py +++ b/trytond/model/fields/selection.py @@ -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] diff --git a/trytond/tests/field_selection.py b/trytond/tests/field_selection.py index 598b39bdbe..cbae723df8 100644 --- a/trytond/tests/field_selection.py +++ b/trytond/tests/field_selection.py @@ -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): diff --git a/trytond/tests/test_field_selection.py b/trytond/tests/test_field_selection.py index a924a1cec7..6ff5647875 100644 --- a/trytond/tests/test_field_selection.py +++ b/trytond/tests/test_field_selection.py @@ -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"