[MERGE] merge with latest stable
[odoo/odoo.git] / addons / mail_gateway / mail_gateway.py
index c459b25..c4baca5 100644 (file)
@@ -127,12 +127,29 @@ class mailgate_thread(osv.osv):
 
         for case in cases:
             attachments = []
-            if attach:
-                for att in attach:
-                    if isinstance(att,(int,long)):
-                        attachments.append(att)
-                    elif isinstance(att,dict):
-                        attachments.append(att_obj.create(cr, uid, {'res_model':case._name,'res_id':case.id,'name': att[0], 'datas': base64.encodestring(att[1])}))
+            for att in attach:
+                if isinstance(att,(int,long)):
+                    attachments.append(att)
+                elif isinstance(att,dict):
+                    domain = [
+                        ('name', '=', att[0]),
+                        ('res_id', '=', case.id),
+                        ('res_model', '=', case._name)
+                    ]
+                    att_ids = att_obj.search(cr, uid, domain, context=context)
+
+                    if att_ids:
+                        attachments.extend(att_ids)
+                    else:
+                        values = {
+                            'res_model' : case._name,
+                            'res_id' : case.id,
+                            'name' : att[0],
+                            'datas' : base64.encodestring(att[1])
+                        }
+                        attachment_id = att_obj.create(cr, uid, values, context=context)
+                        attachments.append(attachment_id)
+
             partner_id = hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False
             if not partner_id and case._name == 'res.partner':
                 partner_id = case.id
@@ -153,25 +170,18 @@ class mailgate_thread(osv.osv):
                     if isinstance(param, list):
                         param = ", ".join(param)
 
-                data = {
+                data.update({
                     'name': subject or _('History'),
                     'history': True,
-                    'user_id': uid,
-                    'model' : case._name,
-                    'res_id': case.id,
                     'date': email_date or time.strftime('%Y-%m-%d %H:%M:%S'),
-                    'description': details or (hasattr(case, 'description') and case.description or False),
                     'email_to': email,
                     'email_from': email_from or \
                         (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
                          case.user_id.address_id.email),
                     'email_cc': email_cc,
                     'email_bcc': email_bcc,
-                    'partner_id': partner_id,
                     'references': references,
-                    'message_id': message_id,
-                    'attachment_ids': [(6, 0, attachments)]
-                }
+                })
             obj.create(cr, uid, data, context=context)
         return True
 mailgate_thread()
@@ -223,7 +233,7 @@ class mailgate_message(osv.osv):
         action_data = False
         action_pool = self.pool.get('ir.actions.act_window')
         message_pool = self.browse(cr ,uid, ids, context=context)[0]
-        att_ids = [x.id for x in message_pool.attachment_ids]
+        att_ids = [x.id for x in message_pool.attachment_ids] 
         action_ids = action_pool.search(cr, uid, [('res_model', '=', 'ir.attachment')])
         if action_ids:
             action_data = action_pool.read(cr, uid, action_ids[0], context=context)
@@ -439,6 +449,7 @@ class mailgate_tool(osv.osv_memory):
                             'res_id': res_id,
                         }
                         att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
+
             return res_id, att_ids
 
         # Warning: message_from_string doesn't always work correctly on unicode,
@@ -524,7 +535,8 @@ class mailgate_tool(osv.osv_memory):
                         elif part.get_content_subtype() == 'plain':
                             body = content
                             has_plain_text = True
-                elif part.get_content_maintype() in ('application', 'image'):
+                elif part.get_content_maintype() in ('application', 'image') \
+                        or part.get_content_subtype() in ('octet-stream'):
                     if filename :
                         attachments[filename] = part.get_payload(decode=True)
                     else: