From 0e6be1f8a63f1da016b20247b17205c1ec12b358 Mon Sep 17 00:00:00 2001 From: edo-neo Date: Mon, 9 Dec 2024 15:37:08 +0100 Subject: [PATCH] dev portal ok overwrite png files --- src/components/archive_synchronizer.py | 31 +++++++++++++++++--------- src/test/remote_fetch_test.py | 16 ++++++------- src/ui/logs_management/info.py | 2 +- src/ui/main_window/main_window.ui | 6 +++++ 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/components/archive_synchronizer.py b/src/components/archive_synchronizer.py index dc3284f..60974ca 100644 --- a/src/components/archive_synchronizer.py +++ b/src/components/archive_synchronizer.py @@ -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.""" diff --git a/src/test/remote_fetch_test.py b/src/test/remote_fetch_test.py index c9db1f3..8eb04d7 100644 --- a/src/test/remote_fetch_test.py +++ b/src/test/remote_fetch_test.py @@ -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() diff --git a/src/ui/logs_management/info.py b/src/ui/logs_management/info.py index f3b63f5..85ccf7f 100644 --- a/src/ui/logs_management/info.py +++ b/src/ui/logs_management/info.py @@ -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: diff --git a/src/ui/main_window/main_window.ui b/src/ui/main_window/main_window.ui index d67b0ee..b75df9a 100644 --- a/src/ui/main_window/main_window.ui +++ b/src/ui/main_window/main_window.ui @@ -39,6 +39,7 @@ + @@ -136,6 +137,11 @@ + + Storico Attivita + + + Aggiornamento