[MERGE] merge condition parser for import attachment from email

This commit is contained in:
Florian Dacosta 2014-01-06 19:30:46 +01:00 committed by Sebastien Beau
commit 61a90782e6
4 changed files with 102 additions and 6 deletions

View File

@ -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('file.document.condition', 'server_id', 'File Document ')
}
_defaults = {

View File

@ -12,6 +12,9 @@
<field name="object_id" position="after">
<field name="file_type"/> <!-- TODO attrs="{'invisible': [('object_id','!=', 'file.document')]}"/>-->
</field>
<field name="action_id" position="after">
<field name="file_document_condition_ids" nolabel="1"/>
</field>
</field>
</record>

View File

@ -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,
@ -65,7 +62,23 @@ class file_document(orm.Model):
context=context,
content_subtype=content_subtype,
**kwargs)
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):
@ -76,7 +89,19 @@ class file_document(orm.Model):
:return: a list of dictionnary that containt the file document data
:rtype: list
"""
return []
res = []
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):
created_ids = []
@ -94,3 +119,30 @@ class file_document(orm.Model):
return created_ids[0]
return None
class file_document_condition(orm.Model):
_name = "file.document.condition"
_description = "File Document Conditions"
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')]
_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),
'server_id': fields.many2one('fetchmail.server', 'Server Mail'),
}
_defaults = {
'type': 'normal'
}

View File

@ -15,6 +15,46 @@
</field>
</record>
<!-- Views and menus for the new object prepare.file.document -->
<record id="view_file_document_condition_tree" model="ir.ui.view">
<field name="name">view_file_document_condition_tree</field>
<field name="model">file.document.condition</field>
<field name="arch" type="xml">
<tree string="Configuration for File Document">
<field name="from_email"/>
<field name="mail_subject"/>
<field name="type"/>
<field name="file_extension"/>
<field name="server_id"/>
</tree>
</field>
</record>
<record id="view_file_document_condition_form" model="ir.ui.view">
<field name="name">view_file_document_condition_form</field>
<field name="model">file.document.condition</field>
<field name="arch" type="xml">
<form string="Configuration for File Document">
<field name="from_email" attrs="{'required': [('type', '=', 'normal')]}"/>
<field name="mail_subject" attrs="{'required': [('type', '=', 'normal')]}"/>
<field name="type"/>
<field name="file_extension"/>
<field name="server_id"/>
</form>
</field>
</record>
<record id="action_file_document_configuration" model="ir.actions.act_window">
<field name="name">Configuration</field>
<field name="res_model">file.document.condition</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_file_document_configuration"
action="action_file_document_configuration"
parent="file_document.menu_file_exchange_root"
sequence="50" />
</data>
</openerp>