diff --git a/modules/account_es/reporting_tax.py b/modules/account_es/reporting_tax.py index 6d53f6bba8..0cf1c91825 100644 --- a/modules/account_es/reporting_tax.py +++ b/modules/account_es/reporting_tax.py @@ -19,8 +19,7 @@ from sql.functions import CurrentTimestamp, Extract from sql.operators import Exists from trytond.i18n import gettext -from trytond.model import ModelSQL, ModelView, fields -from trytond.model.modelsql import convert_from +from trytond.model import ModelSQL, ModelView, convert_from, fields from trytond.modules.account.exceptions import FiscalYearNotFoundError from trytond.modules.account_eu.account import ECSalesList, ECSalesListContext from trytond.modules.currency.fields import Monetary diff --git a/modules/party/party.py b/modules/party/party.py index d5392a2ea8..d629bd9169 100644 --- a/modules/party/party.py +++ b/modules/party/party.py @@ -10,7 +10,7 @@ from trytond import backend from trytond.i18n import gettext from trytond.model import ( DeactivableMixin, Index, ModelSQL, ModelView, MultiValueMixin, Unique, - ValueMixin, fields, sequence_ordered) + ValueMixin, convert_from, fields, sequence_ordered) from trytond.model.exceptions import AccessError from trytond.pool import Pool from trytond.pyson import Bool, Eval @@ -937,18 +937,6 @@ class Erase(Wizard): Party = pool.get('party.party') cursor = Transaction().connection.cursor() - def convert_from(table, tables): - right, condition = tables[None] - if table: - table = table.join(right, condition=condition) - else: - table = right - for k, sub_tables in tables.items(): - if k is None: - continue - table = convert_from(table, sub_tables) - return table - resources = self.get_resources() parties = replacing = [self.ask.party] with inactive_records(): @@ -968,7 +956,7 @@ class Erase(Wizard): Model.write(records, {'active': False}) tables, where = Model.search_domain(domain, active_test=False) - from_ = convert_from(None, tables) + from_ = convert_from(None, tables, type_='INNER') table, _ = tables[None] query = from_.select(table.id, where=where) diff --git a/modules/party_relationship/party.py b/modules/party_relationship/party.py index a07408fd32..e46ac27a1b 100644 --- a/modules/party_relationship/party.py +++ b/modules/party_relationship/party.py @@ -9,7 +9,7 @@ from sql.aggregate import Min from sql.conditionals import Coalesce from trytond.config import config -from trytond.model import Index, ModelSQL, ModelView, fields +from trytond.model import Index, ModelSQL, ModelView, convert_from, fields from trytond.pool import Pool, PoolMeta from trytond.pyson import Eval, If from trytond.transaction import Transaction @@ -159,21 +159,9 @@ class RelationAll(Relation, ModelView): columns.append(column) reverse_columns.append(reverse_column) - def convert_from(table, tables): - right, condition = tables[None] - if table: - table = table.join(right, condition=condition) - else: - table = right - for k, sub_tables in tables.items(): - if k is None: - continue - table = convert_from(table, sub_tables) - return table - - query = convert_from(None, tables).select(*columns) - reverse_query = convert_from(None, reverse_tables).select( - *reverse_columns) + query = convert_from(None, tables, type_='INNER').select(*columns) + reverse_query = convert_from( + None, reverse_tables, type_='INNER').select(*reverse_columns) return Union(query, reverse_query, all_=True) @classmethod diff --git a/modules/product_cost_history/product.py b/modules/product_cost_history/product.py index cbde496f10..1f6971b317 100644 --- a/modules/product_cost_history/product.py +++ b/modules/product_cost_history/product.py @@ -5,26 +5,13 @@ from sql.aggregate import Max from sql.conditionals import Coalesce from sql.functions import CurrentTimestamp, LastValue -from trytond.model import ModelSQL, ModelView, fields +from trytond.model import ModelSQL, ModelView, convert_from, fields from trytond.modules.product import round_price from trytond.pool import Pool, PoolMeta from trytond.tools import timezone as tz from trytond.transaction import Transaction -def convert_from(table, tables): - right, condition = tables[None] - if table: - table = table.join(right, condition=condition) - else: - table = right - for k, sub_tables in tables.items(): - if k is None: - continue - table = convert_from(table, sub_tables) - return table - - class Product(metaclass=PoolMeta): __name__ = 'product.product' @@ -121,7 +108,7 @@ class ProductCostHistory(ModelSQL, ModelView): else: cost_price = cls.cost_price.sql_cast(move.cost_price) - move_history = convert_from(None, tables).select( + move_history = convert_from(None, tables, type_='INNER').select( (move.id * 2).as_('id'), move.effective_date.as_('date'), move.product.as_('product'), diff --git a/trytond/doc/ref/models.rst b/trytond/doc/ref/models.rst index 518fd30111..84ab81df88 100644 --- a/trytond/doc/ref/models.rst +++ b/trytond/doc/ref/models.rst @@ -741,6 +741,15 @@ Additional options are available: * ``begin``: optimize for constant pattern and anchored to the beginning of the string + +convert_from +------------ + +.. method:: convert_from(table, tables[, type_]) + +Return a SQL ``FromItem`` constructed by joining ``table`` and :ref:`tables +` using ``type_`` with default value ``LEFT``. + Workflow ======== diff --git a/trytond/trytond/model/__init__.py b/trytond/trytond/model/__init__.py index f1081192e2..bc16da678b 100644 --- a/trytond/trytond/model/__init__.py +++ b/trytond/trytond/model/__init__.py @@ -8,7 +8,7 @@ from .digits import DigitsMixin from .match import MatchMixin from .model import Model from .modelsingleton import ModelSingleton -from .modelsql import Check, Exclude, Index, ModelSQL, Unique +from .modelsql import Check, Exclude, Index, ModelSQL, Unique, convert_from from .modelstorage import EvalEnvironment, ModelStorage from .modelview import ModelView from .multivalue import MultiValueMixin, ValueMixin @@ -19,7 +19,7 @@ from .union import UnionMixin from .workflow import Workflow __all__ = ['Model', 'ModelView', 'ModelStorage', 'ModelSingleton', 'ModelSQL', - 'Check', 'Unique', 'Exclude', 'Index', + 'Check', 'Unique', 'Exclude', 'Index', 'convert_from', 'Workflow', 'DictSchemaMixin', 'MatchMixin', 'UnionMixin', 'dualmethod', 'MultiValueMixin', 'ValueMixin', 'SymbolMixin', 'DigitsMixin', 'EvalEnvironment', 'sequence_ordered', 'sort', 'DeactivableMixin', 'tree', diff --git a/trytond/trytond/model/modelsql.py b/trytond/trytond/model/modelsql.py index 6981f96041..854030823a 100644 --- a/trytond/trytond/model/modelsql.py +++ b/trytond/trytond/model/modelsql.py @@ -2061,17 +2061,17 @@ class ModelSQL(ModelStorage): transaction.lock_table(cls._table) -def convert_from(table, tables): +def convert_from(table, tables, type_='LEFT'): # Don't nested joins as SQLite doesn't support right, condition = tables[None] if table: - table = table.join(right, 'LEFT', condition) + table = table.join(right, type_, condition) else: table = right for k, sub_tables in tables.items(): if k is None: continue - table = convert_from(table, sub_tables) + table = convert_from(table, sub_tables, type_=type_) return table