dev portal ok overwrite png files

This commit is contained in:
edo-neo 2024-12-09 15:37:08 +01:00
parent 5d52f423e0
commit 0e6be1f8a6
4 changed files with 35 additions and 20 deletions

View File

@ -198,22 +198,26 @@ class ArchiveSynchronizer(Component):
def remote_fetch(self, remote_path=None, local_path=None):
"""
Download a single file from the server.
Download a single file from the server and retrieve the last update time and response status.
:param remote_path: Path of where to download the file from
:param local_path: Path of where to save the file to
:return: A dictionary with errors if any occur.
:return: A dictionary with errors if any occur, and last update information.
"""
if remote_path is None:
raise ValueError("remote_path cannot be None")
if local_path is None:
raise ValueError("local_path cannot be None")
call_url = f"https://dev.r5portal.it/{remote_path}"
if "--dev-portal" in sys.argv:
call_url = f"https://dev.r5portal.it/{remote_path}"
else:
call_url = f"https://r5portal.it/{remote_path}"
log_info_type = "Download"
log_time = datetime.now()
log_info = f"Attempted to download from {call_url}"
last_update_info = None
try:
if not self.simulate:
with requests.Session() as s:
@ -223,21 +227,26 @@ class ArchiveSynchronizer(Component):
self.log.info(f"HTTP Status Code: {response.status_code}")
last_update_info = {
"last_update_time": log_time.isoformat(),
"response_status": response.status_code
}
if response.status_code == 404:
log_info += " - File not found"
self.log.warning(log_info)
self.log_to_db(log_time, log_info_type, log_info)
return {"error": "File not found"}
return {"error": "File not found", "last_update_info": last_update_info}
elif response.status_code in [403, 401]:
log_info += " - Access forbidden or not logged in"
self.log.warning(log_info)
self.log_to_db(log_time, log_info_type, log_info)
return {"error": "Access forbidden or not logged in"}
return {"error": "Access forbidden or not logged in", "last_update_info": last_update_info}
elif response.status_code != 200:
log_info += f" - Unexpected HTTP response status: {response.status_code}"
self.log.error(log_info)
self.log_to_db(log_time, log_info_type, log_info)
return {"error": "Unexpected HTTP response status"}
return {"error": "Unexpected HTTP response status", "last_update_info": last_update_info}
os.makedirs(local_path, exist_ok=True)
@ -248,23 +257,23 @@ class ArchiveSynchronizer(Component):
log_info += f" - File downloaded successfully: {local_file_path}"
self.log.info(log_info)
self.log_to_db(log_time, log_info_type, log_info)
return {"downloaded_file": local_file_path}
return {"downloaded_file": local_file_path, "last_update_info": last_update_info}
except requests.ConnectionError as e:
log_info += f" - Connection error: {str(e)}"
self.log.error(log_info)
self.log_to_db(log_time, log_info_type, log_info)
return {"error": "Connection error"}
return {"error": "Connection error", "last_update_info": last_update_info}
except requests.Timeout as e:
log_info += f" - Timeout error: {str(e)}"
self.log.error(log_info)
self.log_to_db(log_time, log_info_type, log_info)
return {"error": "Timeout error"}
return {"error": "Timeout error", "last_update_info": last_update_info}
except Exception as e:
log_info += f" - Unexpected error: {str(e)}"
self.log.error(log_info)
self.log_to_db(log_time, log_info_type, log_info)
return {"error": "Unexpected error"}
return {"error": "Unexpected error", "last_update_info": last_update_info}
def log_to_db(self, log_time, log_info_type, log_info):
"""Save log information to the database."""

View File

@ -20,7 +20,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
@patch('os.makedirs')
@patch('builtins.open', new_callable=unittest.mock.mock_open)
def test_remote_fetch_success(self, mock_open, mock_makedirs, mock_session):
remote_path = "img-025.png"
remote_path = "img-001.png"
local_path = "config/warning_images"
mock_response = Mock()
@ -36,7 +36,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
@patch('requests.Session')
def test_remote_fetch_not_found(self, mock_session):
remote_path = "img-025.png"
remote_path = "img-001.png"
local_path = "config/warning_images"
mock_response = Mock()
@ -48,7 +48,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
@patch('requests.Session')
def test_remote_fetch_forbidden(self, mock_session):
remote_path = "img-025.png"
remote_path = "img-001.png"
local_path = "config/warning_images"
mock_response = Mock()
@ -61,7 +61,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
@patch('requests.Session')
@patch('requests.Session.get', side_effect=requests.ConnectionError("Connection error"))
def test_remote_fetch_connection_error(self, mock_get, mock_session):
remote_path = "img-025.png"
remote_path = "img-001.png"
local_path = "config/warning_images"
result = self.synchronizer.remote_fetch(remote_path, local_path)
@ -72,7 +72,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
@patch('requests.Session')
@patch('requests.Session.get', side_effect=requests.Timeout("Timeout error"))
def test_remote_fetch_timeout_error(self, mock_get, mock_session):
remote_path = "img-025.png"
remote_path = "img-001.png"
local_path = "config/warning_images"
result = self.synchronizer.remote_fetch(remote_path, local_path)
@ -85,14 +85,14 @@ class TestArchiveSynchronizer(unittest.TestCase):
mock_response.json.return_value = {
"STATUS": "OK",
"ACTIONS_TO_DO": [
{"remote_path": "img-025.png", "local_path": "config/warning_images"}
{"remote_path": "img-001.png", "local_path": "config/warning_images"}
]
}
with patch.object(self.synchronizer, 'remote_fetch',
return_value={"downloaded_file": "config/warning_images/img-025.png"}) as mock_rf:
return_value={"downloaded_file": "config/warning_images/img-001.png"}) as mock_rf:
self.synchronizer.parse_response_and_execute(mock_response)
mock_rf.assert_called_once_with(remote_path="img-025.png", local_path="config/warning_images")
mock_rf.assert_called_once_with(remote_path="img-001.png", local_path="config/warning_images")
def test_parse_response_and_execute_json_error(self):
mock_response = Mock()

View File

@ -51,7 +51,7 @@ class Logs_Management(Widget):
# Set a specific column width for columns; for example, set 'Info' column
for column in range(self.crud.db_tw.columnCount()):
self.crud.db_tw.setColumnWidth(column, 200) # Set width to 300 pixels
self.crud.db_tw.setColumnWidth(column, 175) # Set width to 300 pixels
def row_filter(self, row, row_number, crud):
try:

View File

@ -39,6 +39,7 @@
</property>
<addaction name="about_a"/>
<addaction name="download_a"/>
<addaction name="update_a"/>
</widget>
<widget class="QMenu" name="admin_m">
<property name="font">
@ -136,6 +137,11 @@
</property>
</action>
<action name="download_a">
<property name="text">
<string>Storico Attivita</string>
</property>
</action>
<action name="update_a">
<property name="text">
<string>Aggiornamento</string>
</property>