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): class IrAttachmentMetadata(models.Model):
_name = 'ir.attachment.metadata' _name = 'ir.attachment.metadata'
_inherits = {'ir.attachment': 'attachment_id'} _inherits = {'ir.attachment': 'attachment_id'}
_inherit = ['mail.thread', 'ir.needaction_mixin']
internal_hash = fields.Char(store=True, compute='_compute_hash') internal_hash = fields.Char(store=True, compute='_compute_hash')
external_hash = fields.Char() external_hash = fields.Char()

View File

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

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' _inherit = 'fetchmail.server'
def get_file_type(self, cr, uid, context=None): @api.model
def get_file_type(self):
return [] return []
def _get_file_type(self, cr, uid, context=None): @api.model
return self.get_file_type(cr, uid, context=context) def _get_file_type(self):
return self.get_file_type()
_columns = { def company_default_get(self):
'file_type': fields.selection(_get_file_type, 'File Type', company_id = self.env['res.company']._company_default_get('fetchmail.server')
help='The file type will show some special option'), return self.env['res.company'].browse(company_id)
'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')
}
_defaults = { file_type = fields.Selection(selection='_get_file_type',
'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'fetchmail.server', context=c), 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')
@api.one
def get_context_for_server(self, cr, uid, server_id, context=None): def get_context_for_server(self):
if context is None: if self._context is None:
ctx = {} ctx = {}
else: else:
ctx = context.copy() ctx = self._context.copy()
ctx['default_attachment_metadata_vals'] = {} ctx['default_attachment_metadata_vals'] = {}
server = self.browse(cr, uid, server_id, context=context) ctx['default_company_id'] = self.company_id.id
ctx['default_company_id'] = server.company_id.id ctx['default_fetchmail_server_id'] = self.id
ctx['default_fetchmail_server_id'] = server_id
return ctx return ctx
def fetch_mail(self, cr, uid, ids, context=None): @api.multi
for server_id in ids: def fetch_mail(self):
ctx = self.get_context_for_server(cr, uid, server_id, context=context) for server in self:
super(fetchmail_server, self).fetch_mail(cr, uid, [server_id], context=ctx) ctx = server.get_context_for_server()
super(fetchmail_server, server).fetch_mail()
return True return True