dev portal ok overwrite png files
This commit is contained in:
parent
5d52f423e0
commit
0e6be1f8a6
|
|
@ -198,22 +198,26 @@ class ArchiveSynchronizer(Component):
|
||||||
|
|
||||||
def remote_fetch(self, remote_path=None, local_path=None):
|
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 remote_path: Path of where to download the file from
|
||||||
:param local_path: Path of where to save the file to
|
: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:
|
if remote_path is None:
|
||||||
raise ValueError("remote_path cannot be None")
|
raise ValueError("remote_path cannot be None")
|
||||||
if local_path is None:
|
if local_path is None:
|
||||||
raise ValueError("local_path cannot be None")
|
raise ValueError("local_path cannot be None")
|
||||||
|
if "--dev-portal" in sys.argv:
|
||||||
call_url = f"https://dev.r5portal.it/{remote_path}"
|
call_url = f"https://dev.r5portal.it/{remote_path}"
|
||||||
|
else:
|
||||||
|
call_url = f"https://r5portal.it/{remote_path}"
|
||||||
log_info_type = "Download"
|
log_info_type = "Download"
|
||||||
log_time = datetime.now()
|
log_time = datetime.now()
|
||||||
log_info = f"Attempted to download from {call_url}"
|
log_info = f"Attempted to download from {call_url}"
|
||||||
|
|
||||||
|
last_update_info = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not self.simulate:
|
if not self.simulate:
|
||||||
with requests.Session() as s:
|
with requests.Session() as s:
|
||||||
|
|
@ -223,21 +227,26 @@ class ArchiveSynchronizer(Component):
|
||||||
|
|
||||||
self.log.info(f"HTTP Status Code: {response.status_code}")
|
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:
|
if response.status_code == 404:
|
||||||
log_info += " - File not found"
|
log_info += " - File not found"
|
||||||
self.log.warning(log_info)
|
self.log.warning(log_info)
|
||||||
self.log_to_db(log_time, log_info_type, 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]:
|
elif response.status_code in [403, 401]:
|
||||||
log_info += " - Access forbidden or not logged in"
|
log_info += " - Access forbidden or not logged in"
|
||||||
self.log.warning(log_info)
|
self.log.warning(log_info)
|
||||||
self.log_to_db(log_time, log_info_type, 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:
|
elif response.status_code != 200:
|
||||||
log_info += f" - Unexpected HTTP response status: {response.status_code}"
|
log_info += f" - Unexpected HTTP response status: {response.status_code}"
|
||||||
self.log.error(log_info)
|
self.log.error(log_info)
|
||||||
self.log_to_db(log_time, log_info_type, 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)
|
os.makedirs(local_path, exist_ok=True)
|
||||||
|
|
||||||
|
|
@ -248,23 +257,23 @@ class ArchiveSynchronizer(Component):
|
||||||
log_info += f" - File downloaded successfully: {local_file_path}"
|
log_info += f" - File downloaded successfully: {local_file_path}"
|
||||||
self.log.info(log_info)
|
self.log.info(log_info)
|
||||||
self.log_to_db(log_time, log_info_type, 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:
|
except requests.ConnectionError as e:
|
||||||
log_info += f" - Connection error: {str(e)}"
|
log_info += f" - Connection error: {str(e)}"
|
||||||
self.log.error(log_info)
|
self.log.error(log_info)
|
||||||
self.log_to_db(log_time, log_info_type, 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:
|
except requests.Timeout as e:
|
||||||
log_info += f" - Timeout error: {str(e)}"
|
log_info += f" - Timeout error: {str(e)}"
|
||||||
self.log.error(log_info)
|
self.log.error(log_info)
|
||||||
self.log_to_db(log_time, log_info_type, 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:
|
except Exception as e:
|
||||||
log_info += f" - Unexpected error: {str(e)}"
|
log_info += f" - Unexpected error: {str(e)}"
|
||||||
self.log.error(log_info)
|
self.log.error(log_info)
|
||||||
self.log_to_db(log_time, log_info_type, 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):
|
def log_to_db(self, log_time, log_info_type, log_info):
|
||||||
"""Save log information to the database."""
|
"""Save log information to the database."""
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
|
||||||
@patch('os.makedirs')
|
@patch('os.makedirs')
|
||||||
@patch('builtins.open', new_callable=unittest.mock.mock_open)
|
@patch('builtins.open', new_callable=unittest.mock.mock_open)
|
||||||
def test_remote_fetch_success(self, mock_open, mock_makedirs, mock_session):
|
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"
|
local_path = "config/warning_images"
|
||||||
|
|
||||||
mock_response = Mock()
|
mock_response = Mock()
|
||||||
|
|
@ -36,7 +36,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
|
||||||
|
|
||||||
@patch('requests.Session')
|
@patch('requests.Session')
|
||||||
def test_remote_fetch_not_found(self, mock_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"
|
local_path = "config/warning_images"
|
||||||
|
|
||||||
mock_response = Mock()
|
mock_response = Mock()
|
||||||
|
|
@ -48,7 +48,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
|
||||||
|
|
||||||
@patch('requests.Session')
|
@patch('requests.Session')
|
||||||
def test_remote_fetch_forbidden(self, mock_session):
|
def test_remote_fetch_forbidden(self, mock_session):
|
||||||
remote_path = "img-025.png"
|
remote_path = "img-001.png"
|
||||||
local_path = "config/warning_images"
|
local_path = "config/warning_images"
|
||||||
|
|
||||||
mock_response = Mock()
|
mock_response = Mock()
|
||||||
|
|
@ -61,7 +61,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
|
||||||
@patch('requests.Session')
|
@patch('requests.Session')
|
||||||
@patch('requests.Session.get', side_effect=requests.ConnectionError("Connection error"))
|
@patch('requests.Session.get', side_effect=requests.ConnectionError("Connection error"))
|
||||||
def test_remote_fetch_connection_error(self, mock_get, mock_session):
|
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"
|
local_path = "config/warning_images"
|
||||||
|
|
||||||
result = self.synchronizer.remote_fetch(remote_path, local_path)
|
result = self.synchronizer.remote_fetch(remote_path, local_path)
|
||||||
|
|
@ -72,7 +72,7 @@ class TestArchiveSynchronizer(unittest.TestCase):
|
||||||
@patch('requests.Session')
|
@patch('requests.Session')
|
||||||
@patch('requests.Session.get', side_effect=requests.Timeout("Timeout error"))
|
@patch('requests.Session.get', side_effect=requests.Timeout("Timeout error"))
|
||||||
def test_remote_fetch_timeout_error(self, mock_get, mock_session):
|
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"
|
local_path = "config/warning_images"
|
||||||
|
|
||||||
result = self.synchronizer.remote_fetch(remote_path, local_path)
|
result = self.synchronizer.remote_fetch(remote_path, local_path)
|
||||||
|
|
@ -85,14 +85,14 @@ class TestArchiveSynchronizer(unittest.TestCase):
|
||||||
mock_response.json.return_value = {
|
mock_response.json.return_value = {
|
||||||
"STATUS": "OK",
|
"STATUS": "OK",
|
||||||
"ACTIONS_TO_DO": [
|
"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',
|
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)
|
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):
|
def test_parse_response_and_execute_json_error(self):
|
||||||
mock_response = Mock()
|
mock_response = Mock()
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ class Logs_Management(Widget):
|
||||||
|
|
||||||
# Set a specific column width for columns; for example, set 'Info' column
|
# Set a specific column width for columns; for example, set 'Info' column
|
||||||
for column in range(self.crud.db_tw.columnCount()):
|
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):
|
def row_filter(self, row, row_number, crud):
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@
|
||||||
</property>
|
</property>
|
||||||
<addaction name="about_a"/>
|
<addaction name="about_a"/>
|
||||||
<addaction name="download_a"/>
|
<addaction name="download_a"/>
|
||||||
|
<addaction name="update_a"/>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QMenu" name="admin_m">
|
<widget class="QMenu" name="admin_m">
|
||||||
<property name="font">
|
<property name="font">
|
||||||
|
|
@ -136,6 +137,11 @@
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
<action name="download_a">
|
<action name="download_a">
|
||||||
|
<property name="text">
|
||||||
|
<string>Storico Attivita</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="update_a">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Aggiornamento</string>
|
<string>Aggiornamento</string>
|
||||||
</property>
|
</property>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user