From 59608914583fed17883d2470902595431bdfab42 Mon Sep 17 00:00:00 2001 From: sebalix Date: Wed, 4 Feb 2015 17:01:35 +0100 Subject: [PATCH] [FIX] Module 'auditlog' - Data dictionaries of fields and models are put in the cache instead of recordsets (a recordset contains a reference on the database cursor which is closed after each RPC query) --- auditlog/models/rule.py | 43 +++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/auditlog/models/rule.py b/auditlog/models/rule.py index 834fa7e1b..8a88299b7 100644 --- a/auditlog/models/rule.py +++ b/auditlog/models/rule.py @@ -130,7 +130,7 @@ class auditlog_rule(models.Model): if not self.pool.get(rule.model_id.model): # ignore rules for models not loadable currently continue - model_cache[rule.model_id.model] = rule.model_id + model_cache[rule.model_id.model] = rule.model_id.id model_model = self.env[rule.model_id.model] # CRUD # -> create @@ -297,7 +297,7 @@ class auditlog_rule(models.Model): res_name = model_model.browse(res_id).name_get() vals = { 'name': res_name and res_name[0] and res_name[0][1] or False, - 'model_id': self.pool._auditlog_model_cache[res_model].id, + 'model_id': self.pool._auditlog_model_cache[res_model], 'res_id': res_id, 'method': method, 'user_id': uid, @@ -315,12 +315,13 @@ class auditlog_rule(models.Model): cache = self.pool._auditlog_field_cache if field_name not in cache.get(model.model, {}): cache.setdefault(model.model, {}) - cache[model.model][field_name] = self.env['ir.model.fields']\ - .search( - [ - ('model_id', '=', model.id), - ('name', '=', field_name), - ]) + # We use 'search()' then 'read()' instead of the 'search_read()' + # to take advantage of the 'classic_write' loading + field_model = self.env['ir.model.fields'] + field = field_model.search( + [('model_id', '=', model.id), ('name', '=', field_name)]) + field_data = field.read(load='_classic_write')[0] + cache[model.model][field_name] = field_data return cache[model.model][field_name] def _create_log_line_on_write( @@ -341,19 +342,19 @@ class auditlog_rule(models.Model): 'write' operation. """ vals = { - 'field_id': field.id, + 'field_id': field['id'], 'log_id': log.id, - 'old_value': old_values[log.res_id][field.name], - 'old_value_text': old_values[log.res_id][field.name], - 'new_value': new_values[log.res_id][field.name], - 'new_value_text': new_values[log.res_id][field.name], + 'old_value': old_values[log.res_id][field['name']], + 'old_value_text': old_values[log.res_id][field['name']], + 'new_value': new_values[log.res_id][field['name']], + 'new_value_text': new_values[log.res_id][field['name']], } # for *2many fields, log the name_get - if field.relation and '2many' in field.ttype: - old_value_text = self.env[field.relation].browse( + if field['relation'] and '2many' in field['ttype']: + old_value_text = self.env[field['relation']].browse( vals['old_value']).name_get() vals['old_value_text'] = old_value_text - new_value_text = self.env[field.relation].browse( + new_value_text = self.env[field['relation']].browse( vals['new_value']).name_get() vals['new_value_text'] = new_value_text return vals @@ -375,15 +376,15 @@ class auditlog_rule(models.Model): 'create' operation. """ vals = { - 'field_id': field.id, + 'field_id': field['id'], 'log_id': log.id, 'old_value': False, 'old_value_text': False, - 'new_value': new_values[log.res_id][field.name], - 'new_value_text': new_values[log.res_id][field.name], + 'new_value': new_values[log.res_id][field['name']], + 'new_value_text': new_values[log.res_id][field['name']], } - if field.relation and '2many' in field.ttype: - new_value_text = self.env[field.relation].browse( + if field['relation'] and '2many' in field['ttype']: + new_value_text = self.env[field['relation']].browse( vals['new_value']).name_get() vals['new_value_text'] = new_value_text return vals