From 51117db34435806a4443aa05ff6d17a2e48fe1ba Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Tue, 16 Aug 2011 11:57:55 +0200 Subject: [PATCH] [REF] osv: replace isinstance(m,osv_memory) by m.is_transient(). bzr revid: vmt@openerp.com-20110816095755-2g5cblolcvchtewx --- openerp/addons/base/ir/ir_model.py | 4 ++-- openerp/addons/base/ir/ir_rule.py | 2 +- openerp/modules/loading.py | 8 ++++---- openerp/osv/orm.py | 8 ++++++++ openerp/osv/osv.py | 29 ++++++++++++++++++----------- openerp/tools/yaml_import.py | 2 +- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/openerp/addons/base/ir/ir_model.py b/openerp/addons/base/ir/ir_model.py index 0f60ef44ab9..9294bd4c0ff 100644 --- a/openerp/addons/base/ir/ir_model.py +++ b/openerp/addons/base/ir/ir_model.py @@ -63,7 +63,7 @@ class ir_model(osv.osv): models = self.browse(cr, uid, ids, context=context) res = dict.fromkeys(ids) for model in models: - res[model.id] = isinstance(self.pool.get(model.model), osv.osv_memory) + res[model.id] = self.pool.get(model.model).is_transient() return res def _search_osv_memory(self, cr, uid, model, name, domain, context=None): @@ -483,7 +483,7 @@ class ir_model_access(osv.osv): # osv_memory objects can be read by everyone, as they only return # results that belong to the current user (except for superuser) model_obj = self.pool.get(model_name) - if isinstance(model_obj, osv.osv_memory): + if model_obj.is_transient(): return True # We check if a specific rule exists diff --git a/openerp/addons/base/ir/ir_rule.py b/openerp/addons/base/ir/ir_rule.py index 2a36ee9ccc5..b18999da0a3 100644 --- a/openerp/addons/base/ir/ir_rule.py +++ b/openerp/addons/base/ir/ir_rule.py @@ -68,7 +68,7 @@ class ir_rule(osv.osv): return res def _check_model_obj(self, cr, uid, ids, context=None): - return not any(isinstance(self.pool.get(rule.model_id.model), osv.osv_memory) for rule in self.browse(cr, uid, ids, context)) + return not any(self.pool.get(rule.model_id.model).is_transient() for rule in self.browse(cr, uid, ids, context)) _columns = { 'name': fields.char('Name', size=128, select=1), diff --git a/openerp/modules/loading.py b/openerp/modules/loading.py index 45a466e5462..85d86e8d7e1 100644 --- a/openerp/modules/loading.py +++ b/openerp/modules/loading.py @@ -339,16 +339,16 @@ def load_modules(db, force_demo=False, status=None, update_module=False): cr.execute("""select model,name from ir_model where id NOT IN (select distinct model_id from ir_model_access)""") for (model, name) in cr.fetchall(): model_obj = pool.get(model) - if model_obj and not isinstance(model_obj, osv.osv.osv_memory): - logger.notifyChannel('init', netsvc.LOG_WARNING, 'object %s (%s) has no access rules!' % (model, name)) + if model_obj and not model_obj.is_transient(): + logger.notifyChannel('init', netsvc.LOG_WARNING, 'The model %s (%s) has no access rules!' % (model, name)) # Temporary warning while we remove access rights on osv_memory objects, as they have # been replaced by owner-only access rights cr.execute("""select distinct mod.model, mod.name from ir_model_access acc, ir_model mod where acc.model_id = mod.id""") for (model, name) in cr.fetchall(): model_obj = pool.get(model) - if isinstance(model_obj, osv.osv.osv_memory): - logger.notifyChannel('init', netsvc.LOG_WARNING, 'In-memory object %s (%s) should not have explicit access rules!' % (model, name)) + if model_obj.is_transient(): + logger.notifyChannel('init', netsvc.LOG_WARNING, 'The transient model %s (%s) should not have explicit access rules!' % (model, name)) cr.execute("SELECT model from ir_model") for (model,) in cr.fetchall(): diff --git a/openerp/osv/orm.py b/openerp/osv/orm.py index cdb8a96b1f9..6a08b937676 100644 --- a/openerp/osv/orm.py +++ b/openerp/osv/orm.py @@ -4472,5 +4472,13 @@ class Model(object): results[k] = '' return results + def is_transient(self): + """ Return whether the model is transient. + + See TransientModel. + + """ + return self._transient + # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/osv/osv.py b/openerp/osv/osv.py index cefccc12ef2..71b1781d335 100644 --- a/openerp/osv/osv.py +++ b/openerp/osv/osv.py @@ -202,7 +202,12 @@ class object_proxy(netsvc.Service): class osv_memory(Model): - """ Deprecated class. """ + """ Model for transient records. + + A TransientModel works similarly to a regular Model but the assiociated + records will be cleaned automatically from the database after some time. + + """ __metaclass__ = MetaModel _register = False # Set to false if the model shouldn't be automatically discovered. _transient = True @@ -212,7 +217,7 @@ class osv_memory(Model): def __init__(self, pool, cr): super(osv_memory, self).__init__(pool, cr) - self.check_id = 0 + self.check_count = 0 self._max_count = config.get('osv_memory_count_limit') self._max_hours = config.get('osv_memory_age_limit') cr.execute('delete from wkf_instance where res_type=%s', (self._name,)) @@ -243,16 +248,18 @@ class osv_memory(Model): self.unlink(cr, openerp.SUPERUSER, ids) def vacuum(self, cr, uid, force=False): - """ Run the vacuum cleaner, i.e. unlink old records from the - virtual osv_memory tables if the "max count" or "max age" conditions are enabled - and have been reached. This method can be called very often (e.g. everytime a record - is created), but will only actually trigger the cleanup process once out of - "_check_time" times (by default once out of 20 calls).""" - self.check_id += 1 - if (not force) and (self.check_id % self._check_time): - self.check_id = 0 + """ Clean the TransientModel records. + + This unlinks old records from the transient model tables whenever the + "_max_count" or "_max_age" conditions (if any) are reached. + Actual cleaning will happen only once every "_check_time" calls. + This means this method can be called frequently called (e.g. whenever + a new record is created). + """ + self.check_count += 1 + if (not force) and (self.check_count % self._check_time): + self.check_count = 0 return True - tounlink = [] # Age-based expiration if self._max_hours: diff --git a/openerp/tools/yaml_import.py b/openerp/tools/yaml_import.py index 00bb79f0499..b45a941bfc2 100644 --- a/openerp/tools/yaml_import.py +++ b/openerp/tools/yaml_import.py @@ -305,7 +305,7 @@ class YamlInterpreter(object): import openerp.osv as osv record, fields = node.items()[0] model = self.get_model(record.model) - if isinstance(model, osv.osv.osv_memory): + if model.is_transient(): record_dict=self.create_osv_memory_record(record, fields) else: self.validate_xml_id(record.id)