changeset a2f193a792d4 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=a2f193a792d4
description:
        Add sort and translate options to Reference field

        As Reference field uses the SelectionMixin, they could have the same 
options.

        issue8278
        review253331002
diffstat:

 CHANGELOG                         |   1 +
 doc/ref/models/fields.rst         |  12 ++++++++++--
 trytond/ir/ui/menu.py             |   2 +-
 trytond/model/fields/reference.py |  14 +++++++++-----
 trytond/model/fields/selection.py |   7 ++-----
 5 files changed, 23 insertions(+), 13 deletions(-)

diffs (126 lines):

diff -r 9bf65e1188fb -r a2f193a792d4 CHANGELOG
--- a/CHANGELOG Mon Apr 15 16:07:52 2019 +0200
+++ b/CHANGELOG Mon Apr 15 16:11:00 2019 +0200
@@ -1,3 +1,4 @@
+* Add sort and translate options to Reference field
 * Do not create empty translations
 * Replace dsn by params to connect to postgresql
 * Simplify cron
diff -r 9bf65e1188fb -r a2f193a792d4 doc/ref/models/fields.rst
--- a/doc/ref/models/fields.rst Mon Apr 15 16:07:52 2019 +0200
+++ b/doc/ref/models/fields.rst Mon Apr 15 16:11:00 2019 +0200
@@ -446,7 +446,7 @@
 Selection
 ---------
 
-.. class:: Selection(selection, string[, sort[, selection_change_with[, 
translate[, \**options]]])
+.. class:: Selection(selection, string[, sort[, selection_change_with[, 
translate[, \**options]]]])
 
 A string field with limited values to choice.
 
@@ -505,7 +505,7 @@
 Reference
 ---------
 
-.. class:: Reference(string[, selection[, selection_change_with[, 
search_order[, search_context[, \**options]]]])
+.. class:: Reference(string[, selection[, sort[, selection_change_with[, 
translated[,search_order[, search_context[, \**options]]]]]]])
 
 A field that refers to a record of a model. It will be represented in Python by
 a ``str`` instance like this::
@@ -520,10 +520,18 @@
 
     Same as :attr:`Selection.selection` but only for model name.
 
+.. attribute:: Reference.sort
+
+    Same as :attr:`Selection.sort`.
+
 .. attribute:: Reference.selection_change_with
 
     Same as :attr:`Selection.selection_change_with`.
 
+.. attribute:: Reference.translate_selection
+
+    Same as :attr:`Selection.translate_selection`.
+
 .. attribute:: Reference.datetime_field
 
     Same as :attr:`Many2One.datetime_field`
diff -r 9bf65e1188fb -r a2f193a792d4 trytond/ir/ui/menu.py
--- a/trytond/ir/ui/menu.py     Mon Apr 15 16:07:52 2019 +0200
+++ b/trytond/ir/ui/menu.py     Mon Apr 15 16:11:00 2019 +0200
@@ -99,7 +99,7 @@
                 ('ir.action.act_window', 'ir.action.act_window'),
                 ('ir.action.wizard', 'ir.action.wizard'),
                 ('ir.action.url', 'ir.action.url'),
-                ]), 'get_action', setter='set_action')
+                ], translate=False), 'get_action', setter='set_action')
     action_keywords = fields.One2Many('ir.action.keyword', 'model',
         'Action Keywords')
     favorite = fields.Function(fields.Boolean('Favorite'), 'get_favorite')
diff -r 9bf65e1188fb -r a2f193a792d4 trytond/model/fields/reference.py
--- a/trytond/model/fields/reference.py Mon Apr 15 16:07:52 2019 +0200
+++ b/trytond/model/fields/reference.py Mon Apr 15 16:11:00 2019 +0200
@@ -21,15 +21,17 @@
     _type = 'reference'
     _sql_type = 'VARCHAR'
 
-    def __init__(self, string='', selection=None, selection_change_with=None,
-            search_order=None, search_context=None, help='', required=False,
-            readonly=False, domain=None, states=None, select=False,
-            on_change=None, on_change_with=None, depends=None, context=None,
-            loading='lazy', datetime_field=None):
+    def __init__(self, string='', selection=None, sort=True,
+            selection_change_with=None, translate=True, search_order=None,
+            search_context=None, help='', required=False, readonly=False,
+            domain=None, states=None, select=False, on_change=None,
+            on_change_with=None, depends=None, context=None, loading='lazy',
+            datetime_field=None):
         '''
         :param selection: A list or a function name that returns a list.
             The list must be a list of tuples. First member is an internal name
             of model and the second is the user name of model.
+        :param sort: A boolean to sort or not the selections.
         :param datetime_field: The name of the field that contains the datetime
             value to read the target records.
         :param search_order: The order to use when searching for a record
@@ -52,6 +54,8 @@
                 'use the depends decorator',
                 DeprecationWarning, stacklevel=2)
             self.selection_change_with |= set(selection_change_with)
+        self.sort = sort
+        self.translate_selection = translate
         self.__search_order = None
         self.search_order = search_order
         self.__search_context = None
diff -r 9bf65e1188fb -r a2f193a792d4 trytond/model/fields/selection.py
--- a/trytond/model/fields/selection.py Mon Apr 15 16:07:52 2019 +0200
+++ b/trytond/model/fields/selection.py Mon Apr 15 16:11:00 2019 +0200
@@ -13,6 +13,7 @@
 
 class SelectionMixin(Field):
     translate_selection = True
+    sort = True
 
     def translated(self, name=None):
         "Return a descriptor for the translated value of the field"
@@ -39,6 +40,7 @@
             selection = self.selection
         definition['selection'] = selection
         definition['selection_change_with'] = list(self.selection_change_with)
+        definition['sort'] = self.sort
         return definition
 
     def definition_translations(self, model, language):
@@ -117,11 +119,6 @@
         else:
             return [column]
 
-    def definition(self, model, language):
-        definition = super().definition(model, language)
-        definition['sort'] = self.sort
-        return definition
-
 
 class TranslatedSelection(object):
     'A descriptor for translated value of Selection field'

Reply via email to