From 5c03dba5a671baafd56d6d0d932e62f21e981b3d Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Mon, 15 Apr 2013 15:22:42 +0200 Subject: [PATCH] [FIX] attach mail attachments to matched object in non-OpenERP case --- .../model/fetchmail_server.py | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/fetchmail_attach_from_folder/model/fetchmail_server.py b/fetchmail_attach_from_folder/model/fetchmail_server.py index a18e95319..0d0c60730 100644 --- a/fetchmail_attach_from_folder/model/fetchmail_server.py +++ b/fetchmail_attach_from_folder/model/fetchmail_server.py @@ -20,6 +20,7 @@ # ############################################################################## +import base64 import simplejson from lxml import etree from openerp.osv.orm import Model, except_orm, browse_null @@ -162,6 +163,24 @@ class fetchmail_server(Model): cr, uid, object_id, context ).partner_id.id + attachments=[] + if this.attach and mail_message.get('attachments'): + for attachment in mail_message['attachments']: + fname, fcontent = attachment + if isinstance(fcontent, unicode): + fcontent = fcontent.encode('utf-8') + data_attach = { + 'name': fname, + 'datas': base64.b64encode(str(fcontent)), + 'datas_fname': fname, + 'description': _('Mail attachment'), + 'res_model': folder.model_id.model, + 'res_id': object_id, + } + attachments.append( + self.pool.get('ir.attachment').create( + cr, uid, data_attach, context=context)) + self.pool.get('mail.message').create( cr, uid, { @@ -180,11 +199,10 @@ class fetchmail_server(Model): 'subtype': mail_message.get('subtype'), 'headers': mail_message.get('headers'), 'state': folder.msg_state, + 'attachment_ids': [(6, 0, attachments)], }, context) - if this.attach: - # TODO: create attachments - pass + if folder.delete_matching: connection.store(msgid, '+FLAGS', '\\DELETED')