← Back to team overview

credativ team mailing list archive

[Branch ~credativ/openobject-addons/6.1] Rev 7058: [MERGE] audit trail fix

 

Merge authors:
  Jacob Hicks (credativ) (jah256)
------------------------------------------------------------
revno: 7058 [merge]
committer: Craig Gowing (credativ) <craig.gowing@xxxxxxxxxxxxxx>
branch nick: addons
timestamp: Wed 2014-01-08 08:07:48 +0000
message:
  [MERGE] audit trail fix
modified:
  audittrail/audittrail.py


--
lp:~credativ/openobject-addons/6.1
https://code.launchpad.net/~credativ/openobject-addons/6.1

Your team credativ is subscribed to branch lp:~credativ/openobject-addons/6.1.
To unsubscribe from this branch go to https://code.launchpad.net/~credativ/openobject-addons/6.1/+edit-subscription
=== modified file 'audittrail/audittrail.py'
--- audittrail/audittrail.py	2013-07-22 10:27:12 +0000
+++ audittrail/audittrail.py	2013-12-11 13:56:17 +0000
@@ -300,6 +300,20 @@
         self.process_data(cr, uid_orig, pool, res_ids, model, method, old_values, new_values, field_list)
         return res
 
+    def get_resource_audit_fields(self, cr, uid, resource_pool):
+        """
+        This function returns a list of tuples containing field names and respective
+        column objects.  Only fields with the _classic_write attribute as True are
+        included, as other field types do not need auditing.
+        """
+        audit_fields = []
+        resource_columns = resource_pool._all_columns
+        for resource_field in resource_columns:
+            # Only audit fields with _classic_write == True
+            if resource_columns.get(resource_field).column._classic_write:
+                audit_fields.append((resource_field,resource_columns.get(resource_field)))
+        return audit_fields
+        
     def get_data(self, cr, uid, pool, res_ids, model, method, prev_data=False):
         """
         This function simply read all the fields of the given res_ids, and also recurisvely on
@@ -327,8 +341,10 @@
         prev_data.append(key)
         data = {}
         resource_pool = pool.get(model.model)
-        # read all the fields of the given resources in super admin mode
-        for resource in resource_pool.read(cr, 1, res_ids):
+        audit_fields = self.get_resource_audit_fields(cr, uid, resource_pool)
+        audit_field_names = [audit_field[0] for audit_field in audit_fields]
+        # Only read specific fields which are being audited
+        for resource in resource_pool.read(cr, 1, res_ids, audit_field_names):
             values = {}
             values_text = {}
             resource_id = resource['id']
@@ -339,18 +355,6 @@
                 values[field] = resource[field]
                 # get the textual value of that field for this record
                 values_text[field] = self.get_value_text(cr, 1, pool, resource_pool, method, field, resource[field])
-
-                field_obj = resource_pool._all_columns.get(field).column
-                if field_obj._type in ('one2many'):
-                    # check if an audittrail rule apply in super admin mode
-                    if self.check_rules(cr, 1, field_obj._obj, method):
-                        # check if the model associated to a *2m field exists, in super admin mode
-                        x2m_model_ids = pool.get('ir.model').search(cr, 1, [('model', '=', field_obj._obj)])
-                        x2m_model_id = x2m_model_ids and x2m_model_ids[0] or False
-                        assert x2m_model_id, _("'%s' Model does not exist..." %(field_obj._obj))
-                        x2m_model = pool.get('ir.model').browse(cr, 1, x2m_model_id)
-                        #recursive call on x2m fields that need to be checked too
-                        data.update(self.get_data(cr, 1, pool, resource[field], x2m_model, method, prev_data))
             data[(model.id, resource_id)] = {'text':values_text, 'value': values}
         return data
 
@@ -393,28 +397,13 @@
         lines = {
             key: []
         }
-        # loop on all the fields
-        for field_name, field_definition in pool.get(model.model)._all_columns.items():
+        resource_pool = pool.get(model.model)
+        audit_fields = self.get_resource_audit_fields(cr, uid, resource_pool)
+        # Only loop on specific fields which are being audited
+        for field_name, field_definition in audit_fields:
             #if the field_list param is given, skip all the fields not in that list
             if (field_list and field_name not in field_list) or field_name in ('__last_update', 'id'):
                 continue
-            field_obj = field_definition.column
-            if field_obj._type in ('one2many'):
-                # checking if an audittrail rule apply in super admin mode
-                if self.check_rules(cr, 1, field_obj._obj, method):
-                    # checking if the model associated to a *2m field exists, in super admin mode
-                    x2m_model_ids = pool.get('ir.model').search(cr, 1, [('model', '=', field_obj._obj)])
-                    x2m_model_id = x2m_model_ids and x2m_model_ids[0] or False
-                    assert x2m_model_id, _("'%s' Model does not exist..." %(field_obj._obj))
-                    x2m_model = pool.get('ir.model').browse(cr, 1, x2m_model_id)
-                    # the resource_ids that need to be checked are the sum of both old and previous values (because we
-                    # need to log also creation or deletion in those lists).
-                    x2m_old_values_ids = old_values.get(key, {'value': {}})['value'].get(field_name, [])
-                    x2m_new_values_ids = new_values.get(key, {'value': {}})['value'].get(field_name, [])
-                    # We use list(set(...)) to remove duplicates.
-                    res_ids = list(set(x2m_old_values_ids + x2m_new_values_ids))
-                    for res_id in res_ids:
-                        lines.update(self.prepare_audittrail_log_line(cr, 1, pool, x2m_model, res_id, method, old_values, new_values, field_list, prev_data))
             # if the value value is different than the old value: record the change
             if key not in old_values or key not in new_values or old_values[key]['value'][field_name] != new_values[key]['value'][field_name]:
                 data = {