Migration from v7 api to v8 api done, working on pep8 now

This commit is contained in:
Valentin Chemiere 2015-03-24 16:46:54 +01:00
parent 9330dcd908
commit 0932cb1e73
3 changed files with 101 additions and 91 deletions

View File

@ -31,7 +31,6 @@ from base64 import b64decode
class IrAttachmentMetadata(models.Model):
_name = 'ir.attachment.metadata'
_inherits = {'ir.attachment': 'attachment_id'}
_inherit = ['mail.thread', 'ir.needaction_mixin']
internal_hash = fields.Char(store=True, compute='_compute_hash')
external_hash = fields.Char()

View File

@ -20,49 +20,54 @@
#
###############################################################################
from openerp.osv import fields, orm
from openerp import models, fields, api
import base64
class IrAttachmentMetadata(orm.Model):
class IrAttachmentMetadata(models.Model):
_inherit = "ir.attachment.metadata"
_columns = {
'fetchmail_server_id': fields.many2one('fetchmail.server', 'Email Server'),
}
fetchmail_server_id = fields.Many2one('fetchmail.server', string='Email Server')
def message_process(self, cr, uid, model, message, custom_values=None,
save_original=False, strip_attachments=False,
thread_id=None, context=None):
print "message process"
if context is None:
context = {}
context = {}
context['no_post'] = True
return super(IrAttachmentMetadata, 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)
return True
# return super(IrAttachmentMetadata, 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):
subtype=None, parent_id=False, attachments=None,
content_subtype='html', context=None, **kwargs):
print "message post"
if context.get('no_post'):
return None
return super(IrAttachmentMetadata, 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)
return True
# return super(IrAttachmentMetadata, self).message_post(cr, uid, thread_id,
# body=body,
# subject=subject,
# type='notification',
# subtype=subtype,
# parent_id=parent_id,
# attachments=attachments,
# content_subtype=content_subtype,
# context=context,
# **kwargs)
def _get_attachment_metadata_data(self, cr, uid, condition, msg, att, context=None):
@api.model
def _get_attachment_metadata_data(self, condition, msg, att):
values = {
'file_type': condition.server_id.file_type,
'name': msg['subject'],
@ -70,21 +75,24 @@ class IrAttachmentMetadata(orm.Model):
'date': msg['date'],
'ext_id': msg['message_id'],
'datas_fname': att[0],
'datas': base64.b64encode(att[1])
'datas': base64.b64encode(att[1]),
'state': 'pending'
}
return values
def prepare_data_from_basic_condition(self, cr, uid, condition, msg, context=None):
@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(cr, uid, condition, msg, att, context=context)
vals = self._get_attachment_metadata_data(condition, msg, att)
break
return vals
def _prepare_data_for_attachment_metadata(self, cr, uid, msg, context=None):
@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
@ -93,60 +101,59 @@ class IrAttachmentMetadata(orm.Model):
:rtype: list
"""
res = []
server_id = context.get('default_fetchmail_server_id', False)
doc_file_condition_obj = self.pool.get('ir.attachment.metadata.condition')
cond_ids = doc_file_condition_obj.search(cr, uid, [('server_id', '=', server_id)])
server_id = self._context.get('fetchmail_server_id', False)
file_condition_obj = self.env['ir.attachment.metadata.condition']
cond_ids = file_condition_obj.search([('server_id', '=', server_id)])
if cond_ids:
for cond in doc_file_condition_obj.browse(cr, uid, cond_ids):
for cond in cond_ids:
if cond.type == 'normal':
vals = self.prepare_data_from_basic_condition(cr, uid, cond, msg, context=context)
vals = self.prepare_data_from_basic_condition(cond, msg)
else:
vals = getattr(self, cond.type)(cr, uid, cond, msg, context=context)
vals = getattr(self, cond.type)(cond, msg)
if vals:
res.append(vals)
return res
def message_new(self, cr, uid, msg, custom_values, context=None):
@api.model
def message_new(self, msg, custom_values):
print "message new"
created_ids = []
res = self._prepare_data_for_attachment_metadata(cr, uid, msg, context=context)
res = self._prepare_data_for_attachment_metadata(msg)
if res:
for vals in res:
default = context.get('default_attachment_metadata_vals')
default = self._context.get('default_attachment_metadata_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))
cr.commit()
context['created_ids'] = created_ids
return created_ids[0]
created_ids.append(self.create(vals))
self._cr.commit()
#self._context['created_ids'] = created_ids
return created_ids[0].id
return None
class IrAttachmentMetadataCondition(orm.Model):
class IrAttachmentMetadataCondition(models.Model):
_name = "ir.attachment.metadata.condition"
_description = "Attachment Metadata Conditions"
def _get_attachment_metadata_condition_type(self, cr, uid, context=None):
return self.get_attachment_metadata_condition_type(cr, uid, context=context)
@api.model
def _get_attachment_metadata_condition_type(self):
return self.get_attachment_metadata_condition_type()
def get_attachment_metadata_condition_type(self, cr, uid, context=None):
def get_attachment_metadata_condition_type(self):
return [('normal', 'Normal')]
_columns = {
'from_email': fields.char('Email', size=64),
'mail_subject': fields.char('Mail Subject', size=64),
'type': fields.selection(_get_attachment_metadata_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'
}
from_email = fields.Char(string='Email', size=64)
mail_subject = fields.Char(size=64)
type = fields.Selection(
selection='_get_attachment_metadata_condition_type',
help="Create your own type if the normal type \
do not correspond to your need",
required=True,
default='normal'
)
file_extension = fields.Char(size=64,
help="File extension or file name",
required=True)
server_id = fields.Many2one('fetchmail.server', string='Server Mail')

View File

@ -20,43 +20,47 @@
#
###############################################################################
from openerp.osv import fields, orm
from openerp import models, fields, api
class fetchmail_server(orm.Model):
class fetchmail_server(models.Model):
_inherit = 'fetchmail.server'
def get_file_type(self, cr, uid, context=None):
@api.model
def get_file_type(self):
return []
@api.model
def _get_file_type(self):
return self.get_file_type()
def _get_file_type(self, cr, uid, context=None):
return self.get_file_type(cr, uid, context=context)
def company_default_get(self):
company_id = self.env['res.company']._company_default_get('fetchmail.server')
return self.env['res.company'].browse(company_id)
_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?
'attachment_metadata_condition_ids': fields.one2many('ir.attachment.metadata.condition', 'server_id', 'Attachment')
}
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
) #Why this field do not exist by default?
attachment_metadata_condition_ids = fields.One2many(
'ir.attachment.metadata.condition', 'server_id', string='Attachment')
_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:
@api.one
def get_context_for_server(self):
if self._context is None:
ctx = {}
else:
ctx = context.copy()
ctx = self._context.copy()
ctx['default_attachment_metadata_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
ctx['default_company_id'] = self.company_id.id
ctx['default_fetchmail_server_id'] = self.id
return ctx
def fetch_mail(self, cr, uid, ids, context=None):
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)
@api.multi
def fetch_mail(self):
for server in self:
ctx = server.get_context_for_server()
super(fetchmail_server, server).fetch_mail()
return True