diff --git a/fetchmail_attach_from_folder/models/fetchmail_server.py b/fetchmail_attach_from_folder/models/fetchmail_server.py index 77c346eea..8d20dc66f 100644 --- a/fetchmail_attach_from_folder/models/fetchmail_server.py +++ b/fetchmail_attach_from_folder/models/fetchmail_server.py @@ -70,22 +70,7 @@ class FetchmailServer(models.Model): for this in self: if not this.folders_only: super(FetchmailServer, this).fetch_mail() - try: - # New connection for retrieving folders - connection = this.connect() - for folder in this.folder_ids.filtered('active'): - if folder.state == 'done': - folder.retrieve_imap_folder(connection) - connection.close() - except Exception: - _logger.error(_( - "General failure when trying to connect to" - " %s server %s."), - this.type, this.name, exc_info=True) - finally: - if connection: - connection.logout() - return True + this.folder_ids.fetch_mail() def fields_view_get( self, view_id=None, view_type='form', diff --git a/fetchmail_attach_from_folder/models/fetchmail_server_folder.py b/fetchmail_attach_from_folder/models/fetchmail_server_folder.py index 115feb211..bc40eb10b 100644 --- a/fetchmail_attach_from_folder/models/fetchmail_server_folder.py +++ b/fetchmail_attach_from_folder/models/fetchmail_server_folder.py @@ -123,6 +123,11 @@ class FetchmailServerFolder(models.Model): 'view_type': 'form', 'view_mode': 'form'} + @api.multi + def set_draft(self): + self.write({'state': 'draft'}) + return True + @api.multi def get_msgids(self, connection, criteria): """Return imap ids of messages to process""" @@ -167,6 +172,7 @@ class FetchmailServerFolder(models.Model): msgids = self.get_msgids(connection, 'UNDELETED') match_algorithm = self.get_algorithm() for msgid in msgids[0].split(): + # We will accept exceptions for single messages try: self.env.cr.execute('savepoint apply_matching') self.apply_matching(connection, msgid, match_algorithm) @@ -177,6 +183,29 @@ class FetchmailServerFolder(models.Model): "Failed to fetch mail %s from %s", msgid, self.server_id.name) + @api.multi + def fetch_mail(self): + """Retrieve all mails for IMAP folders. + + We will use a separate connection for each folder. + """ + for this in self: + if not this.active or this.state != 'done': + continue + connection = None + try: + # New connection per folder + connection = this.server_id.connect() + this.retrieve_imap_folder(connection) + connection.close() + except Exception: + _logger.error(_( + "General failure when trying to connect to %s server %s."), + this.server_id.type, this.server_id.name, exc_info=True) + finally: + if connection: + connection.logout() + @api.multi def update_msg(self, connection, msgid, matched=True, flagged=False): """Update msg in imap folder depending on match and settings.""" diff --git a/fetchmail_attach_from_folder/views/fetchmail_server.xml b/fetchmail_attach_from_folder/views/fetchmail_server.xml index 67ebd5e5a..80620e263 100644 --- a/fetchmail_attach_from_folder/views/fetchmail_server.xml +++ b/fetchmail_attach_from_folder/views/fetchmail_server.xml @@ -31,6 +31,7 @@ +
@@ -45,6 +46,16 @@ name="button_attach_mail_manually" string="Attach mail manually" states="done"/> +