Skip to content

Commit

Permalink
[IMP] l10n_it_fatturapa_pec: avoid POP3 in PEC fetchmail
Browse files Browse the repository at this point in the history
  • Loading branch information
odooNextev committed Nov 10, 2023
1 parent 77b1600 commit 3373803
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 46 deletions.
52 changes: 10 additions & 42 deletions l10n_it_fatturapa_pec/models/fetchmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import logging

from odoo import _, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

_logger = logging.getLogger(__name__)
MAX_POP_MESSAGES = 50


class Fetchmail(models.Model):
Expand All @@ -26,6 +26,14 @@ def _default_e_inv_notify_partner_ids(self):
default=_default_e_inv_notify_partner_ids,
)

@api.constrains("is_fatturapa_pec", "server_type")
def onchange_is_fatturapa_pec(self):
for server in self:
if server.is_fatturapa_pec and server.server_type == "pop":
raise ValidationError(
_("FatturaPA PEC incoming mail server cannot be a POP Server")
)

def fetch_mail_server_type_imap(
self, server, MailThread, error_messages, **additional_context
):
Expand Down Expand Up @@ -61,42 +69,6 @@ def fetch_mail_server_type_imap(
imap_server.close()
imap_server.logout()

def fetch_mail_server_type_pop(
self, server, MailThread, error_messages, **additional_context
):
pop_server = None
try:
while True:
pop_server = server.connect()
(num_messages, total_size) = pop_server.stat()
pop_server.list()
for num in range(1, min(MAX_POP_MESSAGES, num_messages) + 1):
(header, messages, octets) = pop_server.retr(num)
message = "\n".join(messages)
try:
MailThread.with_context(**additional_context).message_process(
server.object_id.model,
message,
save_original=server.original,
strip_attachments=(not server.attach),
)
pop_server.dele(num)
# See the comments in the IMAP part
server.last_pec_error_message = ""
except Exception as e:
server.manage_pec_failure(e, error_messages)
continue
# pylint: disable=invalid-commit
self._cr.commit()
if num_messages < MAX_POP_MESSAGES:
break
pop_server.quit()
except Exception as e:
server.manage_pec_failure(e, error_messages)
finally:
if pop_server:
pop_server.quit()

def fetch_mail(self):
for server in self:
if not server.is_fatturapa_pec:
Expand All @@ -121,10 +93,6 @@ def fetch_mail(self):
server.fetch_mail_server_type_imap(
server, MailThread, error_messages, **additional_context
)
elif server.server_type == "pop":
server.fetch_mail_server_type_pop(
server, MailThread, error_messages, **additional_context
)
if error_messages:
server.notify_or_log(error_messages)
server.pec_error_count += 1
Expand Down
4 changes: 2 additions & 2 deletions l10n_it_fatturapa_pec/tests/e_invoice_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ def _create_fetchmail_pec_server(self):
return self.env["fetchmail.server"].create(
{
"name": "Test PEC server",
"server_type": "pop",
"server_type": "imap",
"is_fatturapa_pec": True,
"server": "dummy",
"port": 110,
"port": 143,
"user": "dummy",
"password": "secret",
"state": "done",
Expand Down
4 changes: 2 additions & 2 deletions l10n_it_fatturapa_pec/tests/test_e_invoice_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ def test_process_response_INVIO_broken_XML(self):
error_mails_nbr = outbound_mail_model.search_count(error_mail_domain)
self.assertFalse(error_mails_nbr)

with mock.patch("odoo.addons.fetchmail.models.fetchmail.POP3") as mock_pop3:
instance = mock_pop3.return_value
with mock.patch("odoo.addons.fetchmail.models.fetchmail.IMAP4") as mock_imap4:
instance = mock_imap4.return_value
instance.stat.return_value = (1, 1)
instance.retr.return_value = ("", [incoming_mail], "")

Expand Down

0 comments on commit 3373803

Please sign in to comment.