forked from OCA/l10n-spain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] l10n_es_aeat: use certificate module
- Loading branch information
1 parent
ee6fcfb
commit dcd4311
Showing
14 changed files
with
231 additions
and
304 deletions.
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
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,64 @@ | ||
import base64 | ||
import binascii | ||
import json | ||
import logging | ||
|
||
from odoo import SUPERUSER_ID, api | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def migrate(cr, version): | ||
"""Restores AEAT certificate data into the new schema.""" | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
migration_data = env["ir.config_parameter"].get_param( | ||
"l10n_es_aeat_certificate_migration_data" | ||
) | ||
if not migration_data: | ||
raise ValueError("No AEAT certificate migration data found") | ||
|
||
migration_data = json.loads(migration_data) | ||
|
||
for cert_id, cert_data in migration_data.items(): | ||
private_key_data = cert_data.get("private_key", "") | ||
date_start = cert_data.get("date_start") | ||
date_end = cert_data.get("date_end") | ||
public_key_path = cert_data.get("public_key", "") | ||
try: | ||
private_key_bytes = private_key_data.encode() if private_key_data else None | ||
except binascii.Error: | ||
private_key_bytes = None | ||
if public_key_path: | ||
try: | ||
with open(public_key_path, "rb") as f: | ||
public_key_content = f.read() | ||
except FileNotFoundError: | ||
_logger.info("Public key file not found") | ||
key_id = ( | ||
env["certificate.key"].create( | ||
{ | ||
"name": f"Key for AEAT Cert {cert_id}", | ||
"content": private_key_bytes or b"", | ||
} | ||
) | ||
if private_key_bytes | ||
else None | ||
) | ||
cert_id_new = env["certificate.certificate"].create( | ||
{ | ||
"name": f"AEAT Cert {cert_id}", | ||
"private_key_id": key_id.id if key_id else False, | ||
"date_start": date_start, | ||
"date_end": date_end, | ||
"content": base64.b64encode(public_key_content).decode() | ||
if public_key_content | ||
else "", | ||
} | ||
) | ||
env["l10n.es.aeat.certificate"].browse(int(cert_id)).write( | ||
{"certificate_id": cert_id_new.id} | ||
) | ||
# Clean up stored migration data | ||
env["ir.config_parameter"].sudo().set_param( | ||
"l10n_es_aeat_certificate_migration_data", "" | ||
) |
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,50 @@ | ||
import base64 | ||
import json | ||
|
||
from openupgradelib import openupgrade | ||
|
||
|
||
def _save_certificates(env): | ||
cr = env.cr | ||
cr.execute(""" | ||
SELECT id, company_id, name, state, date_start, date_end, | ||
public_key, private_key | ||
FROM l10n_es_aeat_certificate | ||
""") | ||
certificates = cr.fetchall() | ||
stored_data = {} | ||
for cert in certificates: | ||
( | ||
cert_id, | ||
company_id, | ||
name, | ||
state, | ||
date_start, | ||
date_end, | ||
public_key, | ||
private_key_path, | ||
) = cert | ||
private_key_content = "" | ||
if private_key_path: | ||
try: | ||
with open(private_key_path, "rb") as f: | ||
private_key_content = base64.b64encode(f.read()).decode() | ||
except FileNotFoundError: | ||
private_key_content = "" | ||
stored_data[str(cert_id)] = { | ||
"company_id": company_id, | ||
"name": name, | ||
"state": state, | ||
"date_start": str(date_start) if date_start else None, | ||
"date_end": str(date_end) if date_end else None, | ||
"public_key": public_key or "", | ||
"private_key": private_key_content, | ||
} | ||
env["ir.config_parameter"].set_param( | ||
"l10n_es_aeat_certificate_migration_data", json.dumps(stored_data) | ||
) | ||
|
||
|
||
@openupgrade.migrate() | ||
def migrate(env, version): | ||
_save_certificates(env) |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
# Copyright 2023-2024 Aures Tic - Jose Zambudio <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
import logging | ||
import tempfile | ||
|
||
from requests import Session | ||
|
||
|
@@ -138,12 +139,22 @@ def _connect_aeat(self, mapping_key): | |
company=self.company_id | ||
) | ||
params = self._connect_params_aeat(mapping_key) | ||
session = Session() | ||
session.cert = (public_crt, private_key) | ||
transport = Transport(session=session) | ||
history = HistoryPlugin() | ||
client = Client(wsdl=params["wsdl"], transport=transport, plugins=[history]) | ||
return self._bind_service(client, params["port_name"], params["address"]) | ||
# Create temporary files to store the certificate and key | ||
with ( | ||
tempfile.NamedTemporaryFile(delete=False, suffix=".crt") as cert_file, | ||
tempfile.NamedTemporaryFile(delete=False, suffix=".key") as key_file, | ||
): | ||
cert_file.write(public_crt) | ||
key_file.write(private_key) | ||
cert_file.flush() | ||
key_file.flush() | ||
# Set up session with certificate and key file paths | ||
session = Session() | ||
session.cert = (cert_file.name, key_file.name) # Provide file paths | ||
transport = Transport(session=session) | ||
history = HistoryPlugin() | ||
client = Client(wsdl=params["wsdl"], transport=transport, plugins=[history]) | ||
return self._bind_service(client, params["port_name"], params["address"]) | ||
|
||
def _get_aeat_country_code(self): | ||
self.ensure_one() | ||
|
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
- La configuración de exportación a BOE no se filtran ni se | ||
auto-selecciona por fechas de validez. | ||
- Las partes específicas de las Diputaciones Forales no están incluidas. | ||
- El módulo de certificate no incluye la funcionalidad de especificar la carpeta | ||
dónde se almacena el certificado, se opta por eliminar la funcionalidad en v18 | ||
- El módulo de certificate guarda el password en el certificado, esa información | ||
no está disponible antes de migrar por lo que se dejará vacía |
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
Oops, something went wrong.