[IMP] mail: mail_message.record_name is now store=True, removing a lot of access...
authorThibault Delavallée <tde@openerp.com>
Thu, 25 Oct 2012 15:11:23 +0000 (17:11 +0200)
committerThibault Delavallée <tde@openerp.com>
Thu, 25 Oct 2012 15:11:23 +0000 (17:11 +0200)
bzr revid: tde@openerp.com-20121025151123-s3qiodtf8xm7huvq

addons/mail/mail_message.py
addons/mail/mail_thread.py

index 94cc856..c310969 100644 (file)
@@ -57,17 +57,13 @@ class mail_message(osv.Model):
         return name[:self._message_record_name_length] + '...'
 
     def _get_record_name(self, cr, uid, ids, name, arg, context=None):
         return name[:self._message_record_name_length] + '...'
 
     def _get_record_name(self, cr, uid, ids, name, arg, context=None):
-        """ Return the related document name, using name_get. It is included in
-            a try/except statement, because if uid cannot read the related
-            document, he should see a void string instead of crashing. """
+        """ Return the related document name, using name_get. It is done using
+            SUPERUSER_ID, to be sure to have the record name correctly stored. """
         result = dict.fromkeys(ids, False)
         for message in self.read(cr, uid, ids, ['model', 'res_id'], context=context):
         result = dict.fromkeys(ids, False)
         for message in self.read(cr, uid, ids, ['model', 'res_id'], context=context):
-            if not message['model'] or not message['res_id']:
+            if not message.get('model') or not message.get('res_id'):
                 continue
                 continue
-            try:
-                result[message['id']] = self._shorten_name(self.pool.get(message['model']).name_get(cr, uid, [message['res_id']], context=context)[0][1])
-            except (orm.except_orm, osv.except_osv):
-                pass
+            result[message['id']] = self._shorten_name(self.pool.get(message['model']).name_get(cr, SUPERUSER_ID, [message['res_id']], context=context)[0][1])
         return result
 
     def _get_to_read(self, cr, uid, ids, name, arg, context=None):
         return result
 
     def _get_to_read(self, cr, uid, ids, name, arg, context=None):
@@ -128,8 +124,8 @@ class mail_message(osv.Model):
         'child_ids': fields.one2many('mail.message', 'parent_id', 'Child Messages'),
         'model': fields.char('Related Document Model', size=128, select=1),
         'res_id': fields.integer('Related Document ID', select=1),
         'child_ids': fields.one2many('mail.message', 'parent_id', 'Child Messages'),
         'model': fields.char('Related Document Model', size=128, select=1),
         'res_id': fields.integer('Related Document ID', select=1),
-        'record_name': fields.function(_get_record_name, type='string',
-            string='Message Record Name',
+        'record_name': fields.function(_get_record_name, type='char',
+            store=True, string='Message Record Name',
             help="Name get of the related document."),
         'notification_ids': fields.one2many('mail.notification', 'message_id', 'Notifications'),
         'subject': fields.char('Subject'),
             help="Name get of the related document."),
         'notification_ids': fields.one2many('mail.notification', 'message_id', 'Notifications'),
         'subject': fields.char('Subject'),
index 7d5d472..cc9d7d3 100644 (file)
@@ -676,6 +676,14 @@ class mail_thread(osv.AbstractModel):
         if self._mail_flat_thread and not parent_id and thread_id:
             message_ids = mail_message.search(cr, uid, ['&', ('res_id', '=', thread_id), ('model', '=', model)], context=context, order="id ASC", limit=1)
             parent_id = message_ids and message_ids[0] or False
         if self._mail_flat_thread and not parent_id and thread_id:
             message_ids = mail_message.search(cr, uid, ['&', ('res_id', '=', thread_id), ('model', '=', model)], context=context, order="id ASC", limit=1)
             parent_id = message_ids and message_ids[0] or False
+        # we want to set a parent: force to set the parent_id to the oldest ancestor, to avoid having more than 1 level of thread
+        elif parent_id:
+            message_ids = mail_message.search(cr, SUPERUSER_ID, [('id', '=', parent_id), ('parent_id', '!=', False)], context=context)
+            _counter, _counter_max = 0, 200
+            while (message_ids and (_counter <= _counter_max)):
+                parent_id = message_ids[0]
+                message_ids = mail_message.search(cr, SUPERUSER_ID, [('id', '=', parent_id), ('parent_id', '!=', False)], context=context)
+                _counter += 1
 
         values = kwargs
         values.update({
 
         values = kwargs
         values.update({