[IMP] base_action_rule: simply reload the registry after modifying rules
This simplifies the registry "patching", as it is now always made on a clean registry.
This commit is contained in:
@@ -7,6 +7,7 @@ import logging
|
|||||||
|
|
||||||
import openerp
|
import openerp
|
||||||
from openerp import SUPERUSER_ID
|
from openerp import SUPERUSER_ID
|
||||||
|
from openerp.modules.registry import RegistryManager
|
||||||
from openerp.osv import fields, osv
|
from openerp.osv import fields, osv
|
||||||
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
|
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
|
||||||
from openerp.tools.safe_eval import safe_eval as eval
|
from openerp.tools.safe_eval import safe_eval as eval
|
||||||
@@ -88,6 +89,9 @@ class base_action_rule(osv.osv):
|
|||||||
help="Comma-separated list of field names that triggers the onchange."),
|
help="Comma-separated list of field names that triggers the onchange."),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# which fields have an impact on the registry
|
||||||
|
CRITICAL_FIELDS = ['model_id', 'active', 'kind', 'on_change_fields']
|
||||||
|
|
||||||
_defaults = {
|
_defaults = {
|
||||||
'active': True,
|
'active': True,
|
||||||
'trg_date_range_type': 'day',
|
'trg_date_range_type': 'day',
|
||||||
@@ -298,6 +302,8 @@ class base_action_rule(osv.osv):
|
|||||||
return updated
|
return updated
|
||||||
|
|
||||||
def _update_cron(self, cr, uid, context=None):
|
def _update_cron(self, cr, uid, context=None):
|
||||||
|
""" Activate the cron job depending on whether there exists action rules
|
||||||
|
based on time conditions. """
|
||||||
try:
|
try:
|
||||||
cron = self.pool['ir.model.data'].get_object(
|
cron = self.pool['ir.model.data'].get_object(
|
||||||
cr, uid, 'base_action_rule', 'ir_cron_crm_action', context=context)
|
cr, uid, 'base_action_rule', 'ir_cron_crm_action', context=context)
|
||||||
@@ -306,36 +312,32 @@ class base_action_rule(osv.osv):
|
|||||||
|
|
||||||
return cron.toggle(model=self._name, domain=[('kind', '=', 'on_time')])
|
return cron.toggle(model=self._name, domain=[('kind', '=', 'on_time')])
|
||||||
|
|
||||||
@openerp.api.one
|
def _update_registry(self, cr, uid, context=None):
|
||||||
def _purge_custom_onchange(self):
|
""" Update the registry after a modification on action rules. """
|
||||||
if self.kind != 'on_change':
|
if self.pool.ready:
|
||||||
return
|
# for the sake of simplicity, simply force the registry to reload
|
||||||
model = self.env[self.model_id.model]
|
cr.commit()
|
||||||
for field_name in self.on_change_fields.split(","):
|
openerp.api.Environment.reset()
|
||||||
field_name = field_name.strip()
|
RegistryManager.new(cr.dbname)
|
||||||
if model._onchange_methods.get(field_name):
|
RegistryManager.signal_registry_change(cr.dbname)
|
||||||
model._onchange_methods[field_name] = [method for method in model._onchange_methods[field_name] if method.func_name != 'base_action_rule_onchange']
|
|
||||||
|
|
||||||
def create(self, cr, uid, vals, context=None):
|
def create(self, cr, uid, vals, context=None):
|
||||||
res_id = super(base_action_rule, self).create(cr, uid, vals, context=context)
|
res_id = super(base_action_rule, self).create(cr, uid, vals, context=context)
|
||||||
if self._register_hook(cr, [res_id]):
|
|
||||||
openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname)
|
|
||||||
self._update_cron(cr, uid, context=context)
|
self._update_cron(cr, uid, context=context)
|
||||||
|
self._update_registry(cr, uid, context=context)
|
||||||
return res_id
|
return res_id
|
||||||
|
|
||||||
def write(self, cr, uid, ids, vals, context=None):
|
def write(self, cr, uid, ids, vals, context=None):
|
||||||
if isinstance(ids, (int, long)):
|
|
||||||
ids = [ids]
|
|
||||||
self._purge_custom_onchange(cr, uid, ids, context=context)
|
|
||||||
super(base_action_rule, self).write(cr, uid, ids, vals, context=context)
|
super(base_action_rule, self).write(cr, uid, ids, vals, context=context)
|
||||||
if self._register_hook(cr, ids):
|
if set(vals) & set(self.CRITICAL_FIELDS):
|
||||||
openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname)
|
self._update_cron(cr, uid, context=context)
|
||||||
self._update_cron(cr, uid, context=context)
|
self._update_registry(cr, uid, context=context)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def unlink(self, cr, uid, ids, context=None):
|
def unlink(self, cr, uid, ids, context=None):
|
||||||
res = super(base_action_rule, self).unlink(cr, uid, ids, context=context)
|
res = super(base_action_rule, self).unlink(cr, uid, ids, context=context)
|
||||||
self._update_cron(cr, uid, context=context)
|
self._update_cron(cr, uid, context=context)
|
||||||
|
self._update_registry(cr, uid, context=context)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def onchange_model_id(self, cr, uid, ids, model_id, context=None):
|
def onchange_model_id(self, cr, uid, ids, model_id, context=None):
|
||||||
|
|||||||
Reference in New Issue
Block a user