Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ virtualenv:
system_site_packages: true

install:
- git clone https://github.com/akretion/server-tools.git ${HOME}/server-tools-attachm -b 8-attachm-meta
- git clone https://github.com/OCA/maintainer-quality-tools.git ${HOME}/maintainer-quality-tools
- export PATH=${HOME}/maintainer-quality-tools/travis:${PATH}
- travis_install_nightly
Expand Down
80 changes: 80 additions & 0 deletions file_email/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

==========
File Email
==========

Abstract module for importing and processing the
attachment of an email. The attachment of the email will be imported
as an ir.attachment.metadata and then in your custom module
you can process it.

An example of processing can be found in account_statement_email


Usage
=====

Go the menu Settings > File Exchange > Configuration

A implementation must define in your own module to test this module on a use case



.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/149/8.0


Known issues / Roadmap
======================

The purpose of this module is not to import the data of the file but only exchange files with external application.


Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/server-tools/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed `feedback
<https://github.com/OCA/
server-tools/issues/new?body=module:%20
attachment_metadata%0Aversion:%20
8.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.


Contributors
------------

* Valentin CHEMIERE <valentin.chemiere@akretion.com>
* Sebastien BEAU <sebastian.beau@akretion.com>
* Joel Grand-Guillaume Camptocamp
* initOS <http://initos.com>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
1 change: 1 addition & 0 deletions file_email/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
26 changes: 26 additions & 0 deletions file_email/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8
# @author Sébastien BEAU @ Akretion
# @author Florian DA COSTA @ Akretion
# @author Benoit GUILLOT @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
'name': 'File Email',
'version': '8.0.1.0.0',
'author': 'Akretion,Odoo Community Association (OCA)',
'category': 'Generic Modules',
'license': 'AGPL-3',
'website': 'http://www.akretion.com/',
'depends': [
'attachment_metadata',
'fetchmail'
],
'data': [
'security/ir.model.access.csv',
"views/fetchmail_view.xml",
"views/attachment_view.xml",
],
'demo': [],
'installable': True,
'images': [],
}
2 changes: 2 additions & 0 deletions file_email/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import attachment
from . import fetchmail
109 changes: 109 additions & 0 deletions file_email/models/attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# coding: utf-8
# @author Sébastien BEAU @ Akretion
# @author Florian DA COSTA @ Akretion
# @author Benoit GUILLOT @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import models, fields, api
import base64


class IrAttachmentMetadata(models.Model):
_inherit = "ir.attachment.metadata"

fetchmail_attachment_condition_id = fields.Many2one(
'fetchmail.attachment.condition',
string='FetchMail condition',
help="The Fetchemail attachment condition used"
"to create this attachment")
fetchmail_server_id = fields.Many2one(
'fetchmail.server',
string='Email Server',
related='fetchmail_attachment_condition_id.server_id', store=True,
readonly=True,
help="The email server used to create this attachment")

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 True

def message_post(self, cr, uid, thread_id, body='', subject=None,
type='notification', subtype=None, parent_id=False,
attachments=None, content_subtype='html', context=None,
**kwargs):
if context.get('no_post'):
return None
return True

@api.model
def _get_attachment_metadata_data(self, condition, msg, att):
values = {
'fetchmail_attachment_condition_id': condition.id,
'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]),
'state': 'pending'
}
return values

@api.model
def prepare_data_from_basic_condition(self, condition, msg):
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 = self._get_attachment_metadata_data(
condition, msg, att)
break
return vals

@api.model
def _prepare_data_for_attachment_metadata(self, msg):
"""Method to prepare the data for creating a attachment metadata.
:param msg: a dictionnary with the email data
:type: dict

:return: a list of dictionnary that containt
the attachment metadata data
:rtype: list
"""
res = []
server_id = self._context.get('fetchmail_server_id', False)
file_condition_obj = self.env['fetchmail.attachment.condition']
cond_ids = file_condition_obj.search([('server_id', '=', server_id)])
if cond_ids:
for cond in cond_ids:
# this part of code raise an expetion if type != normal
# if cond.type == 'normal':
vals = self.prepare_data_from_basic_condition(cond, msg)
# else:
# vals = getattr(self, cond.type)(cond, msg)
if vals:
res.append(vals)
return res

@api.model
def message_new(self, msg, custom_values):
created_ids = []
res = self._prepare_data_for_attachment_metadata(msg)
if res:
for vals in res:
default = self._context.get('default_attachment_metadata_vals')
if default:
for key in default:
if key not in vals:
vals[key] = default[key]
created_ids.append(self.create(vals))
self._cr.commit()
return created_ids[0]
return None
72 changes: 72 additions & 0 deletions file_email/models/fetchmail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# coding: utf-8
# @author Sébastien BEAU @ Akretion
# @author Florian DA COSTA @ Akretion
# @author Benoit GUILLOT @ Akretion
# @author Mourad EL HADJ MIMOUNE <mourad.elhadj.mimoune@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import models, fields, api


class FetchmailServer(models.Model):
_inherit = 'fetchmail.server'

def company_default_get(self):
company_id = (self.env['res.company'].
_company_default_get('fetchmail.server'))
return self.env['res.company'].browse(company_id)

file_type = fields.Selection(
selection='get_file_type',
help='The file type will show some special option')
company_id = fields.Many2one(
'res.company', string='Company',
required=True, default=company_default_get)
attachment_metadata_condition_ids = fields.One2many(
'fetchmail.attachment.condition', 'server_id', string='Attachment')

@api.model
def get_file_type(self):
return [('basic_import', 'Basic import')]

@api.one
def get_context_for_server(self):
if self._context is None:
ctx = {}
else:
ctx = self._context.copy()
ctx['default_attachment_metadata_vals'] = {}
ctx['default_company_id'] = self.company_id.id
ctx['default_fetchmail_server_id'] = self.id
return ctx

@api.multi
def fetch_mail(self):
for server in self:
super(FetchmailServer, server).fetch_mail()
return True


class FetchmailAttachmentCondition(models.Model):
_name = 'fetchmail.attachment.condition'
_description = "Fetchmail Attachment Conditions"

@api.model
def _get_attachment_metadata_condition_type(self):
return self.get_attachment_metadata_condition_type()

def get_attachment_metadata_condition_type(self):
return [('normal', 'Normal')]

name = fields.Char(string='Name', required=True,)
from_email = fields.Char(string='Email')
mail_subject = fields.Char()
type = fields.Selection(
selection='_get_attachment_metadata_condition_type',
required=True, default='normal',
help="Create your own type if the normal type "
"do not correspond to your need")
file_extension = fields.Char(
required=True,
help="File extension or file name")
server_id = fields.Many2one('fetchmail.server', string='Server Mail')
3 changes: 3 additions & 0 deletions file_email/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_fetchmail_attachment_condition_manager,fetchmail.attachment.condition.user,model_fetchmail_attachment_condition,base.group_system,1,1,1,1
access_fetchmail_attachment_condition_user,fetchmail.attachment.condition.user,model_fetchmail_attachment_condition,base.group_user,1,0,0,0
24 changes: 24 additions & 0 deletions file_email/views/attachment_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<openerp>
<data>

<!-- SEARCH -->
<record id="attachment_metadata_view_search" model="ir.ui.view">
<field name="name">ir.attachment.metadata.search</field>
<field name="model">ir.attachment.metadata</field>
<field name="type">search</field>
<field name="inherit_id"
ref="attachment_metadata.view_external_attachment_search"/>
<field name="arch" type="xml">
<field name="type" position="after">
<field name="fetchmail_server_id"/>
<field name="fetchmail_attachment_condition_id"/>
</field>
</field>
</record>



</data>
</openerp>

Loading