forked from OCA/l10n-italy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
l10n_it_fatturapa_pec: check possible fetch errors and track them (OC…
…A#682) While sending, check for valid fetchmail server FIX typo 'yo'
- Loading branch information
Showing
5 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
from . import account | ||
from . import fatturapa_attachment_out | ||
from . import mail_thread | ||
from . import fetchmail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2018 Lorenzo Battistini <https://github.com/eLBati> | ||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
|
||
import logging | ||
from odoo import models, api, fields | ||
|
||
_logger = logging.getLogger(__name__) | ||
MAX_POP_MESSAGES = 50 | ||
|
||
|
||
class Fetchmail(models.Model): | ||
_inherit = 'fetchmail.server' | ||
last_pec_error_message = fields.Text( | ||
"Last PEC error message", readonly=True) | ||
|
||
@api.multi | ||
def fetch_mail(self): | ||
for server in self: | ||
if not server.is_fatturapa_pec: | ||
super(Fetchmail, server).fetch_mail() | ||
else: | ||
additional_context = { | ||
'fetchmail_cron_running': True | ||
} | ||
# Setting fetchmail_cron_running to avoid to disable cron while | ||
# cron is running (otherwise it would be done by setting | ||
# server.state = 'draft', | ||
# see _update_cron method) | ||
server = server.with_context(**additional_context) | ||
MailThread = self.env['mail.thread'] | ||
_logger.info( | ||
'start checking for new e-invoices on %s server %s', | ||
server.type, server.name) | ||
additional_context['fetchmail_server_id'] = server.id | ||
additional_context['server_type'] = server.type | ||
imap_server = None | ||
pop_server = None | ||
if server.type == 'imap': | ||
try: | ||
imap_server = server.connect() | ||
imap_server.select() | ||
result, data = imap_server.search(None, '(UNSEEN)') | ||
for num in data[0].split(): | ||
result, data = imap_server.fetch(num, '(RFC822)') | ||
imap_server.store(num, '-FLAGS', '\\Seen') | ||
try: | ||
MailThread.with_context( | ||
**additional_context | ||
).message_process( | ||
server.object_id.model, data[0][1], | ||
save_original=server.original, | ||
strip_attachments=(not server.attach) | ||
) | ||
# if message is processed without exceptions | ||
server.last_pec_error_message = '' | ||
except Exception as e: | ||
_logger.info( | ||
'Failed to process mail from %s server ' | ||
'%s. Resetting server status', | ||
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 | ||
imap_server.store(num, '+FLAGS', '\\Seen') | ||
# We need to commit because message is processed: | ||
# Possible next exceptions, out of try, should not | ||
# rollback processed messages | ||
self._cr.commit() | ||
except Exception as e: | ||
_logger.info( | ||
"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) | ||
finally: | ||
if imap_server: | ||
imap_server.close() | ||
imap_server.logout() | ||
elif server.type == 'pop': | ||
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: | ||
_logger.info( | ||
'Failed to process mail from %s server' | ||
'%s. Resetting server status', | ||
server.type, server.name, exc_info=True | ||
) | ||
# See the comments in the IMAP part | ||
server.state = 'draft' | ||
server.last_pec_error_message = str(e) | ||
break | ||
self._cr.commit() | ||
if num_messages < MAX_POP_MESSAGES: | ||
break | ||
pop_server.quit() | ||
except Exception as e: | ||
_logger.info( | ||
"General failure when trying to fetch mail from %s" | ||
" server %s.", | ||
server.type, server.name, exc_info=True) | ||
# See the comments in the IMAP part | ||
server.state = 'draft' | ||
server.last_pec_error_message = str(e) | ||
finally: | ||
if pop_server: | ||
pop_server.quit() | ||
server.write({'date': fields.Datetime.now()}) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<odoo> | ||
|
||
<record id="view_email_server_form_e_invoice" model="ir.ui.view"> | ||
<field name="name">view_email_server_form_e_invoice</field> | ||
<field name="model">fetchmail.server</field> | ||
<field name="inherit_id" ref="fetchmail.view_email_server_form"/> | ||
<field name="arch" type="xml"> | ||
<notebook position="inside"> | ||
<page string="Last error message" attrs="{'invisible': [('last_pec_error_message', '=', False)]}"> | ||
<field name="last_pec_error_message"/> | ||
</page> | ||
</notebook> | ||
<xpath expr="//notebook/page[1]/group[1]/group[3]" position="attributes"> | ||
<!-- Hiding, because it would not be considered. See 'fetch_mail' override --> | ||
<attribute name="attrs">{'invisible': [('is_fatturapa_pec', '=', True)]}</attribute> | ||
</xpath> | ||
</field> | ||
</record> | ||
</odoo> |