Skip to content

Commit

Permalink
[FIX] [l10n_it_fatturapa_import_zip] more solid import of files
Browse files Browse the repository at this point in the history
  • Loading branch information
rlucia committed Feb 14, 2024
1 parent 6f8f988 commit 3f8aaa7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions l10n_it_fatturapa_import_zip/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import base64
import tempfile
import xml.etree.ElementTree as ET
import zipfile
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -125,6 +126,19 @@ def _compute_invoices_data(self):
),
)

def is_xml_file(self, file_path):
"""Check if the file at `file_path` is an XML file."""
try:
# Attempt to parse the file as XML
ET.parse(file_path)
return True # Successfully parsed, it's an XML file
except ET.ParseError:
return False # Failed to parse, not an XML file

def has_p7m_extension(self, file_path):
"""Check if the file at `file_path` has a .p7m extension."""
return file_path.suffix == ".p7m"

def action_import(self):
self.ensure_one()
company_partner = self.env.company.partner_id
Expand All @@ -137,29 +151,33 @@ def action_import(self):
# we don't have the received date
self.env.company.in_invoice_registration_date = "inv_date"

for xml_file in tmp_dir.glob("*"):
content = xml_file.read_bytes()
attach_vals = {
"name": xml_file.name,
"datas": base64.encodebytes(content),
"attachment_import_zip_id": self.id,
}
attachment = self.env["fatturapa.attachment.in"].create(attach_vals)
if attachment.xml_supplier_id == company_partner:
attachment.unlink()
attach_vals["state"] = "validated"
attachment = self.env["fatturapa.attachment.out"].create(
attach_vals
)
wizard = (
self.env["wizard.import.fatturapa"]
.with_context(
active_ids=attachment.ids,
active_model=attachment._name,
for xml_file in tmp_dir.rglob("*"):
# Skip directories and non-XML files
if xml_file.is_file() and (
self.is_xml_file(xml_file) or self.has_p7m_extension(xml_file)
):
content = xml_file.read_bytes()
attach_vals = {
"name": xml_file.name,
"datas": base64.encodebytes(content),
"attachment_import_zip_id": self.id,
}
attachment = self.env["fatturapa.attachment.in"].create(attach_vals)
if attachment.xml_supplier_id == company_partner:
attachment.unlink()
attach_vals["state"] = "validated"
attachment = self.env["fatturapa.attachment.out"].create(
attach_vals
)
wizard = (
self.env["wizard.import.fatturapa"]
.with_context(
active_ids=attachment.ids,
active_model=attachment._name,
)
.create({})
)
.create({})
)
wizard.importFatturaPA()
wizard.importFatturaPA()
self.env.company.in_invoice_registration_date = (
original_in_invoice_registration_date
)
Expand Down
Binary file modified l10n_it_fatturapa_import_zip/tests/data/xml_import.zip
Binary file not shown.

0 comments on commit 3f8aaa7

Please sign in to comment.