# -*- coding: utf-8 -*- from odoo import api, models from odoo.addons.l10n_it_edi.tools.remove_signature import remove_signature from lxml import etree import logging import re _logger = logging.getLogger(__name__) FATTURAPA_FILENAME_RE = "[A-Z]{2}[A-Za-z0-9]{2,28}_[A-Za-z0-9]{0,5}.((?i:xml.p7m|xml))" class IrAttachment(models.Model): _inherit = 'ir.attachment' def _decode_edi_l10n_it_edi(self, name, content): """ Decodes a into a list of one dictionary representing an attachment. :returns: A list with a dictionary. """ if not (decoded_content := remove_signature(content)): return [] parser = etree.XMLParser(recover=True, resolve_entities=False) try: xml_tree = etree.fromstring(decoded_content, parser) except etree.ParseError as e: _logger.exception("Error when converting the xml content to etree: %s", e) return [] return [{ 'filename': name, 'content': content, 'attachment': self, 'xml_tree': xml_move_tree, 'type': 'l10n_it_edi', 'sort_weight': 11, } for xml_move_tree in xml_tree.xpath('//FatturaElettronicaBody')] def _is_l10n_it_edi_import_file(self): is_xml = ( self.name.endswith('.xml') or self.mimetype.endswith('/xml') or 'text/plain' in self.mimetype and self.raw and self.raw.startswith(b'