From 20a7ba1b1458cc8e76bcc35bb2e309edfca342cf Mon Sep 17 00:00:00 2001 From: Keyur Gajjar Date: Wed, 30 Mar 2016 10:15:37 +0200 Subject: [PATCH] [MIGR] fetchmail : migrate to new api --- addons/fetchmail/__openerp__.py | 6 +- addons/fetchmail/data/fetchmail_data.xml | 4 +- addons/fetchmail/models/__init__.py | 2 + addons/fetchmail/models/fetchmail.py | 309 ++++++++++----------- addons/fetchmail/models/mail_mail.py | 36 ++- addons/fetchmail/models/res_config.py | 12 +- addons/fetchmail/views/fetchmail_views.xml | 22 +- 7 files changed, 178 insertions(+), 213 deletions(-) diff --git a/addons/fetchmail/__openerp__.py b/addons/fetchmail/__openerp__.py index 4213734ccc4..3ae03d6020e 100644 --- a/addons/fetchmail/__openerp__.py +++ b/addons/fetchmail/__openerp__.py @@ -2,9 +2,9 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. { - 'name' : 'Email Gateway', - 'version' : '1.0', - 'depends' : ['mail'], + 'name': 'Email Gateway', + 'version': '1.0', + 'depends': ['mail'], 'category': 'Tools', 'description': """ Retrieve incoming email on POP/IMAP servers. diff --git a/addons/fetchmail/data/fetchmail_data.xml b/addons/fetchmail/data/fetchmail_data.xml index 3f6592b340f..34469793c6e 100644 --- a/addons/fetchmail/data/fetchmail_data.xml +++ b/addons/fetchmail/data/fetchmail_data.xml @@ -1,5 +1,5 @@ - + Fetchmail Service @@ -14,4 +14,4 @@ - + diff --git a/addons/fetchmail/models/__init__.py b/addons/fetchmail/models/__init__.py index b09bd3023dd..9b6348f2c01 100644 --- a/addons/fetchmail/models/__init__.py +++ b/addons/fetchmail/models/__init__.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + import fetchmail import res_config import mail_mail diff --git a/addons/fetchmail/models/fetchmail.py b/addons/fetchmail/models/fetchmail.py index b21ee4a9a7e..45f7e81a240 100644 --- a/addons/fetchmail/models/fetchmail.py +++ b/addons/fetchmail/models/fetchmail.py @@ -3,20 +3,12 @@ import logging import poplib -import time -from imaplib import IMAP4 -from imaplib import IMAP4_SSL -from poplib import POP3 -from poplib import POP3_SSL -try: - import cStringIO as StringIO -except ImportError: - import StringIO +from imaplib import IMAP4, IMAP4_SSL +from poplib import POP3, POP3_SSL + +from odoo import api, fields, models, tools, _ +from odoo.exceptions import UserError -from openerp.osv import fields, osv -from openerp import tools, api, SUPERUSER_ID -from openerp.tools.translate import _ -from openerp.exceptions import UserError _logger = logging.getLogger(__name__) MAX_POP_MESSAGES = 50 @@ -25,129 +17,124 @@ MAIL_TIMEOUT = 60 # Workaround for Python 2.7.8 bug https://bugs.python.org/issue23906 poplib._MAXLINE = 65536 -class fetchmail_server(osv.osv): + +class FetchmailServer(models.Model): """Incoming POP/IMAP mail server account""" + _name = 'fetchmail.server' _description = "POP/IMAP Server" _order = 'priority' - _columns = { - 'name':fields.char('Name', required=True, readonly=False), - 'active':fields.boolean('Active', required=False), - 'state':fields.selection([ - ('draft', 'Not Confirmed'), - ('done', 'Confirmed'), - ], 'Status', select=True, readonly=True, copy=False), - 'server' : fields.char('Server Name', readonly=True, help="Hostname or IP of the mail server", states={'draft':[('readonly', False)]}), - 'port' : fields.integer('Port', readonly=True, states={'draft':[('readonly', False)]}), - 'type':fields.selection([ - ('pop', 'POP Server'), - ('imap', 'IMAP Server'), - ('local', 'Local Server'), - ], 'Server Type', select=True, required=True, readonly=False), - 'is_ssl':fields.boolean('SSL/TLS', help="Connections are encrypted with SSL/TLS through a dedicated port (default: IMAPS=993, POP3S=995)"), - 'attach':fields.boolean('Keep Attachments', help="Whether attachments should be downloaded. " - "If not enabled, incoming emails will be stripped of any attachments before being processed"), - 'original':fields.boolean('Keep Original', help="Whether a full original copy of each email should be kept for reference " - "and attached to each processed message. This will usually double the size of your message database."), - 'date': fields.datetime('Last Fetch Date', readonly=True), - 'user' : fields.char('Username', readonly=True, states={'draft':[('readonly', False)]}), - 'password' : fields.char('Password', readonly=True, states={'draft':[('readonly', False)]}), - 'action_id':fields.many2one('ir.actions.server', 'Server Action', help="Optional custom server action to trigger for each incoming mail, " - "on the record that was created or updated by this mail"), - 'object_id': fields.many2one('ir.model', "Create a New Record", help="Process each incoming mail as part of a conversation " - "corresponding to this document type. This will create " - "new documents for new conversations, or attach follow-up " - "emails to the existing conversations (documents)."), - 'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly', False)]}, help="Defines the order of processing, " - "lower values mean higher priority"), - 'message_ids': fields.one2many('mail.mail', 'fetchmail_server_id', 'Messages', readonly=True), - 'configuration' : fields.text('Configuration', readonly=True), - 'script' : fields.char('Script', readonly=True), - } - _defaults = { - 'state': "draft", - 'type': "pop", - 'active': True, - 'priority': 5, - 'attach': True, - 'script': '/mail/static/scripts/openerp_mailgate.py', - } + name = fields.Char('Name', required=True) + active = fields.Boolean('Active', default=True) + state = fields.Selection([ + ('draft', 'Not Confirmed'), + ('done', 'Confirmed'), + ], string='Status', index=True, readonly=True, copy=False, default='draft') + server = fields.Char(string='Server Name', readonly=True, help="Hostname or IP of the mail server", states={'draft': [('readonly', False)]}) + port = fields.Integer(readonly=True, states={'draft': [('readonly', False)]}) + type = fields.Selection([ + ('pop', 'POP Server'), + ('imap', 'IMAP Server'), + ('local', 'Local Server'), + ], 'Server Type', index=True, required=True, default='pop') + is_ssl = fields.Boolean('SSL/TLS', help="Connections are encrypted with SSL/TLS through a dedicated port (default: IMAPS=993, POP3S=995)") + attach = fields.Boolean('Keep Attachments', help="Whether attachments should be downloaded. " + "If not enabled, incoming emails will be stripped of any attachments before being processed", default=True) + original = fields.Boolean('Keep Original', help="Whether a full original copy of each email should be kept for reference " + "and attached to each processed message. This will usually double the size of your message database.") + date = fields.Datetime(string='Last Fetch Date', readonly=True) + user = fields.Char(string='Username', readonly=True, states={'draft': [('readonly', False)]}) + password = fields.Char(readonly=True, states={'draft': [('readonly', False)]}) + action_id = fields.Many2one('ir.actions.server', string='Server Action', help="Optional custom server action to trigger for each incoming mail, on the record that was created or updated by this mail") + object_id = fields.Many2one('ir.model', string="Create a New Record", help="Process each incoming mail as part of a conversation " + "corresponding to this document type. This will create " + "new documents for new conversations, or attach follow-up " + "emails to the existing conversations (documents).") + priority = fields.Integer(string='Server Priority', readonly=True, states={'draft': [('readonly', False)]}, help="Defines the order of processing, lower values mean higher priority", default=5) + message_ids = fields.One2many('mail.mail', 'fetchmail_server_id', string='Messages', readonly=True) + configuration = fields.Text('Configuration', readonly=True) + script = fields.Char(readonly=True, default='/mail/static/scripts/openerp_mailgate.py') - def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False, object_id=False): - port = 0 - values = {} - if server_type == 'pop': - port = ssl and 995 or 110 - elif server_type == 'imap': - port = ssl and 993 or 143 + @api.onchange('type', 'is_ssl', 'object_id') + def onchange_server_type(self): + self.port = 0 + if self.type == 'pop': + self.port = self.is_ssl and 995 or 110 + elif self.type == 'imap': + self.port = self.is_ssl and 993 or 143 else: - values['server'] = '' - values['port'] = port + self.server = '' conf = { - 'dbname' : cr.dbname, - 'uid' : uid, - 'model' : 'MODELNAME', + 'dbname': self.env.cr.dbname, + 'uid': self.env.uid, + 'model': self.object_id.model if self.object_id else 'MODELNAME' } - if object_id: - m = self.pool.get('ir.model') - r = m.read(cr,uid,[object_id],['model']) - conf['model']=r[0]['model'] - values['configuration'] = """Use the below script with the following command line options with your Mail Transport Agent (MTA) + self.configuration = """ + Use the below script with the following command line options with your Mail Transport Agent (MTA) + openerp_mailgate.py --host=HOSTNAME --port=PORT -u %(uid)d -p PASSWORD -d %(dbname)s + Example configuration for the postfix mta running locally: + /etc/postfix/virtual_aliases: + @youdomain openerp_mailgate@localhost + /etc/aliases: + openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p PASSWORD -d %(dbname)s" + """ % conf -openerp_mailgate.py --host=HOSTNAME --port=PORT -u %(uid)d -p PASSWORD -d %(dbname)s + @api.model + def create(self, values): + res = super(FetchmailServer, self).create(values) + self._update_cron() + return res -Example configuration for the postfix mta running locally: + @api.multi + def write(self, values): + res = super(FetchmailServer, self).write(values) + self._update_cron() + return res -/etc/postfix/virtual_aliases: -@youdomain openerp_mailgate@localhost + @api.multi + def unlink(self): + res = super(FetchmailServer, self).unlink() + self._update_cron() + return res -/etc/aliases: -openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p PASSWORD -d %(dbname)s" - -""" % conf - - return {'value':values} - - def set_draft(self, cr, uid, ids, context=None): - self.write(cr, uid, ids , {'state':'draft'}) + @api.multi + def set_draft(self): + self.write({'state': 'draft'}) return True - @api.cr_uid_ids_context - def connect(self, cr, uid, server_id, context=None): - if isinstance(server_id, (list,tuple)): - server_id = server_id[0] - server = self.browse(cr, uid, server_id, context) - if server.type == 'imap': - if server.is_ssl: - connection = IMAP4_SSL(server.server, int(server.port)) + @api.multi + def connect(self): + self.ensure_one() + if self.type == 'imap': + if self.is_ssl: + connection = IMAP4_SSL(self.server, int(self.port)) else: - connection = IMAP4(server.server, int(server.port)) - connection.login(server.user, server.password) - elif server.type == 'pop': - if server.is_ssl: - connection = POP3_SSL(server.server, int(server.port)) + connection = IMAP4(self.server, int(self.port)) + connection.login(self.user, self.password) + elif self.type == 'pop': + if self.is_ssl: + connection = POP3_SSL(self.server, int(self.port)) else: - connection = POP3(server.server, int(server.port)) + connection = POP3(self.server, int(self.port)) #TODO: use this to remove only unread messages #connection.user("recent:"+server.user) - connection.user(server.user) - connection.pass_(server.password) + connection.user(self.user) + connection.pass_(self.password) # Add timeout on socket connection.sock.settimeout(MAIL_TIMEOUT) return connection - def button_confirm_login(self, cr, uid, ids, context=None): - if context is None: - context = {} - for server in self.browse(cr, uid, ids, context=context): + @api.multi + def button_confirm_login(self): + for server in self: try: connection = server.connect() - server.write({'state':'done'}) - except Exception, e: + server.write({'state': 'done'}) + except Exception, err: _logger.info("Failed to connect to %s server %s.", server.type, server.name, exc_info=True) - raise UserError(_("Connection test failed: %s") % tools.ustr(e)) + raise UserError(_("Connection test failed: %s") % tools.ustr(err)) finally: try: if connection: @@ -160,23 +147,25 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p pass return True - def _fetch_mails(self, cr, uid, ids=False, context=None): - if not ids: - ids = self.search(cr, uid, [('state','=','done'),('type','in',['pop','imap'])]) - return self.fetch_mail(cr, uid, ids, context=context) + @api.model + def _fetch_mails(self): + """ Method called by cron to fetch mails from servers """ + return self.search([('state', '=', 'done'), ('type', 'in', ['pop', 'imap'])]).fetch_mail() - def fetch_mail(self, cr, uid, ids, context=None): - """WARNING: meant for cron usage only - will commit() after each email!""" - context = dict(context or {}) - context['fetchmail_cron_running'] = True - mail_thread = self.pool.get('mail.thread') - action_pool = self.pool.get('ir.actions.server') - for server in self.browse(cr, uid, ids, context=context): + @api.multi + def fetch_mail(self): + """ WARNING: meant for cron usage only - will commit() after each email! """ + additionnal_context = { + 'fetchmail_cron_running': True + } + MailThread = self.env['mail.thread'] + for server in self: _logger.info('start checking for new emails on %s server %s', server.type, server.name) - context.update({'fetchmail_server_id': server.id, 'server_type': server.type}) + additionnal_context['fetchmail_server_id'] = server.id + additionnal_context['server_type'] = server.type count, failed = 0, 0 - imap_server = False - pop_server = False + imap_server = None + pop_server = None if server.type == 'imap': try: imap_server = server.connect() @@ -187,18 +176,18 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p result, data = imap_server.fetch(num, '(RFC822)') imap_server.store(num, '-FLAGS', '\\Seen') try: - res_id = mail_thread.message_process(cr, uid, server.object_id.model, - data[0][1], - save_original=server.original, - strip_attachments=(not server.attach), - context=context) + res_id = MailThread.with_context(**additionnal_context).message_process(server.object_id.model, data[0][1], save_original=server.original, strip_attachments=(not server.attach)) except Exception: _logger.info('Failed to process mail from %s server %s.', server.type, server.name, exc_info=True) failed += 1 if res_id and server.action_id: - action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids': [res_id], 'active_model': context.get("thread_model", server.object_id.model)}) + server.action_id.with_context({ + 'active_id': res_id, + 'active_ids': [res_id], + 'active_model': self.env.context.get("thread_model", server.object_id.model) + }).run() imap_server.store(num, '+FLAGS', '\\Seen') - cr.commit() + self._cr.commit() count += 1 _logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", count, server.type, server.name, (count - failed), failed) except Exception: @@ -211,62 +200,44 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p try: while True: pop_server = server.connect() - (numMsgs, totalSize) = pop_server.stat() + (num_messages, total_size) = pop_server.stat() pop_server.list() - for num in range(1, min(MAX_POP_MESSAGES, numMsgs) + 1): - (header, msges, octets) = pop_server.retr(num) - msg = '\n'.join(msges) + for num in range(1, min(MAX_POP_MESSAGES, num_messages) + 1): + (header, messages, octets) = pop_server.retr(num) + message = '\n'.join(messages) res_id = None try: - res_id = mail_thread.message_process(cr, uid, server.object_id.model, - msg, - save_original=server.original, - strip_attachments=(not server.attach), - context=context) + res_id = MailThread.with_context(**additionnal_context).message_process(server.object_id.model, message, save_original=server.original, strip_attachments=(not server.attach)) pop_server.dele(num) except Exception: _logger.info('Failed to process mail from %s server %s.', server.type, server.name, exc_info=True) failed += 1 if res_id and server.action_id: - action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids': [res_id], 'active_model': context.get("thread_model", server.object_id.model)}) - cr.commit() - if numMsgs < MAX_POP_MESSAGES: + server.action_id.with_context({ + 'active_id': res_id, + 'active_ids': [res_id], + 'active_model': self.env.context.get("thread_model", server.object_id.model) + }).run() + self.env.cr.commit() + if num_messages < MAX_POP_MESSAGES: break pop_server.quit() - _logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", numMsgs, server.type, server.name, (numMsgs - failed), failed) + _logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", num_messages, server.type, server.name, (num_messages - failed), failed) except Exception: _logger.info("General failure when trying to fetch mail from %s server %s.", server.type, server.name, exc_info=True) finally: if pop_server: pop_server.quit() - server.write({'date': time.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)}) + server.write({'date': fields.Datetime.now()}) return True - def _update_cron(self, cr, uid, context=None): - if context and context.get('fetchmail_cron_running'): + @api.model + def _update_cron(self): + if self.env.context.get('fetchmail_cron_running'): return - try: - cron = self.pool['ir.model.data'].get_object( - cr, SUPERUSER_ID, 'fetchmail', 'ir_cron_mail_gateway_action', context=context) + # Enabled/Disable cron based on the number of 'done' server of type pop or imap + cron = self.env.ref('fetchmail.ir_cron_mail_gateway_action') + cron.toggle(model=self._name, domain=[('state', '=', 'done'), ('type', 'in', ['pop', 'imap'])]) except ValueError: - # Nevermind if default cron cannot be found - return - - # Enabled/Disable cron based on the number of 'done' server of type pop or imap - cron.toggle(model=self._name, domain=[('state','=','done'), ('type','in',['pop','imap'])]) - - def create(self, cr, uid, values, context=None): - res = super(fetchmail_server, self).create(cr, uid, values, context=context) - self._update_cron(cr, uid, context=context) - return res - - def write(self, cr, uid, ids, values, context=None): - res = super(fetchmail_server, self).write(cr, uid, ids, values, context=context) - self._update_cron(cr, uid, context=context) - return res - - def unlink(self, cr, uid, ids, context=None): - res = super(fetchmail_server, self).unlink(cr, uid, ids, context=context) - self._update_cron(cr, uid, context=context) - return res + pass diff --git a/addons/fetchmail/models/mail_mail.py b/addons/fetchmail/models/mail_mail.py index 57c5fca4561..66a9154af70 100644 --- a/addons/fetchmail/models/mail_mail.py +++ b/addons/fetchmail/models/mail_mail.py @@ -1,29 +1,25 @@ # -*- coding: utf-8 -*- -from openerp.osv import fields, osv +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models + + +class MailMail(models.Model): -class mail_mail(osv.osv): _inherit = "mail.mail" - _columns = { - 'fetchmail_server_id': fields.many2one('fetchmail.server', "Inbound Mail Server", - readonly=True, - select=True, - oldname='server_id'), - } - def create(self, cr, uid, values, context=None): - if context is None: - context = {} - fetchmail_server_id = context.get('fetchmail_server_id') + fetchmail_server_id = fields.Many2one('fetchmail.server', "Inbound Mail Server", readonly=True, index=True, oldname='server_id') + + @api.model + def create(self, values): + fetchmail_server_id = self.env.context.get('fetchmail_server_id') if fetchmail_server_id: values['fetchmail_server_id'] = fetchmail_server_id - res = super(mail_mail, self).create(cr, uid, values, context=context) - return res + return super(MailMail, self).create(values) - def write(self, cr, uid, ids, values, context=None): - if context is None: - context = {} - fetchmail_server_id = context.get('fetchmail_server_id') + @api.multi + def write(self, values): + fetchmail_server_id = self.env.context.get('fetchmail_server_id') if fetchmail_server_id: values['fetchmail_server_id'] = fetchmail_server_id - res = super(mail_mail, self).write(cr, uid, ids, values, context=context) - return res + return super(MailMail, self).write(values) diff --git a/addons/fetchmail/models/res_config.py b/addons/fetchmail/models/res_config.py index a3020ded49d..c58e2ca2947 100644 --- a/addons/fetchmail/models/res_config.py +++ b/addons/fetchmail/models/res_config.py @@ -1,22 +1,20 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from openerp.osv import fields, osv +from odoo import api, fields, models -class fetchmail_config_settings(osv.osv_memory): + +class FetchmailConfigSettings(models.TransientModel): """ This wizard can be inherited in conjunction with 'res.config.settings', in order to define fields that configure a fetchmail server. It relies on the following convention on the object:: - class my_config_settings(osv.osv_memory): + class MyConfigSettings(models.TransientModel): _name = 'my.settings' _inherit = ['res.config.settings', 'fetchmail.config.settings'] - _columns = { - 'fetchmail_stuff': fields.boolean(..., - fetchmail_model='my.stuff', fetchmail_name='Incoming Stuff'), - } + fetchmail_stuff = fields.Boolean(..., fetchmail_model='my.stuff', fetchmail_name='Incoming Stuff'), def configure_fetchmail_stuff(self, cr, uid, ids, context=None): return self.configure_fetchmail(cr, uid, 'fetchmail_stuff', context) diff --git a/addons/fetchmail/views/fetchmail_views.xml b/addons/fetchmail/views/fetchmail_views.xml index 63708c3fe1a..409e181ce1d 100644 --- a/addons/fetchmail/views/fetchmail_views.xml +++ b/addons/fetchmail/views/fetchmail_views.xml @@ -1,8 +1,7 @@ - - + - + fetchmail.server.list fetchmail.server @@ -18,7 +17,7 @@ - + fetchmail.server.form fetchmail.server @@ -32,7 +31,7 @@ - + @@ -41,14 +40,14 @@ - + - + @@ -71,7 +70,7 @@ - + fetchmail.server.search fetchmail.server @@ -85,7 +84,7 @@ - + Incoming Mail Servers fetchmail.server form @@ -93,7 +92,7 @@ - + General Settings base.config.settings @@ -119,5 +118,4 @@ groups="base.group_no_one" /> - - +