From bee7ea0722c1b1605553ab8db4745fd8ef360973 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Wed, 6 Jun 2018 14:03:26 +0200 Subject: [PATCH] [IMP] fetchmail_attach_from_folder. Separate confirm for folder. Now it is impossible to confirm a connection to a imap server if there is a problem in one folder. This commit provides a new state field on the folder and e separate comfirm button per folder. --- .../models/fetchmail_server.py | 20 ++---------- .../models/fetchmail_server_folder.py | 31 ++++++++++++++++--- .../views/fetchmail_server.xml | 8 ++++- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/fetchmail_attach_from_folder/models/fetchmail_server.py b/fetchmail_attach_from_folder/models/fetchmail_server.py index 7f29187e5..6ea3fb807 100644 --- a/fetchmail_attach_from_folder/models/fetchmail_server.py +++ b/fetchmail_attach_from_folder/models/fetchmail_server.py @@ -5,7 +5,7 @@ import logging import simplejson from lxml import etree -from odoo import _, api, exceptions, fields, models +from odoo import _, api, fields, models from odoo.tools.safe_eval import safe_eval from odoo.tools.misc import UnquoteEvalContext @@ -46,7 +46,8 @@ class FetchmailServer(models.Model): # New connection for retrieving folders connection = this.connect() for folder in this.folder_ids.filtered('active'): - folder.retrieve_imap_folder(connection) + if folder.state == 'done': + folder.retrieve_imap_folder(connection) connection.close() except Exception: _logger.error(_( @@ -58,21 +59,6 @@ class FetchmailServer(models.Model): connection.logout() return True - @api.multi - def button_confirm_login(self): - retval = super(FetchmailServer, self).button_confirm_login() - for this in self: - this.write({'state': 'draft'}) - connection = this.connect() - connection.select() - for folder in this.folder_ids.filtered('active'): - if connection.select(folder.path)[0] != 'OK': - raise exceptions.ValidationError( - _('Mailbox %s not found!') % folder.path) - connection.close() - this.write({'state': 'done'}) - return retval - def fields_view_get( self, view_id=None, view_type='form', toolbar=False, submenu=False): diff --git a/fetchmail_attach_from_folder/models/fetchmail_server_folder.py b/fetchmail_attach_from_folder/models/fetchmail_server_folder.py index 979fa1d2f..115feb211 100644 --- a/fetchmail_attach_from_folder/models/fetchmail_server_folder.py +++ b/fetchmail_attach_from_folder/models/fetchmail_server_folder.py @@ -5,7 +5,7 @@ import base64 import logging from odoo import _, api, models, fields -from odoo.exceptions import UserError +from odoo.exceptions import UserError, ValidationError from .. import match_algorithm @@ -35,11 +35,21 @@ class FetchmailServerFolder(models.Model): algorithms.sort() return algorithms + server_id = fields.Many2one('fetchmail.server', 'Server') sequence = fields.Integer('Sequence') + state = fields.Selection([ + ('draft', 'Not Confirmed'), + ('done', 'Confirmed')], + string='Status', + readonly=True, + required=True, + copy=False, + default='draft') path = fields.Char( 'Path', - help="The path to your mail folder. Typically would be something like " - "'INBOX.myfolder'", required=True) + required=True, + help="The path to your mail folder." + " Typically would be something like 'INBOX.myfolder'") model_id = fields.Many2one( 'ir.model', 'Model', required=True, help='The model to attach emails to') @@ -62,7 +72,6 @@ class FetchmailServerFolder(models.Model): 'Field (email)', help='The field in the email used for matching. Typically ' "this is 'to' or 'from'") - server_id = fields.Many2one('fetchmail.server', 'Server') delete_matching = fields.Boolean( 'Delete matches', help='Delete matched emails from server') @@ -89,6 +98,20 @@ class FetchmailServerFolder(models.Model): def get_algorithm(self): return self._get_match_algorithms()[self.match_algorithm]() + @api.multi + def button_confirm_folder(self): + for this in self: + this.write({'state': 'draft'}) + if not this.active: + continue + connection = this.server_id.connect() + connection.select() + if connection.select(this.path)[0] != 'OK': + raise ValidationError( + _('Invalid folder %s!') % this.path) + connection.close() + this.write({'state': 'done'}) + @api.multi def button_attach_mail_manually(self): self.ensure_one() diff --git a/fetchmail_attach_from_folder/views/fetchmail_server.xml b/fetchmail_attach_from_folder/views/fetchmail_server.xml index 3a1b2e010..7bc8c0a6a 100644 --- a/fetchmail_attach_from_folder/views/fetchmail_server.xml +++ b/fetchmail_attach_from_folder/views/fetchmail_server.xml @@ -33,12 +33,18 @@
+
+