}
threads.append(data)
return sorted(threads, key=lambda x: (x['popularity'], x['id']), reverse=True)[:3]
+
+ def message_change_thread(self, cr, uid, id, new_res_id, new_model, context=None):
+ """
+ Transfert the list of the mail thread messages from an model to another
+
+ :param id : the old res_id of the mail.message
+ :param new_res_id : the new res_id of the mail.message
+ :param new_model : the name of the new model of the mail.message
+
+ Example : self.pool.get("crm.lead").message_change_thread(self, cr, uid, 2, 4, "project.issue", context)
+ will transfert thread of the lead (id=2) to the issue (id=4)
+ """
+
+ # get the sbtype id of the comment Message
+ subtype_res_id = self.pool.get('ir.model.data').xmlid_to_res_id(cr, uid, 'mail.mt_comment', raise_if_not_found=True)
+
+ # get the ids of the comment and none-comment of the thread
+ message_obj = self.pool.get('mail.message')
+ msg_ids_comment = message_obj.search(cr, uid, [
+ ('model', '=', self._name),
+ ('res_id', '=', id),
+ ('subtype_id', '=', subtype_res_id)], context=context)
+ msg_ids_not_comment = message_obj.search(cr, uid, [
+ ('model', '=', self._name),
+ ('res_id', '=', id),
+ ('subtype_id', '!=', subtype_res_id)], context=context)
+
+ # update the messages
+ message_obj.write(cr, uid, msg_ids_comment, {"res_id" : new_res_id, "model" : new_model}, context=context)
+ message_obj.write(cr, uid, msg_ids_not_comment, {"res_id" : new_res_id, "model" : new_model, "subtype_id" : None}, context=context)
+
- return True
++ return True