diff --git a/trytond/trytond/model/fields/char.py b/trytond/trytond/model/fields/char.py index 1d113d9fcf..0363ce427f 100644 --- a/trytond/trytond/model/fields/char.py +++ b/trytond/trytond/model/fields/char.py @@ -12,7 +12,7 @@ from trytond.rpc import RPC from trytond.tools import is_full_text, unescape_wildcard from trytond.transaction import Transaction -from .field import Field, FieldTranslate, size_validate +from .field import Field, FieldTranslate, order_method, size_validate class Char(FieldTranslate): @@ -188,10 +188,8 @@ class Char(FieldTranslate): expression = fts return expression + @order_method def convert_order(self, name, tables, Model): - method = getattr(Model, 'order_%s' % name, None) - if method: - return method(tables) transaction = Transaction() context = transaction.context database = transaction.database diff --git a/trytond/trytond/model/fields/dict.py b/trytond/trytond/model/fields/dict.py index f5e657363c..2a9a3dd929 100644 --- a/trytond/trytond/model/fields/dict.py +++ b/trytond/trytond/model/fields/dict.py @@ -12,7 +12,7 @@ from trytond.tools import grouped_slice from trytond.tools.immutabledict import ImmutableDict from trytond.transaction import Transaction -from .field import SQL_OPERATORS, Field, domain_method +from .field import SQL_OPERATORS, Field, domain_method, order_method # Use canonical form dumps = partial( @@ -164,6 +164,7 @@ class Dict(Field): expression = self._domain_add_null(column, operator, value, expression) return expression + @order_method def convert_order(self, name, tables, Model): fname, _, key = name.partition('.') if not key: diff --git a/trytond/trytond/model/fields/field.py b/trytond/trytond/model/fields/field.py index f846e4c4aa..333afa1f2f 100644 --- a/trytond/trytond/model/fields/field.py +++ b/trytond/trytond/model/fields/field.py @@ -221,6 +221,19 @@ def domain_method(func): return wrapper +def order_method(func): + @wraps(func) + def wrapper(self, name, tables, Model): + fname, _, oexpr = name.partition('.') + assert fname == self.name + method = getattr(Model, f'order_{fname}', None) + if not oexpr and method: + return method(tables) + else: + return func(self, name, tables, Model) + return wrapper + + SQL_OPERATORS = { '=': operators.Equal, '!=': operators.NotEqual, @@ -456,15 +469,11 @@ class Field(object): expression = self._domain_add_null(column, operator, value, expression) return expression + @order_method def convert_order(self, name, tables, Model): "Return a SQL expression to order" - assert name == self.name table, _ = tables[None] - method = getattr(Model, 'order_%s' % name, None) - if method: - return method(tables) - else: - return [self.sql_column(table)] + return [self.sql_column(table)] def set_rpc(self, model): for attribute, result in ( @@ -732,6 +741,7 @@ class FieldTranslate(Field): language = get_parent_language(language) return column + @order_method def convert_order(self, name, tables, Model): if not self.translate: return super().convert_order(name, tables, Model) diff --git a/trytond/trytond/model/fields/many2one.py b/trytond/trytond/model/fields/many2one.py index 1f9b967bcf..dcecb9df3e 100644 --- a/trytond/trytond/model/fields/many2one.py +++ b/trytond/trytond/model/fields/many2one.py @@ -12,7 +12,7 @@ from trytond.tools import cached_property, reduce_ids from trytond.transaction import Transaction, inactive_records from .field import ( - Field, context_validate, domain_method, instantiate_context, + Field, context_validate, domain_method, instantiate_context, order_method, search_order_validate) _subquery_threshold = config.getint('database', 'subquery_threshold') @@ -280,11 +280,9 @@ class Many2One(Field): target_domain, tables=target_tables) return expression + @order_method def convert_order(self, name, tables, Model): fname, _, oexpr = name.partition('.') - if not oexpr and getattr(Model, 'order_%s' % fname, None): - return super(Many2One, self).convert_order(fname, tables, Model) - assert fname == self.name Target = self.get_target() diff --git a/trytond/trytond/model/fields/selection.py b/trytond/trytond/model/fields/selection.py index db8b0cfce2..9a87742948 100644 --- a/trytond/trytond/model/fields/selection.py +++ b/trytond/trytond/model/fields/selection.py @@ -11,7 +11,7 @@ from trytond.tools import is_instance_method from trytond.tools.string_ import LazyString from trytond.transaction import Transaction -from .field import Field +from .field import Field, order_method class SelectionMixin(Field): @@ -141,11 +141,8 @@ class Selection(SelectionMixin, Field): model.__rpc__.setdefault( self.selection, RPC(instantiate=instantiate, cache=cache)) + @order_method def convert_order(self, name, tables, Model): - if getattr(Model, 'order_%s' % name, None): - return super(Selection, self).convert_order(name, tables, Model) - - assert name == self.name table, _ = tables[None] selections = Model.fields_get([name])[name]['selection'] if not isinstance(selections, (tuple, list)): diff --git a/trytond/trytond/model/fields/text.py b/trytond/trytond/model/fields/text.py index c1e7c019cd..284ccd0708 100644 --- a/trytond/trytond/model/fields/text.py +++ b/trytond/trytond/model/fields/text.py @@ -6,7 +6,7 @@ from sql.operators import ILike, In, Not, NotIn from trytond.transaction import Transaction from .char import Char -from .field import SQL_OPERATORS, Field +from .field import SQL_OPERATORS, Field, order_method class Text(Char): @@ -85,10 +85,8 @@ class FullText(Field): column, operator, value, expression) return expression + @order_method def convert_order(self, name, tables, Model): - method = getattr(Model, 'order_%s' % name, None) - if method: - return method(tables) table, _ = tables[None] column = self.sql_column(table) column = self._domain_column('ilike', column)