1+ # Copyright 2022 Simone Rubino - TAKOBI
2+ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
14import base64
25import binascii
36import logging
69
710import lxml .etree as ET
811
9- from odoo import fields , models
12+ from odoo import api , fields , models
1013from odoo .exceptions import UserError
11- from odoo .modules import get_resource_path
14+ from odoo .modules import get_module_resource
1215from odoo .tools .translate import _
1316
1417_logger = logging .getLogger (__name__ )
@@ -28,17 +31,36 @@ def is_base64(s):
2831 return re_base64 .match (s )
2932
3033
31- class Attachment (models .Model ):
32- _inherit = "ir.attachment"
33-
34+ class FatturaPAAttachment (models .AbstractModel ):
35+ _name = "fatturapa.attachment"
36+ _description = "SdI file"
37+ _inherits = {
38+ "ir.attachment" : "ir_attachment_id" ,
39+ }
40+ _inherit = [
41+ "mail.thread" ,
42+ ]
43+ _order = "id desc"
44+
45+ ir_attachment_id = fields .Many2one (
46+ comodel_name = "ir.attachment" ,
47+ string = "Attachment" ,
48+ required = True ,
49+ ondelete = "cascade" ,
50+ )
51+ att_name = fields .Char (
52+ string = "SdI file name" ,
53+ related = "ir_attachment_id.name" ,
54+ store = True ,
55+ )
3456 ftpa_preview_link = fields .Char (
3557 "Preview link" , readonly = True , compute = "_compute_ftpa_preview_link"
3658 )
3759
3860 def _compute_ftpa_preview_link (self ):
3961 for att in self :
4062 att .ftpa_preview_link = (
41- att .get_base_url () + "/fatturapa/preview/%s" % att .id
63+ att .get_base_url () + "/fatturapa/preview/%s" % att .ir_attachment_id . id
4264 )
4365
4466 @staticmethod
@@ -50,6 +72,7 @@ def ftpa_preview(self):
5072 "target" : "new" ,
5173 }
5274
75+ @api .model
5376 def remove_xades_sign (self , xml ):
5477 # Recovering parser is needed for files where strings like
5578 # xmlns:ds="http://www.w3.org/2000/09/xmldsig#""
@@ -65,6 +88,7 @@ def remove_xades_sign(self, xml):
6588 break
6689 return ET .tostring (root )
6790
91+ @api .model
6892 def strip_xml_content (self , xml ):
6993 recovering_parser = ET .XMLParser (recover = True )
7094 root = ET .XML (xml , parser = recovering_parser )
@@ -75,14 +99,18 @@ def extract_cades(data):
7599 info = cms .ContentInfo .load (data )
76100 return info ["content" ]["encap_content_info" ]["content" ].native
77101
102+ @api .model
78103 def cleanup_xml (self , xml_string ):
79104 xml_string = self .remove_xades_sign (xml_string )
80105 xml_string = self .strip_xml_content (xml_string )
81106 return xml_string
82107
83- def get_xml_string (self ):
108+ def get_xml_string (self , attachment = None ):
109+ if not attachment :
110+ self .ensure_one ()
111+ attachment = self .ir_attachment_id
84112 try :
85- data = base64 .b64decode (self .datas )
113+ data = base64 .b64decode (attachment .datas )
86114 except binascii .Error as e :
87115 raise UserError (_ ("Corrupted attachment %s." ) % e .args )
88116
@@ -112,14 +140,14 @@ def get_xml_string(self):
112140 except AttributeError as e :
113141 raise UserError (_ ("Invalid xml %s." ) % e .args )
114142
115- def get_fattura_elettronica_preview ( self ):
116- xsl_path = get_resource_path (
117- "l10n_it_fatturapa" ,
118- "data" ,
119- self . env . company .fatturapa_preview_style ,
143+ @ api . model
144+ def get_fattura_elettronica_preview ( self , attachment ):
145+ company = self . env . user . company_id
146+ xsl_path = get_module_resource (
147+ "l10n_it_fatturapa" , "data" , company .fatturapa_preview_style
120148 )
121149 xslt = ET .parse (xsl_path )
122- xml_string = self .get_xml_string ()
150+ xml_string = self .get_xml_string (attachment )
123151 xml_file = BytesIO (xml_string )
124152 recovering_parser = ET .XMLParser (recover = True )
125153 dom = ET .parse (xml_file , parser = recovering_parser )
0 commit comments