From a6b025d6d9a10f1f6594e7cd1aa5367b4e6c3fcb Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 8 Jul 2014 10:16:16 +0200 Subject: [PATCH 1/6] [FIX] models, fields: add model dependencies for models backed up by sql views --- addons/hr_timesheet_sheet/hr_timesheet_sheet.py | 5 +++++ openerp/fields.py | 14 +++++++++++--- openerp/models.py | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py index a2845171212..7bbcda29c09 100644 --- a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py @@ -532,6 +532,11 @@ class hr_timesheet_sheet_sheet_day(osv.osv): 'total_attendance': fields.float('Attendance', readonly=True), 'total_difference': fields.float('Difference', readonly=True), } + _depends = { + 'hr.analytic.timesheet': ['line_id', 'sheet_id'], + 'account.analytic.line': ['date', 'unit_amount'], + 'hr.attendance': ['name', 'action', 'sheet_id'], + } def init(self, cr): cr.execute("""create or replace view hr_timesheet_sheet_sheet_day as diff --git a/openerp/fields.py b/openerp/fields.py index ede066e37fe..f8b5bd4189c 100644 --- a/openerp/fields.py +++ b/openerp/fields.py @@ -366,9 +366,17 @@ class Field(object): else: self._setup_regular(env) - # put invalidation/recomputation triggers on dependencies + # put invalidation/recomputation triggers on field dependencies + model = env[self.model_name] for path in self.depends: - self._setup_dependency([], env[self.model_name], path.split('.')) + self._setup_dependency([], model, path.split('.')) + + # put invalidation triggers on model dependencies + for dep_model_name, field_names in model._depends.iteritems(): + dep_model = env[dep_model_name] + for field_name in field_names: + field = dep_model._fields[field_name] + field._triggers.add((self, None)) # # Setup of related fields @@ -800,7 +808,7 @@ class Field(object): # invalidate the fields that depend on self, and prepare recomputation spec = [(self, records._ids)] for field, path in self._triggers: - if field.store: + if path and field.store: # don't move this line to function top, see log env = records.env(user=SUPERUSER_ID, context={'active_test': False}) target = env[field.model_name].search([(path, 'in', records.ids)]) diff --git a/openerp/models.py b/openerp/models.py index 6f017b617b1..3b8e1a93648 100644 --- a/openerp/models.py +++ b/openerp/models.py @@ -326,6 +326,10 @@ class BaseModel(object): _log_create = False _sql_constraints = [] + # model dependencies, for models backed up by sql views: + # {model_name: field_names, ...} + _depends = {} + CONCURRENCY_CHECK_FIELD = '__last_update' def log(self, cr, uid, id, message, secondary=False, context=None): From 34dac62d32a759d10615fedbf43228d9642567a1 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 8 Jul 2014 14:52:23 +0200 Subject: [PATCH 2/6] [IMP] models: add an extension mechanism for attribute _depends on models --- openerp/models.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/openerp/models.py b/openerp/models.py index 3b8e1a93648..bd9f4f49272 100644 --- a/openerp/models.py +++ b/openerp/models.py @@ -589,6 +589,10 @@ class BaseModel(object): inherits = dict(parent_class._inherits) inherits.update(cls._inherits) + depends = dict(parent_class._depends) + for m, fs in cls._depends.iteritems(): + depends.setdefault(m, []).extend(fs) + old_constraints = parent_class._constraints new_constraints = cls._constraints # filter out from old_constraints the ones overridden by a @@ -608,6 +612,7 @@ class BaseModel(object): '_columns': columns, '_defaults': defaults, '_inherits': inherits, + '_depends': depends, '_constraints': constraints, '_sql_constraints': sql_constraints, } @@ -621,6 +626,7 @@ class BaseModel(object): '_columns': dict(cls._columns), '_defaults': dict(cls._defaults), '_inherits': dict(cls._inherits), + '_depends': dict(cls._depends), '_constraints': list(cls._constraints), '_sql_constraints': list(cls._sql_constraints), '_original_module': original_module, From 2398f480cddb8d3c36bf1336a3d267ba07a2d5f9 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 8 Jul 2014 15:32:38 +0200 Subject: [PATCH 3/6] [IMP] addons: add _depends on models backed up by an sql view and used by other models --- .../account/report/account_invoice_report.py | 18 ++++++++++++++++++ .../account_analytic_analysis.py | 11 +++++++++++ .../wizard/account_followup_print.py | 8 ++++++++ .../report/hr_evaluation_report.py | 8 ++++++++ .../hr_timesheet_sheet/hr_timesheet_sheet.py | 10 ++++++++-- addons/sale/report/invoice_report.py | 3 +++ 6 files changed, 56 insertions(+), 2 deletions(-) diff --git a/addons/account/report/account_invoice_report.py b/addons/account/report/account_invoice_report.py index 8fecf85ee0b..7a5ce9c0fb1 100644 --- a/addons/account/report/account_invoice_report.py +++ b/addons/account/report/account_invoice_report.py @@ -98,6 +98,24 @@ class account_invoice_report(osv.osv): } _order = 'date desc' + _depends = { + 'account.invoice': [ + 'account_id', 'amount_total', 'commercial_partner_id', 'company_id', + 'currency_id', 'date_due', 'date_invoice', 'fiscal_position', + 'journal_id', 'partner_bank_id', 'partner_id', 'payment_term', + 'period_id', 'residual', 'state', 'type', 'user_id', + ], + 'account.invoice.line': [ + 'account_id', 'invoice_id', 'price_subtotal', 'product_id', + 'quantity', 'uos_id', + ], + 'product.product': ['product_tmpl_id'], + 'product.template': ['categ_id'], + 'product.uom': ['category_id', 'factor', 'name', 'uom_type'], + 'res.currency.rate': ['currency_id', 'name'], + 'res.partner': ['country_id'], + } + def _select(self): select_str = """ SELECT sub.id, sub.date, sub.product_id, sub.partner_id, sub.country_id, diff --git a/addons/account_analytic_analysis/account_analytic_analysis.py b/addons/account_analytic_analysis/account_analytic_analysis.py index 3b5819968a3..d1647f77390 100644 --- a/addons/account_analytic_analysis/account_analytic_analysis.py +++ b/addons/account_analytic_analysis/account_analytic_analysis.py @@ -804,6 +804,12 @@ class account_analytic_account_summary_user(osv.osv): 'user': fields.many2one('res.users', 'User'), } + _depends = { + 'res.users': ['id'], + 'account.analytic.line': ['account_id', 'journal_id', 'unit_amount', 'user_id'], + 'account.analytic.journal': ['type'], + } + def init(self, cr): openerp.tools.sql.drop_view_if_exists(cr, 'account_analytic_analysis_summary_user') cr.execute('''CREATE OR REPLACE VIEW account_analytic_analysis_summary_user AS ( @@ -837,6 +843,11 @@ class account_analytic_account_summary_month(osv.osv): 'month': fields.char('Month', size=32, readonly=True), } + _depends = { + 'account.analytic.line': ['account_id', 'date', 'journal_id', 'month', 'unit_amount'], + 'account.analytic.journal': ['type'], + } + def init(self, cr): openerp.tools.sql.drop_view_if_exists(cr, 'account_analytic_analysis_summary_month') cr.execute('CREATE VIEW account_analytic_analysis_summary_month AS (' \ diff --git a/addons/account_followup/wizard/account_followup_print.py b/addons/account_followup/wizard/account_followup_print.py index 83100723edb..550ca753059 100644 --- a/addons/account_followup/wizard/account_followup_print.py +++ b/addons/account_followup/wizard/account_followup_print.py @@ -42,6 +42,14 @@ class account_followup_stat_by_partner(osv.osv): 'company_id': fields.many2one('res.company', 'Company', readonly=True), } + _depends = { + 'account.move.line': [ + 'account_id', 'company_id', 'credit', 'date', 'debit', + 'followup_date', 'followup_line_id', 'partner_id', 'reconcile_id', + ], + 'account.account': ['active', 'type'], + } + def init(self, cr): tools.drop_view_if_exists(cr, 'account_followup_stat_by_partner') # Here we don't have other choice but to create a virtual ID based on the concatenation diff --git a/addons/hr_evaluation/report/hr_evaluation_report.py b/addons/hr_evaluation/report/hr_evaluation_report.py index ce6f381fb60..32ec3f43734 100644 --- a/addons/hr_evaluation/report/hr_evaluation_report.py +++ b/addons/hr_evaluation/report/hr_evaluation_report.py @@ -59,6 +59,14 @@ class hr_evaluation_report(osv.Model): } _order = 'create_date desc' + _depends = { + 'hr.evaluation.interview': ['evaluation_id', 'id', 'request_id'], + 'hr_evaluation.evaluation': [ + 'create_date', 'date', 'date_close', 'employee_id', 'plan_id', + 'rating', 'state', + ], + } + def init(self, cr): tools.drop_view_if_exists(cr, 'hr_evaluation_report') cr.execute(""" diff --git a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py index 7bbcda29c09..96be836bff1 100644 --- a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py @@ -533,9 +533,9 @@ class hr_timesheet_sheet_sheet_day(osv.osv): 'total_difference': fields.float('Difference', readonly=True), } _depends = { - 'hr.analytic.timesheet': ['line_id', 'sheet_id'], 'account.analytic.line': ['date', 'unit_amount'], - 'hr.attendance': ['name', 'action', 'sheet_id'], + 'hr.analytic.timesheet': ['line_id', 'sheet_id'], + 'hr.attendance': ['action', 'name', 'sheet_id'], } def init(self, cr): @@ -607,6 +607,12 @@ class hr_timesheet_sheet_sheet_account(osv.osv): 'invoice_rate': fields.many2one('hr_timesheet_invoice.factor', 'Invoice rate', readonly=True), } + _depends = { + 'account.analytic.line': ['account_id', 'date', 'to_invoice', 'unit_amount', 'user_id'], + 'hr.analytic.timesheet': ['line_id'], + 'hr_timesheet_sheet.sheet': ['date_from', 'date_to', 'user_id'], + } + def init(self, cr): cr.execute("""create or replace view hr_timesheet_sheet_sheet_account as ( select diff --git a/addons/sale/report/invoice_report.py b/addons/sale/report/invoice_report.py index c12db9bfdb0..e66daddcfa8 100644 --- a/addons/sale/report/invoice_report.py +++ b/addons/sale/report/invoice_report.py @@ -25,6 +25,9 @@ class account_invoice_report(osv.osv): _columns = { 'section_id': fields.many2one('crm.case.section', 'Sales Team'), } + _depends = { + 'account.invoice': ['section_id'], + } def _select(self): return super(account_invoice_report, self)._select() + ", sub.section_id as section_id" From b05cf32b8f1170f7bbc99b7db9348bc806138b41 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 8 Jul 2014 15:47:44 +0200 Subject: [PATCH 4/6] [IMP] fields: add missing case for invalidating fields, when path is None --- openerp/fields.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openerp/fields.py b/openerp/fields.py index f8b5bd4189c..d4a880c5f43 100644 --- a/openerp/fields.py +++ b/openerp/fields.py @@ -833,9 +833,9 @@ class Field(object): if path == 'id': target = records - computed else: - for record in target.browse(env.cache[field]) - computed: - if record._mapped_cache(path) & records: - target += record + target = target.browse(env.cache[field]) - computed + if path: + target = target.filtered(lambda rec: rec._mapped_cache(path) & records) if target: spec.append((field, target._ids)) From 603bde1b1b5ecb04fd9c65cdf2a279a4e0c50556 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 8 Jul 2014 15:52:02 +0200 Subject: [PATCH 5/6] [IMP] fields: improve code in former commit --- openerp/fields.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/openerp/fields.py b/openerp/fields.py index d4a880c5f43..be5b58ffcf4 100644 --- a/openerp/fields.py +++ b/openerp/fields.py @@ -832,10 +832,13 @@ class Field(object): computed = target.browse(env.computed[field]) if path == 'id': target = records - computed + elif path: + target = (target.browse(env.cache[field]) - computed).filtered( + lambda rec: rec._mapped_cache(path) & records + ) else: target = target.browse(env.cache[field]) - computed - if path: - target = target.filtered(lambda rec: rec._mapped_cache(path) & records) + if target: spec.append((field, target._ids)) From 2d39a4fadaeb79155f8397496ddc88dde38e2c7a Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 8 Jul 2014 15:55:42 +0200 Subject: [PATCH 6/6] [FIX] account_analytic_analysis: fix _depends --- addons/account_analytic_analysis/account_analytic_analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/account_analytic_analysis/account_analytic_analysis.py b/addons/account_analytic_analysis/account_analytic_analysis.py index d1647f77390..d0eb8c4765d 100644 --- a/addons/account_analytic_analysis/account_analytic_analysis.py +++ b/addons/account_analytic_analysis/account_analytic_analysis.py @@ -844,7 +844,7 @@ class account_analytic_account_summary_month(osv.osv): } _depends = { - 'account.analytic.line': ['account_id', 'date', 'journal_id', 'month', 'unit_amount'], + 'account.analytic.line': ['account_id', 'date', 'journal_id', 'unit_amount'], 'account.analytic.journal': ['type'], }