From 8de978bb714bcaf14d8a1b2c7f9039057dd2e26a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Beau?= Date: Wed, 3 Apr 2013 17:14:57 +0200 Subject: [PATCH 01/18] [ADD] add file-email --- __init__.py | 24 +++++++++++++++++++++ __openerp__.py | 42 +++++++++++++++++++++++++++++++++++++ file_buffer.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 __init__.py create mode 100644 __openerp__.py create mode 100644 file_buffer.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 00000000..91628407 --- /dev/null +++ b/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# file_email for OpenERP +# Copyright (C) 2012-TODAY Akretion . +# @author Sébastien BEAU +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################### + +import file_buffer + diff --git a/__openerp__.py b/__openerp__.py new file mode 100644 index 00000000..592affad --- /dev/null +++ b/__openerp__.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# file_buffer_from_mail for OpenERP +# Copyright (C) 2012-TODAY Akretion . +# @author Sébastien BEAU +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################### + +{ + 'name': 'file_buffer_from_mail', + 'version': '0.1', + 'category': 'Generic Modules/Others', + 'license': 'AGPL-3', + 'description': """Abstract module for importing and processing the + attachment of an email. The attachment of the email will be imported + as a file_buffer and then in your custom module you can process it. + An example of processing can be found in account_statement_email + """, + 'author': 'Akretion', + 'website': 'http://www.akretion.com/', + 'depends': ['file_buffer'], + 'init_xml': [], + 'update_xml': [ + ], + 'demo_xml': [], + 'installable': True, + 'active': False, +} diff --git a/file_buffer.py b/file_buffer.py new file mode 100644 index 00000000..8e5fe50e --- /dev/null +++ b/file_buffer.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# file_email for OpenERP +# Copyright (C) 2012-TODAY Akretion . +# @author Sébastien BEAU +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################### + +from openerp.osv import fields +from openerp.osv.orm import Model + +class file_buffer(Model): + _inherit = "file.buffer" + + def _prepare_data_for_file_buffer(self, cr, uid, msg, context=None): + """Method to prepare the data for creating a file buffer. + :param msg: a dictionnary with the email data + :type: dict + + :return: a list of dictionnary that containt the file buffer data + :rtype: list + """ + return [] + + def message_new(self, cr, uid, msg, custom_values, context=None): + create_ids = [] + res = self._get_vals_for_file_buffer(cr, uid, msg, context=context) + if res: + for vals in res: + file_id = self.create(cr, uid, vals, context=context) + self.create_file_buffer_attachment(cr, uid, file_id, + datas, file_name, + context=context, + extension=vals['extension']) + create_ids = file_id + return create_ids + return None + + + + + + From 140fa02533d13cf2abd7eb50f082a97d7d518946 Mon Sep 17 00:00:00 2001 From: Benoit Guillot Date: Wed, 3 Apr 2013 17:47:11 +0200 Subject: [PATCH 02/18] [FIX] fix module name --- __openerp__.py | 8 ++++---- file_buffer.py | 14 ++++---------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/__openerp__.py b/__openerp__.py index 592affad..439d74c6 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- ############################################################################### # -# file_buffer_from_mail for OpenERP +# file_email for OpenERP # Copyright (C) 2012-TODAY Akretion . # @author Sébastien BEAU # @@ -21,7 +21,7 @@ ############################################################################### { - 'name': 'file_buffer_from_mail', + 'name': 'file_email', 'version': '0.1', 'category': 'Generic Modules/Others', 'license': 'AGPL-3', @@ -32,9 +32,9 @@ """, 'author': 'Akretion', 'website': 'http://www.akretion.com/', - 'depends': ['file_buffer'], + 'depends': ['file_buffer'], 'init_xml': [], - 'update_xml': [ + 'update_xml': [ ], 'demo_xml': [], 'installable': True, diff --git a/file_buffer.py b/file_buffer.py index 8e5fe50e..ee712b1f 100644 --- a/file_buffer.py +++ b/file_buffer.py @@ -20,10 +20,10 @@ # ############################################################################### -from openerp.osv import fields -from openerp.osv.orm import Model +from openerp.osv import fields, orm -class file_buffer(Model): + +class file_buffer(orm.Model): _inherit = "file.buffer" def _prepare_data_for_file_buffer(self, cr, uid, msg, context=None): @@ -44,14 +44,8 @@ class file_buffer(Model): file_id = self.create(cr, uid, vals, context=context) self.create_file_buffer_attachment(cr, uid, file_id, datas, file_name, - context=context, + context=context, extension=vals['extension']) create_ids = file_id return create_ids return None - - - - - - From 658e0d33754a7d537b27bfc58494acf435fab0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Beau?= Date: Thu, 4 Apr 2013 17:10:54 +0200 Subject: [PATCH 03/18] [REF] refactor file email --- __init__.py | 4 +-- __openerp__.py | 7 ++-- fetchmail.py | 53 ++++++++++++++++++++++++++++++ fetchmail_view.xml | 19 +++++++++++ file_buffer.py => file_document.py | 26 +++++++-------- 5 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 fetchmail.py create mode 100644 fetchmail_view.xml rename file_buffer.py => file_document.py (62%) diff --git a/__init__.py b/__init__.py index 91628407..c2827724 100644 --- a/__init__.py +++ b/__init__.py @@ -20,5 +20,5 @@ # ############################################################################### -import file_buffer - +import file_document +import fetchmail diff --git a/__openerp__.py b/__openerp__.py index 439d74c6..8dcc25e3 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -27,15 +27,16 @@ 'license': 'AGPL-3', 'description': """Abstract module for importing and processing the attachment of an email. The attachment of the email will be imported - as a file_buffer and then in your custom module you can process it. + as a file_document and then in your custom module you can process it. An example of processing can be found in account_statement_email """, 'author': 'Akretion', 'website': 'http://www.akretion.com/', - 'depends': ['file_buffer'], + 'depends': ['file_document'], 'init_xml': [], 'update_xml': [ - ], + "fetchmail_view.xml", + ], 'demo_xml': [], 'installable': True, 'active': False, diff --git a/fetchmail.py b/fetchmail.py new file mode 100644 index 00000000..a140017e --- /dev/null +++ b/fetchmail.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# file_email for OpenERP +# Copyright (C) 2013 Akretion (http://www.akretion.com). +# @author Sébastien BEAU +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################### + +from openerp.osv.orm import Model +from openerp.osv import fields + + +class fetchmail_server(Model): + _inherit = 'fetchmail.server' + + def get_file_type(self, cr, uid, context=None): + return [] + + def _get_file_type(self, cr, uid, context=None): + return self.get_file_type(cr, uid, context=context) + + _columns = { + 'file_type': fields.selection(_get_file_type, 'File Type', + help='The file type will show some special option'), + } + + def get_context_for_server(self, cr, uid, server_id, context=None): + if context is None: + ctx = {} + else: + ctx = context.copy() + ctx['default_file_document_vals'] = {} + return ctx + + def fetch_mail(self, cr, uid, ids, context=None): + for id in ids: + ctx = self.get_context_for_server(cr, uid, id, context=context) + super(fetchmail_server, self).fetch_mail(cr, uid, ids, context=ctx) + return True diff --git a/fetchmail_view.xml b/fetchmail_view.xml new file mode 100644 index 00000000..e9eaf732 --- /dev/null +++ b/fetchmail_view.xml @@ -0,0 +1,19 @@ + + + + + + fetchmail.server.form + fetchmail.server + form + + + + + + + + + + + diff --git a/file_buffer.py b/file_document.py similarity index 62% rename from file_buffer.py rename to file_document.py index ee712b1f..ab59be0d 100644 --- a/file_buffer.py +++ b/file_document.py @@ -23,29 +23,27 @@ from openerp.osv import fields, orm -class file_buffer(orm.Model): - _inherit = "file.buffer" +class file_document(orm.Model): + _inherit = "file.document" - def _prepare_data_for_file_buffer(self, cr, uid, msg, context=None): - """Method to prepare the data for creating a file buffer. + def _prepare_data_for_file_document(self, cr, uid, msg, context=None): + """Method to prepare the data for creating a file document. :param msg: a dictionnary with the email data :type: dict - :return: a list of dictionnary that containt the file buffer data + :return: a list of dictionnary that containt the file document data :rtype: list """ return [] def message_new(self, cr, uid, msg, custom_values, context=None): - create_ids = [] - res = self._get_vals_for_file_buffer(cr, uid, msg, context=context) + created_ids = [] + res = self._prepare_data_for_file_document(cr, uid, msg, context=context) if res: for vals in res: - file_id = self.create(cr, uid, vals, context=context) - self.create_file_buffer_attachment(cr, uid, file_id, - datas, file_name, - context=context, - extension=vals['extension']) - create_ids = file_id - return create_ids + if context.get('default_file_document_vals'): + vals.update(context['default_file_document_vals']) + created_ids.append(self.create(cr, uid, vals, context=context)) + print "create message", vals['date'] + return created_ids return None From 82f4c94193c3c70b4c219d777b203066645d596f Mon Sep 17 00:00:00 2001 From: Benoit Guillot Date: Fri, 5 Apr 2013 12:20:41 +0200 Subject: [PATCH 04/18] [FIX] fix Model -> orm.Model --- fetchmail.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/fetchmail.py b/fetchmail.py index a140017e..d8fcdfe3 100644 --- a/fetchmail.py +++ b/fetchmail.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- ############################################################################### # -# file_email for OpenERP +# file_email for OpenERP # Copyright (C) 2013 Akretion (http://www.akretion.com). # @author Sébastien BEAU # @@ -20,11 +20,10 @@ # ############################################################################### -from openerp.osv.orm import Model -from openerp.osv import fields +from openerp.osv import fields, orm -class fetchmail_server(Model): +class fetchmail_server(orm.Model): _inherit = 'fetchmail.server' def get_file_type(self, cr, uid, context=None): From ee1ec85726927235af1f892ee7b2dfecd42021a0 Mon Sep 17 00:00:00 2001 From: Benoit Guillot Date: Sun, 7 Apr 2013 16:53:47 +0200 Subject: [PATCH 05/18] [PORT] port view to v7 --- fetchmail_view.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/fetchmail_view.xml b/fetchmail_view.xml index e9eaf732..2c30a5ca 100644 --- a/fetchmail_view.xml +++ b/fetchmail_view.xml @@ -5,7 +5,6 @@ fetchmail.server.form fetchmail.server - form @@ -16,4 +15,3 @@ - From 548e308a7ac6f1f5ff43b5014c89436e743612a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Beau?= Date: Thu, 11 Jul 2013 01:09:07 +0200 Subject: [PATCH 06/18] [IMP] file_email: fix default value, add company on email server, add sql constraint for uniqueness --- __openerp__.py | 1 + fetchmail.py | 9 +++++++++ fetchmail_view.xml | 4 ++++ file_document.py | 17 ++++++++++++++--- file_document_view.xml | 21 +++++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 file_document_view.xml diff --git a/__openerp__.py b/__openerp__.py index 8dcc25e3..125a27aa 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -36,6 +36,7 @@ 'init_xml': [], 'update_xml': [ "fetchmail_view.xml", + "file_document_view.xml", ], 'demo_xml': [], 'installable': True, diff --git a/fetchmail.py b/fetchmail.py index d8fcdfe3..9710338b 100644 --- a/fetchmail.py +++ b/fetchmail.py @@ -35,14 +35,23 @@ class fetchmail_server(orm.Model): _columns = { 'file_type': fields.selection(_get_file_type, 'File Type', help='The file type will show some special option'), + 'company_id': fields.many2one('res.company', 'Company', required=True),#Why this field do not exist by default? } + _defaults = { + 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'fetchmail.server', context=c), + } + + def get_context_for_server(self, cr, uid, server_id, context=None): if context is None: ctx = {} else: ctx = context.copy() ctx['default_file_document_vals'] = {} + server = self.browse(cr, uid, server_id, context=context) + ctx['default_company_id'] = server.company_id.id + ctx['default_fetchmail_server_id'] = server_id return ctx def fetch_mail(self, cr, uid, ids, context=None): diff --git a/fetchmail_view.xml b/fetchmail_view.xml index e9eaf732..e83d2d74 100644 --- a/fetchmail_view.xml +++ b/fetchmail_view.xml @@ -8,6 +8,10 @@ form + + + + diff --git a/file_document.py b/file_document.py index ab59be0d..af5277c8 100644 --- a/file_document.py +++ b/file_document.py @@ -26,6 +26,15 @@ from openerp.osv import fields, orm class file_document(orm.Model): _inherit = "file.document" + _columns = { + 'fetchmail_server_id': fields.many2one('fetchmail.server', 'Email Server'), + } + + _sql_constraints = [ + ('fecthmail_server_ext_id_uniq', 'unique(fetchmail_server_id, ext_id)', + 'The combination of Email Server and External id must be unique !'), + ] + def _prepare_data_for_file_document(self, cr, uid, msg, context=None): """Method to prepare the data for creating a file document. :param msg: a dictionnary with the email data @@ -41,9 +50,11 @@ class file_document(orm.Model): res = self._prepare_data_for_file_document(cr, uid, msg, context=context) if res: for vals in res: - if context.get('default_file_document_vals'): - vals.update(context['default_file_document_vals']) + default = context.get('default_file_document_vals') + if default: + for key in default: + if not key in vals: + vals[key] = default[key] created_ids.append(self.create(cr, uid, vals, context=context)) - print "create message", vals['date'] return created_ids return None diff --git a/file_document_view.xml b/file_document_view.xml new file mode 100644 index 00000000..1529e47d --- /dev/null +++ b/file_document_view.xml @@ -0,0 +1,21 @@ + + + + + + + file.document.search + file.document + search + + + + + + + + + + + + From 4af45fce4ecbaa9e15739a97ada5f366b21e0083 Mon Sep 17 00:00:00 2001 From: Arthur Vuillard Date: Thu, 8 Aug 2013 16:47:19 +0200 Subject: [PATCH 07/18] Add a missing dependency to fetchmail --- __openerp__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__openerp__.py b/__openerp__.py index 125a27aa..0d88bca3 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -32,7 +32,7 @@ """, 'author': 'Akretion', 'website': 'http://www.akretion.com/', - 'depends': ['file_document'], + 'depends': ['file_document', 'fetchmail'], 'init_xml': [], 'update_xml': [ "fetchmail_view.xml", From 97b8e2089f87b1df90b6ec7ed26b83871f797246 Mon Sep 17 00:00:00 2001 From: Sebastien Beau Date: Wed, 4 Sep 2013 20:49:52 +0200 Subject: [PATCH 08/18] [FIX] fix missing dependency --- __openerp__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__openerp__.py b/__openerp__.py index 8dcc25e3..018a4d4a 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -32,7 +32,7 @@ """, 'author': 'Akretion', 'website': 'http://www.akretion.com/', - 'depends': ['file_document'], + 'depends': ['file_document','fetchmail_server'], 'init_xml': [], 'update_xml': [ "fetchmail_view.xml", From 4e67654e3bb8d9fb800a163972b9effd5b2a533a Mon Sep 17 00:00:00 2001 From: Sebastien Beau Date: Tue, 26 Nov 2013 19:21:08 +0100 Subject: [PATCH 09/18] [FIX] fix email import for V7 --- fetchmail_view.xml | 5 +---- file_document.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/fetchmail_view.xml b/fetchmail_view.xml index e83d2d74..7a63ad83 100644 --- a/fetchmail_view.xml +++ b/fetchmail_view.xml @@ -3,17 +3,14 @@ - fetchmail.server.form fetchmail.server - form - - + diff --git a/file_document.py b/file_document.py index af5277c8..7e26412c 100644 --- a/file_document.py +++ b/file_document.py @@ -35,6 +35,39 @@ class file_document(orm.Model): 'The combination of Email Server and External id must be unique !'), ] + + def message_process(self, cr, uid, model, message, custom_values=None, + save_original=False, strip_attachments=False, + thread_id=None, context=None): + if context is None: + context = {} + context['no_post'] = True + return super(file_document, self).message_process(self, cr, uid, model, + message, + custom_values=custom_values, + save_original=save_original, + strip_attachments=strip_attachments, + thread_id=thread_id, + context=context) + + def message_post(self, cr, uid, thread_id, body='', subject=None, type='notification', + subtype=None, parent_id=False, attachments=None, context=None, + content_subtype='html', **kwargs): + if context.get('no_post'): + return None + return super(file_document, self).message_post(cr, uid, thread_id, + body=body, + subject=subject, + type='notification', + subtype=subtype, + parent_id=parent_id, + attachments=attachments, + context=context, + content_subtype=content_subtype, + **kwargs) + + + def _prepare_data_for_file_document(self, cr, uid, msg, context=None): """Method to prepare the data for creating a file document. :param msg: a dictionnary with the email data @@ -56,5 +89,8 @@ class file_document(orm.Model): if not key in vals: vals[key] = default[key] created_ids.append(self.create(cr, uid, vals, context=context)) - return created_ids + cr.commit() + context['created_ids'] = created_ids + return created_ids[0] return None + From 8e691f8a881cdc12d4321c7a264a4e9e4b781120 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Fri, 13 Dec 2013 23:12:10 +0100 Subject: [PATCH 10/18] [IMP] Add possibility to create condition in the fetchmail server which will allow to create a file document depending on the file name, the email and the subject --- fetchmail.py | 3 ++ fetchmail_view.xml | 3 ++ file_document.py | 62 ++++++++++++++++++++++++++++++++++++++---- file_document_view.xml | 26 ++++++++++++++++++ 4 files changed, 89 insertions(+), 5 deletions(-) diff --git a/fetchmail.py b/fetchmail.py index 9710338b..be4082bb 100644 --- a/fetchmail.py +++ b/fetchmail.py @@ -36,6 +36,7 @@ class fetchmail_server(orm.Model): 'file_type': fields.selection(_get_file_type, 'File Type', help='The file type will show some special option'), 'company_id': fields.many2one('res.company', 'Company', required=True),#Why this field do not exist by default? + 'file_document_condition_ids': fields.one2many('prepare.file.document', 'server_id', 'File Document ') } _defaults = { @@ -49,9 +50,11 @@ class fetchmail_server(orm.Model): else: ctx = context.copy() ctx['default_file_document_vals'] = {} + ctx['default_file_document_condition_ids'] = {} server = self.browse(cr, uid, server_id, context=context) ctx['default_company_id'] = server.company_id.id ctx['default_fetchmail_server_id'] = server_id + ctx['default_file_document_condition_ids'] = [cond.id for cond in server.file_document_condition_ids] return ctx def fetch_mail(self, cr, uid, ids, context=None): diff --git a/fetchmail_view.xml b/fetchmail_view.xml index f8f91110..9c7b5fdb 100644 --- a/fetchmail_view.xml +++ b/fetchmail_view.xml @@ -12,6 +12,9 @@ + + + diff --git a/file_document.py b/file_document.py index 7e26412c..86024277 100644 --- a/file_document.py +++ b/file_document.py @@ -21,6 +21,7 @@ ############################################################################### from openerp.osv import fields, orm +import base64 class file_document(orm.Model): @@ -30,10 +31,6 @@ class file_document(orm.Model): 'fetchmail_server_id': fields.many2one('fetchmail.server', 'Email Server'), } - _sql_constraints = [ - ('fecthmail_server_ext_id_uniq', 'unique(fetchmail_server_id, ext_id)', - 'The combination of Email Server and External id must be unique !'), - ] def message_process(self, cr, uid, model, message, custom_values=None, @@ -66,6 +63,12 @@ class file_document(orm.Model): content_subtype=content_subtype, **kwargs) + def custom_data_for_file_document(self, cr, uid, msg, context=None): + return {} + + + def add_more_fields(self, cr, uid, vals, msg, context=None): + return {} def _prepare_data_for_file_document(self, cr, uid, msg, context=None): @@ -76,7 +79,31 @@ class file_document(orm.Model): :return: a list of dictionnary that containt the file document data :rtype: list """ - return [] + res = [] + doc_file_condition_obj = self.pool.get('prepare.file.document') + cond_ids = context.get('default_file_document_condition_ids', False) + for cond in doc_file_condition_obj.browse(cr, uid, cond_ids): + vals = {} + if cond.type == 'normal': + if cond.from_email in msg['from'] and cond.mail_subject == msg['subject']: + vals = { + 'name': msg['subject'], + 'direction': 'input', + 'date': msg['date'], + 'ext_id': msg['message_id'], + } + #attachment_names = [att[0] for att in msg['attachments']] + for att in msg['attachments']: + if cond.file_extension in att[0]: + vals['datas_fname'] = att[0] + vals['datas'] = base64.b64encode(att[0][1]) + pass + vals.update(self.add_more_fields(cr, uid, vals, msg, context=context)) + else: + vals = eval('self.'+cond.type)(cr, uid, msg, context=context) + if 'datas_fname' in vals: + res.append(vals) + return res def message_new(self, cr, uid, msg, custom_values, context=None): created_ids = [] @@ -94,3 +121,28 @@ class file_document(orm.Model): return created_ids[0] return None + +class prepare_file_document(orm.Model): + _name = "prepare.file.document" + _description = "Prepare File Document" + + def _get_prepare_file_document_type(self, cr, uid, context=None): + return self.get_prepare_file_document_type(cr, uid, context=context) + + def get_prepare_file_document_type(self, cr, uid, context=None): + return [('normal', 'Normal')] + + _columns = { + 'from_email': fields.char('Email', size=64), + 'mail_subject': fields.char('Mail Subject', size=64), + 'type': fields.selection(_get_prepare_file_document_type, + 'Type', help="Create your own type if the normal type do not correspond to your need", required=True), + 'file_extension' : fields.char('File Extension', size=64, help="File extension or file name", required=True), + 'server_id': fields.many2one('fetchmail.server', 'Server Mail'), + } + + + _defaults = { + 'type': 'normal' + } + diff --git a/file_document_view.xml b/file_document_view.xml index 1529e47d..adce9945 100644 --- a/file_document_view.xml +++ b/file_document_view.xml @@ -15,6 +15,32 @@ + + + view_prepare_file_document_tree + prepare.file.document + + + + + + + + + + + + + Configuration + prepare.file.document + form + tree,form + + + From 220881df66c2c9ca4ff1c9ef9f55a0935ae1e450 Mon Sep 17 00:00:00 2001 From: Sebastien Beau Date: Mon, 16 Dec 2013 17:16:40 +0100 Subject: [PATCH 11/18] [FIX] fix import of email. We have to call super with the server_id and not the ids --- fetchmail.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fetchmail.py b/fetchmail.py index 9710338b..7272ceaf 100644 --- a/fetchmail.py +++ b/fetchmail.py @@ -55,7 +55,7 @@ class fetchmail_server(orm.Model): return ctx def fetch_mail(self, cr, uid, ids, context=None): - for id in ids: - ctx = self.get_context_for_server(cr, uid, id, context=context) - super(fetchmail_server, self).fetch_mail(cr, uid, ids, context=ctx) + for server_id in ids: + ctx = self.get_context_for_server(cr, uid, server_id, context=context) + super(fetchmail_server, self).fetch_mail(cr, uid, [server_id], context=ctx) return True From 1396b72c965caac56daf711a1eec9c94cffadc11 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Tue, 17 Dec 2013 12:00:25 +0100 Subject: [PATCH 12/18] [REF] Refactore prepare data for file document function + clear some code --- fetchmail.py | 4 +-- file_document.py | 71 ++++++++++++++++++++---------------------- file_document_view.xml | 8 ++--- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/fetchmail.py b/fetchmail.py index be4082bb..989e2831 100644 --- a/fetchmail.py +++ b/fetchmail.py @@ -36,7 +36,7 @@ class fetchmail_server(orm.Model): 'file_type': fields.selection(_get_file_type, 'File Type', help='The file type will show some special option'), 'company_id': fields.many2one('res.company', 'Company', required=True),#Why this field do not exist by default? - 'file_document_condition_ids': fields.one2many('prepare.file.document', 'server_id', 'File Document ') + 'file_document_condition_ids': fields.one2many('file.document.condition', 'server_id', 'File Document ') } _defaults = { @@ -50,11 +50,9 @@ class fetchmail_server(orm.Model): else: ctx = context.copy() ctx['default_file_document_vals'] = {} - ctx['default_file_document_condition_ids'] = {} server = self.browse(cr, uid, server_id, context=context) ctx['default_company_id'] = server.company_id.id ctx['default_fetchmail_server_id'] = server_id - ctx['default_file_document_condition_ids'] = [cond.id for cond in server.file_document_condition_ids] return ctx def fetch_mail(self, cr, uid, ids, context=None): diff --git a/file_document.py b/file_document.py index 86024277..58892c99 100644 --- a/file_document.py +++ b/file_document.py @@ -62,13 +62,23 @@ class file_document(orm.Model): context=context, content_subtype=content_subtype, **kwargs) - - def custom_data_for_file_document(self, cr, uid, msg, context=None): - return {} - def add_more_fields(self, cr, uid, vals, msg, context=None): - return {} + def prepare_data_from_basic_condition(self, cr, uid, condition, msg, context=None): + vals = {} + if condition.from_email in msg['from'] and condition.mail_subject == msg['subject']: + for att in msg['attachments']: + if condition.file_extension in att[0]: + vals = { + 'name': msg['subject'], + 'direction': 'input', + 'date': msg['date'], + 'ext_id': msg['message_id'], + 'datas_fname': att[0], + 'datas': base64.b64encode(att[0][1]) + } + break + return vals def _prepare_data_for_file_document(self, cr, uid, msg, context=None): @@ -80,29 +90,17 @@ class file_document(orm.Model): :rtype: list """ res = [] - doc_file_condition_obj = self.pool.get('prepare.file.document') - cond_ids = context.get('default_file_document_condition_ids', False) - for cond in doc_file_condition_obj.browse(cr, uid, cond_ids): - vals = {} - if cond.type == 'normal': - if cond.from_email in msg['from'] and cond.mail_subject == msg['subject']: - vals = { - 'name': msg['subject'], - 'direction': 'input', - 'date': msg['date'], - 'ext_id': msg['message_id'], - } - #attachment_names = [att[0] for att in msg['attachments']] - for att in msg['attachments']: - if cond.file_extension in att[0]: - vals['datas_fname'] = att[0] - vals['datas'] = base64.b64encode(att[0][1]) - pass - vals.update(self.add_more_fields(cr, uid, vals, msg, context=context)) - else: - vals = eval('self.'+cond.type)(cr, uid, msg, context=context) - if 'datas_fname' in vals: - res.append(vals) + server_id = context.get('default_fetchmail_server_id', False) + doc_file_condition_obj = self.pool.get('file.document.condition') + cond_ids = doc_file_condition_obj.search(cr, uid, [('server_id', '=', server_id)]) + if cond_ids: + for cond in doc_file_condition_obj.browse(cr, uid, cond_ids): + if cond.type == 'normal': + vals = self.prepare_data_from_basic_condition(cr, uid, cond, msg, context=context) + else: + vals = getattr(self, cond.type)(cr, uid, cond, msg, context=context) + if vals: + res.append(vals) return res def message_new(self, cr, uid, msg, custom_values, context=None): @@ -122,20 +120,19 @@ class file_document(orm.Model): return None -class prepare_file_document(orm.Model): - _name = "prepare.file.document" - _description = "Prepare File Document" +class file_document_condition(orm.Model): + _name = "file.document.condition" + _description = "File Document Conditions" - def _get_prepare_file_document_type(self, cr, uid, context=None): - return self.get_prepare_file_document_type(cr, uid, context=context) - - def get_prepare_file_document_type(self, cr, uid, context=None): - return [('normal', 'Normal')] + def _get_file_document_condition_type(self, cr, uid, context=None): + return self.get_file_document_condition_type(cr, uid, context=context) + def get_file_document_condition_type(self, cr, uid, context=None): + return [('normal', 'Normal'), ('test', 'TEST')] _columns = { 'from_email': fields.char('Email', size=64), 'mail_subject': fields.char('Mail Subject', size=64), - 'type': fields.selection(_get_prepare_file_document_type, + 'type': fields.selection(_get_file_document_condition_type, 'Type', help="Create your own type if the normal type do not correspond to your need", required=True), 'file_extension' : fields.char('File Extension', size=64, help="File extension or file name", required=True), 'server_id': fields.many2one('fetchmail.server', 'Server Mail'), diff --git a/file_document_view.xml b/file_document_view.xml index adce9945..4690837c 100644 --- a/file_document_view.xml +++ b/file_document_view.xml @@ -16,9 +16,9 @@ - - view_prepare_file_document_tree - prepare.file.document + + view_file_document_condition_tree + file.document.condition @@ -32,7 +32,7 @@ Configuration - prepare.file.document + file.document.condition form tree,form From 7a03c0c10d85cf761d425cd8a2c7b7affc05f1fb Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Tue, 17 Dec 2013 12:13:40 +0100 Subject: [PATCH 13/18] [IMP] Add required to mail and subject fields in file.document.condition when type is normal --- file_document.py | 9 ++++++--- file_document_view.xml | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/file_document.py b/file_document.py index 58892c99..9653a0cb 100644 --- a/file_document.py +++ b/file_document.py @@ -128,13 +128,16 @@ class file_document_condition(orm.Model): return self.get_file_document_condition_type(cr, uid, context=context) def get_file_document_condition_type(self, cr, uid, context=None): - return [('normal', 'Normal'), ('test', 'TEST')] + return [('normal', 'Normal')] _columns = { 'from_email': fields.char('Email', size=64), 'mail_subject': fields.char('Mail Subject', size=64), 'type': fields.selection(_get_file_document_condition_type, - 'Type', help="Create your own type if the normal type do not correspond to your need", required=True), - 'file_extension' : fields.char('File Extension', size=64, help="File extension or file name", required=True), + 'Type', help="Create your own type if the normal type \ + do not correspond to your need", required=True), + 'file_extension' : fields.char('File Extension', size=64, + help="File extension or file name", + required=True), 'server_id': fields.many2one('fetchmail.server', 'Server Mail'), } diff --git a/file_document_view.xml b/file_document_view.xml index 4690837c..7addf407 100644 --- a/file_document_view.xml +++ b/file_document_view.xml @@ -30,6 +30,20 @@ + + view_file_document_condition_form + file.document.condition + +
+ + + + + + + +
+ Configuration file.document.condition From f935defd4945138376db8b2c28b984745e4d3548 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Thu, 30 Jan 2014 16:56:22 +0100 Subject: [PATCH 14/18] [FIX] Change condition on the mail object for more flexibility --- file_document.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/file_document.py b/file_document.py index 9653a0cb..c73528d5 100644 --- a/file_document.py +++ b/file_document.py @@ -66,7 +66,7 @@ class file_document(orm.Model): def prepare_data_from_basic_condition(self, cr, uid, condition, msg, context=None): vals = {} - if condition.from_email in msg['from'] and condition.mail_subject == msg['subject']: + if condition.from_email in msg['from'] and condition.mail_subject in msg['subject']: for att in msg['attachments']: if condition.file_extension in att[0]: vals = { @@ -129,6 +129,7 @@ class file_document_condition(orm.Model): def get_file_document_condition_type(self, cr, uid, context=None): return [('normal', 'Normal')] + _columns = { 'from_email': fields.char('Email', size=64), 'mail_subject': fields.char('Mail Subject', size=64), From e7139d3a5ddf8d4da4ba284ce6aff9e1f258551c Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Thu, 30 Jan 2014 18:20:53 +0100 Subject: [PATCH 15/18] [FIX] fix empty data when saving an attachment --- file_document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file_document.py b/file_document.py index c73528d5..26e4d17b 100644 --- a/file_document.py +++ b/file_document.py @@ -75,7 +75,7 @@ class file_document(orm.Model): 'date': msg['date'], 'ext_id': msg['message_id'], 'datas_fname': att[0], - 'datas': base64.b64encode(att[0][1]) + 'datas': base64.b64encode(att[1]) } break return vals From 95274049ce44b8b53b7c89c30b706fa3f188516c Mon Sep 17 00:00:00 2001 From: Sebastien Beau Date: Tue, 25 Mar 2014 15:16:17 +0100 Subject: [PATCH 16/18] [FIX] passing the file_type was missing --- file_document.py | 1 + 1 file changed, 1 insertion(+) diff --git a/file_document.py b/file_document.py index 26e4d17b..4b159701 100644 --- a/file_document.py +++ b/file_document.py @@ -70,6 +70,7 @@ class file_document(orm.Model): for att in msg['attachments']: if condition.file_extension in att[0]: vals = { + 'file_type': condition.server_id.file_type, 'name': msg['subject'], 'direction': 'input', 'date': msg['date'], From 01a9e55a3b9c2627467ef2e3e2f6205bc6b34bc1 Mon Sep 17 00:00:00 2001 From: florian-dacosta Date: Mon, 15 Dec 2014 10:33:58 +0100 Subject: [PATCH 17/18] puts profile on email condition instead of fetchmail server --- file_document.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/file_document.py b/file_document.py index 4b159701..655c4bcb 100644 --- a/file_document.py +++ b/file_document.py @@ -63,21 +63,24 @@ class file_document(orm.Model): content_subtype=content_subtype, **kwargs) + def _get_file_document_data(self, cr, uid, condition, msg, att, context=None): + values = { + 'file_type': condition.server_id.file_type, + 'name': msg['subject'], + 'direction': 'input', + 'date': msg['date'], + 'ext_id': msg['message_id'], + 'datas_fname': att[0], + 'datas': base64.b64encode(att[1]) + } + return values def prepare_data_from_basic_condition(self, cr, uid, condition, msg, context=None): vals = {} if condition.from_email in msg['from'] and condition.mail_subject in msg['subject']: for att in msg['attachments']: if condition.file_extension in att[0]: - vals = { - 'file_type': condition.server_id.file_type, - 'name': msg['subject'], - 'direction': 'input', - 'date': msg['date'], - 'ext_id': msg['message_id'], - 'datas_fname': att[0], - 'datas': base64.b64encode(att[1]) - } + vals = self._get_file_document_data(cr, uid, condition, msg, att, context=context) break return vals From 20204c457830718d6eace8ca82af2e1e23528683 Mon Sep 17 00:00:00 2001 From: Valentin Chemiere Date: Thu, 19 Mar 2015 15:26:35 +0100 Subject: [PATCH 18/18] Move into file_email directory --- __init__.py => file_email/__init__.py | 0 __openerp__.py => file_email/__openerp__.py | 0 fetchmail.py => file_email/fetchmail.py | 0 fetchmail_view.xml => file_email/fetchmail_view.xml | 0 file_document.py => file_email/file_document.py | 0 file_document_view.xml => file_email/file_document_view.xml | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename __init__.py => file_email/__init__.py (100%) rename __openerp__.py => file_email/__openerp__.py (100%) rename fetchmail.py => file_email/fetchmail.py (100%) rename fetchmail_view.xml => file_email/fetchmail_view.xml (100%) rename file_document.py => file_email/file_document.py (100%) rename file_document_view.xml => file_email/file_document_view.xml (100%) diff --git a/__init__.py b/file_email/__init__.py similarity index 100% rename from __init__.py rename to file_email/__init__.py diff --git a/__openerp__.py b/file_email/__openerp__.py similarity index 100% rename from __openerp__.py rename to file_email/__openerp__.py diff --git a/fetchmail.py b/file_email/fetchmail.py similarity index 100% rename from fetchmail.py rename to file_email/fetchmail.py diff --git a/fetchmail_view.xml b/file_email/fetchmail_view.xml similarity index 100% rename from fetchmail_view.xml rename to file_email/fetchmail_view.xml diff --git a/file_document.py b/file_email/file_document.py similarity index 100% rename from file_document.py rename to file_email/file_document.py diff --git a/file_document_view.xml b/file_email/file_document_view.xml similarity index 100% rename from file_document_view.xml rename to file_email/file_document_view.xml