Skip to content

Commit 5a2f1de

Browse files
committed
Merge PR #4858 into 18.0
Signed-off-by pedrobaeza
2 parents 4111090 + 80bee5a commit 5a2f1de

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

l10n_es_atc/models/l10n_es_atc_report.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
import base64
55
import contextlib
6+
import logging
67
import os
78
import subprocess
89
import tempfile
910
import zipfile
11+
from email.utils import parsedate_to_datetime
1012
from io import BytesIO
1113

1214
import requests
@@ -18,6 +20,8 @@
1820

1921
from odoo.addons.l10n_es_aeat.models.spanish_states_mapping import SPANISH_STATES
2022

23+
_logger = logging.getLogger(__name__)
24+
2125
# The URL to download the file
2226
# this should be inherited in the module that uses this model
2327
# the key is the ATC model number
@@ -226,12 +230,6 @@ def _get_or_download_atc_jar(self, jar_filename, timeout=60):
226230
"""
227231
Get the ATC jar file from the database or download it from the server
228232
:param timeout: max timeout for the request"""
229-
attachment = self.env["ir.attachment"].search(
230-
[("name", "=", jar_filename)], limit=1
231-
)
232-
if attachment:
233-
return attachment
234-
# If the jar file is not present, download it from the server
235233
url = ATC_JAR_URL.get(self._aeat_number)
236234
if not url:
237235
raise UserError(
@@ -241,6 +239,37 @@ def _get_or_download_atc_jar(self, jar_filename, timeout=60):
241239
self._aeat_number,
242240
)
243241
)
242+
attachment = self.env["ir.attachment"].search(
243+
[("name", "=", jar_filename)], limit=1
244+
)
245+
if attachment:
246+
# Check if there's a newer version available using HEAD request
247+
try:
248+
response = requests.head(url, timeout=timeout)
249+
response.raise_for_status()
250+
last_modified_header = response.headers.get("last-modified")
251+
if last_modified_header:
252+
server_date = parsedate_to_datetime(last_modified_header)
253+
if server_date and server_date.replace(
254+
tzinfo=None
255+
) > attachment.create_date.replace(tzinfo=None):
256+
# Server version is newer,
257+
# delete old attachment to force re-download
258+
attachment.unlink()
259+
attachment = None
260+
261+
except Exception as error:
262+
# If we can't check for updates,
263+
# log error but continue with existing attachment
264+
_logger.debug(
265+
"Could not check for JAR updates from %s: %s",
266+
url,
267+
ustr(error),
268+
exc_info=True,
269+
)
270+
if attachment:
271+
return attachment
272+
# If no attachment exists or need to download new version
244273
try:
245274
response = requests.get(url, timeout=timeout)
246275
response.raise_for_status()

0 commit comments

Comments
 (0)