[REF] refactor file email

This commit is contained in:
Sébastien Beau 2013-04-04 17:10:54 +02:00
parent 140fa02533
commit 658e0d3375
5 changed files with 90 additions and 19 deletions

View File

@ -20,5 +20,5 @@
# #
############################################################################### ###############################################################################
import file_buffer import file_document
import fetchmail

View File

@ -27,15 +27,16 @@
'license': 'AGPL-3', 'license': 'AGPL-3',
'description': """Abstract module for importing and processing the 'description': """Abstract module for importing and processing the
attachment of an email. The attachment of the email will be imported 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 An example of processing can be found in account_statement_email
""", """,
'author': 'Akretion', 'author': 'Akretion',
'website': 'http://www.akretion.com/', 'website': 'http://www.akretion.com/',
'depends': ['file_buffer'], 'depends': ['file_document'],
'init_xml': [], 'init_xml': [],
'update_xml': [ 'update_xml': [
], "fetchmail_view.xml",
],
'demo_xml': [], 'demo_xml': [],
'installable': True, 'installable': True,
'active': False, 'active': False,

53
fetchmail.py Normal file
View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
###############################################################################
#
# file_email for OpenERP
# Copyright (C) 2013 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <sebastien.beau@akretion.com>
#
# 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 <http://www.gnu.org/licenses/>.
#
###############################################################################
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

19
fetchmail_view.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<openerp>
<data>
<record model="ir.ui.view" id="view_email_server_form">
<field name="name">fetchmail.server.form</field>
<field name="model">fetchmail.server</field>
<field name="type">form</field>
<field name="inherit_id" ref="fetchmail.view_email_server_form"/>
<field name="arch" type="xml">
<field name="object_id" position="after">
<field name="file_type"/> <!-- TODO attrs="{'invisible': [('object_id','!=', 'file.document')]}"/>-->
</field>
</field>
</record>
</data>
</openerp>

View File

@ -23,29 +23,27 @@
from openerp.osv import fields, orm from openerp.osv import fields, orm
class file_buffer(orm.Model): class file_document(orm.Model):
_inherit = "file.buffer" _inherit = "file.document"
def _prepare_data_for_file_buffer(self, cr, uid, msg, context=None): def _prepare_data_for_file_document(self, cr, uid, msg, context=None):
"""Method to prepare the data for creating a file buffer. """Method to prepare the data for creating a file document.
:param msg: a dictionnary with the email data :param msg: a dictionnary with the email data
:type: dict :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 :rtype: list
""" """
return [] return []
def message_new(self, cr, uid, msg, custom_values, context=None): def message_new(self, cr, uid, msg, custom_values, context=None):
create_ids = [] created_ids = []
res = self._get_vals_for_file_buffer(cr, uid, msg, context=context) res = self._prepare_data_for_file_document(cr, uid, msg, context=context)
if res: if res:
for vals in res: for vals in res:
file_id = self.create(cr, uid, vals, context=context) if context.get('default_file_document_vals'):
self.create_file_buffer_attachment(cr, uid, file_id, vals.update(context['default_file_document_vals'])
datas, file_name, created_ids.append(self.create(cr, uid, vals, context=context))
context=context, print "create message", vals['date']
extension=vals['extension']) return created_ids
create_ids = file_id
return create_ids
return None return None