mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-21 11:37:26 -06:00
[REF] Refactore prepare data for file document function + clear some code
This commit is contained in:
parent
8e691f8a88
commit
1396b72c96
@ -36,7 +36,7 @@ class fetchmail_server(orm.Model):
|
|||||||
'file_type': fields.selection(_get_file_type, 'File Type',
|
'file_type': fields.selection(_get_file_type, 'File Type',
|
||||||
help='The file type will show some special option'),
|
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?
|
'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 = {
|
_defaults = {
|
||||||
@ -50,11 +50,9 @@ class fetchmail_server(orm.Model):
|
|||||||
else:
|
else:
|
||||||
ctx = context.copy()
|
ctx = context.copy()
|
||||||
ctx['default_file_document_vals'] = {}
|
ctx['default_file_document_vals'] = {}
|
||||||
ctx['default_file_document_condition_ids'] = {}
|
|
||||||
server = self.browse(cr, uid, server_id, context=context)
|
server = self.browse(cr, uid, server_id, context=context)
|
||||||
ctx['default_company_id'] = server.company_id.id
|
ctx['default_company_id'] = server.company_id.id
|
||||||
ctx['default_fetchmail_server_id'] = server_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
|
return ctx
|
||||||
|
|
||||||
def fetch_mail(self, cr, uid, ids, context=None):
|
def fetch_mail(self, cr, uid, ids, context=None):
|
||||||
|
@ -62,13 +62,23 @@ class file_document(orm.Model):
|
|||||||
context=context,
|
context=context,
|
||||||
content_subtype=content_subtype,
|
content_subtype=content_subtype,
|
||||||
**kwargs)
|
**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):
|
def prepare_data_from_basic_condition(self, cr, uid, condition, msg, context=None):
|
||||||
return {}
|
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):
|
def _prepare_data_for_file_document(self, cr, uid, msg, context=None):
|
||||||
@ -80,29 +90,17 @@ class file_document(orm.Model):
|
|||||||
:rtype: list
|
:rtype: list
|
||||||
"""
|
"""
|
||||||
res = []
|
res = []
|
||||||
doc_file_condition_obj = self.pool.get('prepare.file.document')
|
server_id = context.get('default_fetchmail_server_id', False)
|
||||||
cond_ids = context.get('default_file_document_condition_ids', False)
|
doc_file_condition_obj = self.pool.get('file.document.condition')
|
||||||
for cond in doc_file_condition_obj.browse(cr, uid, cond_ids):
|
cond_ids = doc_file_condition_obj.search(cr, uid, [('server_id', '=', server_id)])
|
||||||
vals = {}
|
if cond_ids:
|
||||||
if cond.type == 'normal':
|
for cond in doc_file_condition_obj.browse(cr, uid, cond_ids):
|
||||||
if cond.from_email in msg['from'] and cond.mail_subject == msg['subject']:
|
if cond.type == 'normal':
|
||||||
vals = {
|
vals = self.prepare_data_from_basic_condition(cr, uid, cond, msg, context=context)
|
||||||
'name': msg['subject'],
|
else:
|
||||||
'direction': 'input',
|
vals = getattr(self, cond.type)(cr, uid, cond, msg, context=context)
|
||||||
'date': msg['date'],
|
if vals:
|
||||||
'ext_id': msg['message_id'],
|
res.append(vals)
|
||||||
}
|
|
||||||
#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
|
return res
|
||||||
|
|
||||||
def message_new(self, cr, uid, msg, custom_values, context=None):
|
def message_new(self, cr, uid, msg, custom_values, context=None):
|
||||||
@ -122,20 +120,19 @@ class file_document(orm.Model):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
class prepare_file_document(orm.Model):
|
class file_document_condition(orm.Model):
|
||||||
_name = "prepare.file.document"
|
_name = "file.document.condition"
|
||||||
_description = "Prepare File Document"
|
_description = "File Document Conditions"
|
||||||
|
|
||||||
def _get_prepare_file_document_type(self, cr, uid, context=None):
|
def _get_file_document_condition_type(self, cr, uid, context=None):
|
||||||
return self.get_prepare_file_document_type(cr, uid, context=context)
|
return self.get_file_document_condition_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 [('normal', 'Normal'), ('test', 'TEST')]
|
||||||
_columns = {
|
_columns = {
|
||||||
'from_email': fields.char('Email', size=64),
|
'from_email': fields.char('Email', size=64),
|
||||||
'mail_subject': fields.char('Mail Subject', 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),
|
'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),
|
'file_extension' : fields.char('File Extension', size=64, help="File extension or file name", required=True),
|
||||||
'server_id': fields.many2one('fetchmail.server', 'Server Mail'),
|
'server_id': fields.many2one('fetchmail.server', 'Server Mail'),
|
||||||
|
@ -16,9 +16,9 @@
|
|||||||
</record>
|
</record>
|
||||||
|
|
||||||
<!-- Views and menus for the new object prepare.file.document -->
|
<!-- Views and menus for the new object prepare.file.document -->
|
||||||
<record id="view_prepare_file_document_tree" model="ir.ui.view">
|
<record id="view_file_document_condition_tree" model="ir.ui.view">
|
||||||
<field name="name">view_prepare_file_document_tree</field>
|
<field name="name">view_file_document_condition_tree</field>
|
||||||
<field name="model">prepare.file.document</field>
|
<field name="model">file.document.condition</field>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<tree string="Configuration for File Document">
|
<tree string="Configuration for File Document">
|
||||||
<field name="from_email"/>
|
<field name="from_email"/>
|
||||||
@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
<record id="action_file_document_configuration" model="ir.actions.act_window">
|
<record id="action_file_document_configuration" model="ir.actions.act_window">
|
||||||
<field name="name">Configuration</field>
|
<field name="name">Configuration</field>
|
||||||
<field name="res_model">prepare.file.document</field>
|
<field name="res_model">file.document.condition</field>
|
||||||
<field name="view_type">form</field>
|
<field name="view_type">form</field>
|
||||||
<field name="view_mode">tree,form</field>
|
<field name="view_mode">tree,form</field>
|
||||||
</record>
|
</record>
|
||||||
|
Loading…
Reference in New Issue
Block a user