From 7a2ce7905a84dcf394c788508b56a7a577f78a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 24 Jul 2018 22:01:08 +0200 Subject: [PATCH] Add __table_handler__ to ModelSQL issue7581 review48531002 --- CHANGELOG | 1 + doc/ref/models/models.rst | 4 ++++ trytond/ir/action.py | 11 ++++------- trytond/ir/attachment.py | 4 +--- trytond/ir/cron.py | 4 +--- trytond/ir/model.py | 12 +++--------- trytond/ir/module.py | 4 ++-- trytond/ir/rule.py | 4 +--- trytond/ir/sequence.py | 5 ++--- trytond/ir/session.py | 4 +--- trytond/ir/translation.py | 8 +++----- trytond/ir/trigger.py | 7 ++----- trytond/ir/ui/view.py | 11 ++++------- trytond/model/modelsql.py | 12 ++++++++---- trytond/res/ir.py | 9 ++++----- trytond/res/user.py | 8 +++----- 16 files changed, 44 insertions(+), 64 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8d39e62a7f..ab63c46a64 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add __table_handler__ to ModelSQL * Support partial index * Allow to use SQL expression for index action * Add cache clean timeout configuration diff --git a/doc/ref/models/models.rst b/doc/ref/models/models.rst index 09bf2d7c07..c4611f0d2f 100644 --- a/doc/ref/models/models.rst +++ b/doc/ref/models/models.rst @@ -411,6 +411,10 @@ Class methods: Return a SQL Table instance for the history of Model. +.. classmethod:: ModelSQL.__table_handler__([module_name[, history]]) + + Return a TableHandler for the Model. + .. classmethod:: ModelSQL.table_query() Could be defined to use a custom SQL query instead of a table of the diff --git a/trytond/ir/action.py b/trytond/ir/action.py index 2070cfc7ee..8a543e35ec 100644 --- a/trytond/ir/action.py +++ b/trytond/ir/action.py @@ -120,10 +120,9 @@ class ActionKeyword(ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') super(ActionKeyword, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.index_action(['keyword', 'model'], 'add') def get_groups(self, name): @@ -504,7 +503,7 @@ class ActionReport(ActionMixin, ModelSQL, ModelView): transaction = Transaction() cursor = transaction.connection.cursor() - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) action_report = cls.__table__() # Migration from 1.0 report_name_uniq has been removed @@ -743,11 +742,10 @@ class ActionActWindow(ActionMixin, ModelSQL, ModelView): @classmethod def __register__(cls, module_name): cursor = Transaction().connection.cursor() - TableHandler = backend.get('TableHandler') act_window = cls.__table__() super(ActionActWindow, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.0: new search_value format cursor.execute(*act_window.update( @@ -933,9 +931,8 @@ class ActionActWindowView(DeactivableMixin, ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') super(ActionActWindowView, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 1.0 remove multi table.drop_column('multi') diff --git a/trytond/ir/attachment.py b/trytond/ir/attachment.py index 492224ac62..2500edd1aa 100644 --- a/trytond/ir/attachment.py +++ b/trytond/ir/attachment.py @@ -4,7 +4,6 @@ from sql import Null from sql.operators import Concat from ..model import ModelView, ModelSQL, fields, Unique -from .. import backend from ..transaction import Transaction from ..pyson import Eval from .resource import ResourceMixin @@ -64,12 +63,11 @@ class Attachment(ResourceMixin, ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') cursor = Transaction().connection.cursor() super(Attachment, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) attachment = cls.__table__() # Migration from 1.4 res_model and res_id merged into resource diff --git a/trytond/ir/cron.py b/trytond/ir/cron.py index ac7580c380..014c6e7091 100644 --- a/trytond/ir/cron.py +++ b/trytond/ir/cron.py @@ -12,7 +12,6 @@ from ast import literal_eval from ..model import ModelView, ModelSQL, DeactivableMixin, fields, dualmethod from ..transaction import Transaction from ..pool import Pool -from .. import backend from ..config import config from ..sendmail import sendmail @@ -76,12 +75,11 @@ class Cron(DeactivableMixin, ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') cursor = Transaction().connection.cursor() cron = cls.__table__() # Migration from 2.0: rename numbercall, doall and nextcall - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.column_rename('numbercall', 'number_calls') table.column_rename('doall', 'repeat_missed') table.column_rename('nextcall', 'next_call') diff --git a/trytond/ir/model.py b/trytond/ir/model.py index 0b8d8d35fd..29516d936e 100644 --- a/trytond/ir/model.py +++ b/trytond/ir/model.py @@ -20,7 +20,6 @@ from ..cache import Cache from ..pool import Pool from ..pyson import Bool, Eval, PYSONDecoder from ..rpc import RPC -from .. import backend from ..protocols.jsonrpc import JSONDecoder, JSONEncoder from ..tools import is_instance_method, cursor_dict, grouped_slice try: @@ -435,11 +434,9 @@ class ModelAccess(ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') - super(ModelAccess, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.6 (model, group) no more unique table.drop_constraint('model_group_uniq') @@ -619,11 +616,9 @@ class ModelFieldAccess(ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') - super(ModelFieldAccess, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.6 (field, group) no more unique table.drop_constraint('field_group_uniq') @@ -1121,13 +1116,12 @@ class ModelData(ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') cursor = Transaction().connection.cursor() model_data = cls.__table__() super(ModelData, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.6: remove inherit if table.column_exist('inherit'): diff --git a/trytond/ir/module.py b/trytond/ir/module.py index ea42116fed..fbd13614df 100644 --- a/trytond/ir/module.py +++ b/trytond/ir/module.py @@ -415,10 +415,10 @@ class ModuleConfigWizardItem(ModelSQL, ModelView): where=(model_data.model == 'ir.module.module.config_wizard.item'))) - table = TableHandler(cls, module_name) + table_h = cls.__table_handler__(module_name) # Migrate from 2.2 remove name - table.drop_column('name') + table_h.drop_column('name') super(ModuleConfigWizardItem, cls).__register__(module_name) diff --git a/trytond/ir/rule.py b/trytond/ir/rule.py index b1abbebcc4..e9fe2b3478 100644 --- a/trytond/ir/rule.py +++ b/trytond/ir/rule.py @@ -4,7 +4,6 @@ from ..model import ModelView, ModelSQL, fields, EvalEnvironment, Check from ..transaction import Transaction from ..cache import Cache from ..pool import Pool -from .. import backend from ..pyson import PYSONDecoder __all__ = [ @@ -108,9 +107,8 @@ class Rule(ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') super(Rule, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.6: replace field, operator and operand by domain table.not_null_action('field', action='remove') diff --git a/trytond/ir/sequence.py b/trytond/ir/sequence.py index 0a167a29f7..c2e84e0231 100644 --- a/trytond/ir/sequence.py +++ b/trytond/ir/sequence.py @@ -101,12 +101,11 @@ class Sequence(DeactivableMixin, ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') - table = TableHandler(cls, module_name) + table_h = cls.__table_handler__(module_name) transaction = Transaction() # Migration from 2.0 rename number_next into number_next_internal - table.column_rename('number_next', 'number_next_internal') + table_h.column_rename('number_next', 'number_next_internal') super(Sequence, cls).__register__(module_name) diff --git a/trytond/ir/session.py b/trytond/ir/session.py index c10adbb00d..d6ffc1f1fe 100644 --- a/trytond/ir/session.py +++ b/trytond/ir/session.py @@ -14,7 +14,6 @@ except ImportError: from trytond.model import ModelSQL, fields from trytond.config import config -from .. import backend __all__ = [ 'Session', 'SessionWizard', @@ -35,10 +34,9 @@ class Session(ModelSQL): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') super(Session, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.index_action('create_uid', 'add') @classmethod diff --git a/trytond/ir/translation.py b/trytond/ir/translation.py index e5a9faf0a6..9f92e322e3 100644 --- a/trytond/ir/translation.py +++ b/trytond/ir/translation.py @@ -22,7 +22,6 @@ from ..model import ModelView, ModelSQL, fields, Unique from ..wizard import Wizard, StateView, StateTransition, StateAction, \ Button from ..tools import file_open, reduce_ids, grouped_slice, cursor_dict -from .. import backend from ..pyson import PYSONEncoder, Eval from ..transaction import Transaction from ..pool import Pool @@ -97,11 +96,10 @@ class Translation(ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') transaction = Transaction() cursor = transaction.connection.cursor() ir_translation = cls.__table__() - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 1.8: new field src_md5 src_md5_exist = table.column_exist('src_md5') if not src_md5_exist: @@ -124,7 +122,7 @@ class Translation(ModelSQL, ModelView): cls.write([translation], { 'src_md5': src_md5, }) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.not_null_action('src_md5', action='add') # Migration from 2.2 and 2.8 @@ -138,7 +136,7 @@ class Translation(ModelSQL, ModelView): ['report'], where=ir_translation.type == 'odt')) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.index_action(['lang', 'type', 'name'], 'add') @classmethod diff --git a/trytond/ir/trigger.py b/trytond/ir/trigger.py index 16b9a3a44c..03304ac171 100644 --- a/trytond/ir/trigger.py +++ b/trytond/ir/trigger.py @@ -9,7 +9,6 @@ from ..model import ( ModelView, ModelSQL, DeactivableMixin, fields, EvalEnvironment, Check) from ..pyson import Eval, PYSONDecoder from ..tools import grouped_slice -from .. import backend from ..tools import reduce_ids from ..transaction import Transaction from ..cache import Cache @@ -73,9 +72,8 @@ class Trigger(DeactivableMixin, ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') cursor = Transaction().connection.cursor() - table = TableHandler(cls, module_name) + table = cls.__table_handler__(cls, module_name) sql_table = cls.__table__() super(Trigger, cls).__register__(module_name) @@ -300,8 +298,7 @@ class TriggerLog(ModelSQL): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') super(TriggerLog, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(cls, module_name) table.index_action(['trigger', 'record_id'], 'add') diff --git a/trytond/ir/ui/view.py b/trytond/ir/ui/view.py index 6ad996a50f..ea523a9a11 100644 --- a/trytond/ir/ui/view.py +++ b/trytond/ir/ui/view.py @@ -6,7 +6,6 @@ import json from lxml import etree from trytond.model import ModelView, ModelSQL, fields -from trytond import backend from trytond.pyson import Eval, Bool, PYSONDecoder, If from trytond.tools import file_open from trytond.transaction import Transaction @@ -81,8 +80,7 @@ class View(ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.4 arch moved into data if table.column_exist('arch'): @@ -91,7 +89,7 @@ class View(ModelSQL, ModelView): super(View, cls).__register__(module_name) # New instance to refresh definition - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 1.0 arch no more required table.not_null_action('arch', action='remove') @@ -331,15 +329,14 @@ class ViewTreeState(ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.8: table name changed table.table_rename('ir_ui_view_tree_expanded_state', cls._table) super(ViewTreeState, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.index_action(['model', 'domain', 'user', 'child_name'], 'add') @staticmethod diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 1d57fd0421..cea3b6ab93 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -190,6 +190,11 @@ class ModelSQL(ModelStorage): raise ValueError('No history table') return Table(cls._table + '__history') + @classmethod + def __table_handler__(cls, module_name=None, history=False): + TableHandler = backend.get('TableHandler') + return TableHandler(cls, module_name, history=history) + @classmethod def __register__(cls, module_name): sql_table = cls.__table__() @@ -203,9 +208,9 @@ class ModelSQL(ModelStorage): pool = Pool() # create/update table in the database - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) if cls._history: - history_table = TableHandler(cls, module_name, history=True) + history_table = cls.__table_handler__(module_name, history=True) history_table.index_action('id', action='add') for field_name, field in cls._fields.items(): @@ -303,9 +308,8 @@ class ModelSQL(ModelStorage): @classmethod def _update_history_table(cls): - TableHandler = backend.get('TableHandler') if cls._history: - history_table = TableHandler(cls, history=True) + history_table = cls.__table_handler__(history=True) for field_name, field in cls._fields.items(): if not field.sql_type(): continue diff --git a/trytond/res/ir.py b/trytond/res/ir.py index 3c2cd37843..ca6d4597d6 100644 --- a/trytond/res/ir.py +++ b/trytond/res/ir.py @@ -31,7 +31,7 @@ class UIMenuGroup(ModelSQL): transaction.database.sequence_rename(transaction.connection, 'ir_ui_menu_group_rel_id_seq', cls._table + '_id_seq') # Migration from 2.0 menu_id and gid renamed into menu group - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.column_rename('menu_id', 'menu') table.column_rename('gid', 'group') super(UIMenuGroup, cls).__register__(module_name) @@ -73,7 +73,7 @@ class ActionGroup(ModelSQL): transaction.database.sequence_rename(transaction.connection, 'ir_action_group_rel_id_seq', cls._table + '_id_seq') # Migration from 2.0 action_id and gid renamed into action and group - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.column_rename('action_id', 'action') table.column_rename('gid', 'group') super(ActionGroup, cls).__register__(module_name) @@ -122,13 +122,12 @@ class ModelFieldGroup(ModelSQL): @classmethod def __register__(cls, module_name): TableHandler = backend.get('TableHandler') - database = Transaction().database transaction = Transaction() # Migration from 1.0 table name change TableHandler.table_rename('ir_model_field_group_rel', cls._table) transaction.database.sequence_rename(transaction.connection, 'ir_model_field_group_rel_id_seq', cls._table + '_id_seq') - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.6: field_id and group_id renamed to field and group table.column_rename('field_id', 'field') table.column_rename('group_id', 'group') @@ -194,7 +193,7 @@ class RuleGroupGroup(ModelSQL): 'group_rule_group_rel_id_seq', cls._table + '_id_seq') # Migration from 2.0 rule_group_id and group_id renamed into rule_group # and group - table = TableHandler(cls, module_name) + table = cls.__table_handler__(cls, module_name) table.column_rename('rule_group_id', 'rule_group') table.column_rename('group_id', 'group') super(RuleGroupGroup, cls).__register__(module_name) diff --git a/trytond/res/user.py b/trytond/res/user.py index b622b909d7..1f729f9b1b 100644 --- a/trytond/res/user.py +++ b/trytond/res/user.py @@ -187,10 +187,9 @@ class User(DeactivableMixin, ModelSQL, ModelView): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') cursor = Transaction().connection.cursor() super(User, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 1.6 @@ -732,9 +731,8 @@ class LoginAttempt(ModelSQL): @classmethod def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') super(LoginAttempt, cls).__register__(module_name) - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) # Migration from 2.8: remove user table.drop_column('user') @@ -858,7 +856,7 @@ class UserGroup(ModelSQL): transaction.database.sequence_rename(transaction.connection, 'res_group_user_rel_id_seq', cls._table + '_id_seq') # Migration from 2.0 uid and gid rename into user and group - table = TableHandler(cls, module_name) + table = cls.__table_handler__(module_name) table.column_rename('uid', 'user') table.column_rename('gid', 'group') super(UserGroup, cls).__register__(module_name)