Skip to content

Commit 4e55234

Browse files
committed
Re-enable validation after an error occurs
Re-enables validation button and hides progress bar if a the validation lambda call fails.
1 parent 5cb17db commit 4e55234

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

arho_feature_template/core/lambda_service.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class LambdaService(QObject):
1616
jsons_received = pyqtSignal(dict, dict)
1717
validation_received = pyqtSignal(dict)
18+
validation_failed = pyqtSignal()
1819
ActionAttribute = cast(QNetworkRequest.Attribute, QNetworkRequest.User + 1)
1920
ACTION_VALIDATE_PLANS = "validate_plans"
2021
ACTION_GET_PLANS = "get_plans"
@@ -58,9 +59,12 @@ def _is_api_gateway_request(self) -> bool:
5859
return bool(match)
5960

6061
def _handle_reply(self, reply: QNetworkReply):
62+
action = reply.request().attribute(LambdaService.ActionAttribute)
6163
if reply.error() != QNetworkReply.NoError:
6264
error = reply.errorString()
63-
QMessageBox.critical(None, "API Error", f"Lambda call failed: {error}")
65+
QMessageBox.critical(None, "API Virhe", f"Lambda kutsu epäonnistui: {error}")
66+
if action == self.ACTION_VALIDATE_PLANS:
67+
self.validation_failed.emit()
6468
reply.deleteLater()
6569
return
6670

@@ -72,20 +76,23 @@ def _handle_reply(self, reply: QNetworkReply):
7276
# If calling the lambda directly, the response includes status code and body
7377
if int(response_json.get("statusCode", 0)) != HTTPStatus.OK:
7478
error = response_json["body"] if "body" in response_json else response_json["errorMessage"]
75-
QMessageBox.critical(None, "API Error", f"Lambda call failed: {error}")
79+
QMessageBox.critical(None, "API Virhe", f"Lambda kutsu epäonnistui: {error}")
80+
if action == self.ACTION_VALIDATE_PLANS:
81+
self.validation_failed.emit()
7682
reply.deleteLater()
7783
return
7884
body = response_json["body"]
7985
else:
8086
body = response_json
8187

8288
except (json.JSONDecodeError, KeyError) as e:
83-
QMessageBox.critical(None, "JSON Error", f"Failed to parse response JSON: {e}")
89+
QMessageBox.critical(None, "JSON Virhe", f"Vastauksen JSON-tiedoston jäsennys epäonnistui: {e}")
90+
if action == self.ACTION_VALIDATE_PLANS:
91+
self.validation_failed.emit()
8492
return
8593
finally:
8694
reply.deleteLater()
8795

88-
action = reply.request().attribute(LambdaService.ActionAttribute)
8996
if action == self.ACTION_GET_PLANS:
9097
self._process_json_reply(body)
9198
elif action == self.ACTION_VALIDATE_PLANS:
@@ -95,7 +102,6 @@ def _process_validation_reply(self, response_json: dict):
95102
"""Processes the validation reply from the lambda and emits a signal."""
96103

97104
validation_errors = response_json.get("ryhti_responses")
98-
99105
self.validation_received.emit(validation_errors)
100106

101107
def _process_json_reply(self, response_json: dict):

arho_feature_template/gui/docks/validation_dock.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(self, parent=None):
2929

3030
self.lambda_service = LambdaService()
3131
self.lambda_service.validation_received.connect(self.list_validation_errors)
32+
self.lambda_service.validation_failed.connect(self.enable_validation)
3233
self.validate_button.clicked.connect(self.validate)
3334

3435
def validate(self):
@@ -48,15 +49,25 @@ def validate(self):
4849

4950
self.lambda_service.validate_plan(active_plan_id)
5051

52+
def enable_validation(self):
53+
"""Hide progress bar and re-enable the button"""
54+
self.progress_bar.setVisible(False)
55+
self.validate_button.setEnabled(True)
56+
self.validation_result_tree_view.expandAll()
57+
self.validation_result_tree_view.resizeColumnToContents(0)
58+
5159
def list_validation_errors(self, validation_json):
5260
"""Slot for listing validation errors and warnings."""
61+
5362
if not validation_json:
5463
iface.messageBar().pushMessage("Virhe", "Validaatio json puuttuu.", level=1)
64+
self.enable_validation()
5565
return
5666

57-
if not validation_json:
58-
# If no errors or warnings, display a message and exit
67+
# If no errors or warnings, display a message and exit
68+
if not any(validation_json.values()):
5969
iface.messageBar().pushMessage("Virhe", "Ei virheitä havaittu.", level=1)
70+
self.enable_validation()
6071
return
6172

6273
for error_data in validation_json.values():
@@ -79,8 +90,5 @@ def list_validation_errors(self, validation_json):
7990
warning.get("message", ""),
8091
)
8192

82-
# Hide progress bar and re-enable the button
83-
self.progress_bar.setVisible(False)
84-
self.validate_button.setEnabled(True)
85-
self.validation_result_tree_view.expandAll()
86-
self.validation_result_tree_view.resizeColumnToContents(0)
93+
# Always enable validation at the end
94+
self.enable_validation()

0 commit comments

Comments
 (0)