changeset 8f1b91ce455e in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=8f1b91ce455e
description:
        Order unsorted Selection by index definition

        issue11169
        review387771003
diffstat:

 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 deletions(-)

diffs (89 lines):

diff -r a32b84d96649 -r 8f1b91ce455e CHANGELOG
--- a/CHANGELOG Mon Feb 14 23:12:00 2022 +0100
+++ b/CHANGELOG Tue Feb 15 00:06:57 2022 +0100
@@ -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 -r a32b84d96649 -r 8f1b91ce455e doc/ref/models/fields.rst
--- a/doc/ref/models/fields.rst Mon Feb 14 23:12:00 2022 +0100
+++ b/doc/ref/models/fields.rst Tue Feb 15 00:06:57 2022 +0100
@@ -543,6 +543,11 @@
     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 -r a32b84d96649 -r 8f1b91ce455e trytond/model/fields/selection.py
--- a/trytond/model/fields/selection.py Mon Feb 14 23:12:00 2022 +0100
+++ b/trytond/model/fields/selection.py Tue Feb 15 00:06:57 2022 +0100
@@ -132,11 +132,16 @@
             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 -r a32b84d96649 -r 8f1b91ce455e trytond/tests/field_selection.py
--- a/trytond/tests/field_selection.py  Mon Feb 14 23:12:00 2022 +0100
+++ b/trytond/tests/field_selection.py  Tue Feb 15 00:06:57 2022 +0100
@@ -19,6 +19,12 @@
         '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 -r a32b84d96649 -r 8f1b91ce455e trytond/tests/test_field_selection.py
--- a/trytond/tests/test_field_selection.py     Mon Feb 14 23:12:00 2022 +0100
+++ b/trytond/tests/test_field_selection.py     Tue Feb 15 00:06:57 2022 +0100
@@ -173,6 +173,25 @@
         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"
         Selection = Pool().get('test.selection')

Reply via email to