From 7797320a6115ef8707b08de6d606827b886de9e1 Mon Sep 17 00:00:00 2001
From: Lorenzo Battistini
I messaggi PEC con Message-Id %s sono stati letti ma non elaborati, non " -"sono relativi a una e-fattura.
Controllare la casella PEC %s: server %" -"s, utente %s
" +"sono relativi a una e-fattura.Controllare la casella PEC %s: server " +"%s, utente %s
" + +#. module: l10n_it_fatturapa_pec +#: code:addons/l10n_it_fatturapa_pec/models/fetchmail.py:166 +#, python-format +msgid "" +"PEC server %s has been reset. Last error message is
%s" +"strong>
" +msgstr "" #. module: l10n_it_fatturapa_pec #: selection:account.invoice,fatturapa_state:0 @@ -112,6 +120,12 @@ msgstr "Thread email" msgid "Error" msgstr "Errore" +#. module: l10n_it_fatturapa_pec +#: code:addons/l10n_it_fatturapa_pec/models/fetchmail.py:163 +#, python-format +msgid "Fetchmail PEC server [%s] reset" +msgstr "" + #. module: l10n_it_fatturapa_pec #: model:ir.model.fields,field_description:l10n_it_fatturapa_pec.field_wizard_fatturapa_send_pec__id msgid "ID" @@ -170,6 +184,11 @@ msgstr "Nessun server PEC in ingresso trovato, configurarne uno." msgid "Not delivered" msgstr "Non consegnato" +#. module: l10n_it_fatturapa_pec +#: model:ir.model.fields,field_description:l10n_it_fatturapa_pec.field_fetchmail_server__pec_error_count +msgid "PEC error count" +msgstr "" + #. module: l10n_it_fatturapa_pec #: code:addons/l10n_it_fatturapa_pec/models/mail_thread.py:121 #, python-format diff --git a/l10n_it_fatturapa_pec/i18n/l10n_it_fatturapa_pec.pot b/l10n_it_fatturapa_pec/i18n/l10n_it_fatturapa_pec.pot index 22db66f2d115..f31fa9737813 100644 --- a/l10n_it_fatturapa_pec/i18n/l10n_it_fatturapa_pec.pot +++ b/l10n_it_fatturapa_pec/i18n/l10n_it_fatturapa_pec.pot @@ -19,6 +19,12 @@ msgstr "" msgid "PEC message with Message-Id %s has been read but not processed, as not related to an e-invoice.
Please check PEC mailbox %s, at server %s, with user %s
" msgstr "" +#. module: l10n_it_fatturapa_pec +#: code:addons/l10n_it_fatturapa_pec/models/fetchmail.py:166 +#, python-format +msgid "PEC server %s has been reset. Last error message is
%s
" +msgstr "" + #. module: l10n_it_fatturapa_pec #: selection:account.invoice,fatturapa_state:0 #: selection:fatturapa.attachment.out,state:0 @@ -102,6 +108,12 @@ msgstr "" msgid "Error" msgstr "" +#. module: l10n_it_fatturapa_pec +#: code:addons/l10n_it_fatturapa_pec/models/fetchmail.py:163 +#, python-format +msgid "Fetchmail PEC server [%s] reset" +msgstr "" + #. module: l10n_it_fatturapa_pec #: model:ir.model.fields,field_description:l10n_it_fatturapa_pec.field_wizard_fatturapa_send_pec__id msgid "ID" @@ -160,6 +172,11 @@ msgstr "" msgid "Not delivered" msgstr "" +#. module: l10n_it_fatturapa_pec +#: model:ir.model.fields,field_description:l10n_it_fatturapa_pec.field_fetchmail_server__pec_error_count +msgid "PEC error count" +msgstr "" + #. module: l10n_it_fatturapa_pec #: code:addons/l10n_it_fatturapa_pec/models/mail_thread.py:121 #, python-format diff --git a/l10n_it_fatturapa_pec/models/fetchmail.py b/l10n_it_fatturapa_pec/models/fetchmail.py index dcf4c8a51361..d45a4ea72a58 100644 --- a/l10n_it_fatturapa_pec/models/fetchmail.py +++ b/l10n_it_fatturapa_pec/models/fetchmail.py @@ -3,7 +3,7 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). import logging -from odoo import models, api, fields +from odoo import models, api, fields, _ _logger = logging.getLogger(__name__) MAX_POP_MESSAGES = 50 @@ -17,6 +17,7 @@ def _default_e_inv_notify_partner_ids(self): last_pec_error_message = fields.Text( "Last PEC Error Message", readonly=True) + pec_error_count = fields.Integer("PEC error count", readonly=True) e_inv_notify_partner_ids = fields.Many2many( "res.partner", string="Contacts to notify", help="Contacts to notify when PEC message can't be processed", @@ -46,6 +47,7 @@ def fetch_mail(self): additional_context['server_type'] = server.type imap_server = None pop_server = None + error_raised = False if server.type == 'imap': try: imap_server = server.connect() @@ -71,11 +73,9 @@ def fetch_mail(self): server.type, server.name, exc_info=True ) # Here is where we need to intervene. - # Setting to draft prevents new e-invoices to - # be sent via PEC - server.state = 'draft' server.last_pec_error_message = str(e) - break + error_raised = True + continue imap_server.store(num, '+FLAGS', '\\Seen') # We need to commit because message is processed: # Possible next exceptions, out of try, should not @@ -86,8 +86,8 @@ def fetch_mail(self): "General failure when trying to fetch mail from " "%s server %s.", server.type, server.name, exc_info=True) - server.state = 'draft' server.last_pec_error_message = str(e) + error_raised = True finally: if imap_server: imap_server.close() @@ -122,9 +122,9 @@ def fetch_mail(self): server.type, server.name, exc_info=True ) # See the comments in the IMAP part - server.state = 'draft' + error_raised = True server.last_pec_error_message = str(e) - break + continue self._cr.commit() if num_messages < MAX_POP_MESSAGES: break @@ -135,10 +135,48 @@ def fetch_mail(self): " server %s.", server.type, server.name, exc_info=True) # See the comments in the IMAP part - server.state = 'draft' + error_raised = True server.last_pec_error_message = str(e) finally: if pop_server: pop_server.quit() + if error_raised: + server.pec_error_count += 1 + max_retry = self.env['ir.config_parameter'].get_param( + 'fetchmail.pec.max.retry') + if server.pec_error_count > int(max_retry): + # Setting to draft prevents new e-invoices to + # be sent via PEC. + # Resetting server state only after N fails. + # So that the system can try to fetch again after + # temporary connection errors + server.state = 'draft' + server.notify_about_server_reset() + else: + server.pec_error_count = 0 server.write({'date': fields.Datetime.now()}) return True + + def notify_about_server_reset(self): + if self.e_inv_notify_partner_ids: + self.env['mail.mail'].create({ + 'subject': _( + "Fetchmail PEC server [%s] reset" + ) % self.name, + 'body_html': _( + "" + "PEC server %s has been reset. Last error message is
" + "%s
" + ) % (self.name, self.last_pec_error_message), + 'recipient_ids': [( + 6, 0, + self.e_inv_notify_partner_ids.ids + )] + }) + _logger.info( + 'Notifying partners %s about PEC server %s reset' + % (self.e_inv_notify_partner_ids.ids, self.name) + ) + else: + _logger.error( + "Can't notify anyone about PEC server %s reset" % self.name) diff --git a/l10n_it_fatturapa_pec/views/fetchmail_view.xml b/l10n_it_fatturapa_pec/views/fetchmail_view.xml index 7381f203a1c1..abc98942651d 100644 --- a/l10n_it_fatturapa_pec/views/fetchmail_view.xml +++ b/l10n_it_fatturapa_pec/views/fetchmail_view.xml @@ -9,6 +9,9 @@