|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from importlib import resources |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from qgis.PyQt.QtWidgets import QDialog, QProgressDialog, QListView, QMessageBox |
| 7 | +from qgis.PyQt.QtCore import Qt, QStringListModel |
| 8 | +from qgis.PyQt import uic |
| 9 | + |
| 10 | +from arho_feature_template.core.lambda_service import LambdaService |
| 11 | +from arho_feature_template.utils.misc_utils import get_active_plan_id |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from qgis.PyQt.QtWidgets import QListView |
| 15 | + |
| 16 | +# Load the UI file |
| 17 | +ui_path = resources.files(__package__) / "post_plan.ui" |
| 18 | +FormClass, _ = uic.loadUiType(ui_path) |
| 19 | + |
| 20 | + |
| 21 | +class PostPlan(QDialog, FormClass): # type: ignore |
| 22 | + def __init__(self, parent=None) -> None: |
| 23 | + super().__init__(parent) |
| 24 | + self.setupUi(self) |
| 25 | + |
| 26 | + # Initialize list model and assign to the list view |
| 27 | + self.validation_list_model: QStringListModel = QStringListModel() |
| 28 | + self.validation_list_view: QListView = self.validationListView |
| 29 | + self.validation_list_view.setModel(self.validation_list_model) |
| 30 | + |
| 31 | + # Initialize buttons |
| 32 | + self.export_button = self.exportButton |
| 33 | + self.export_button.clicked.connect(self.post_plan) |
| 34 | + self.dialogButtonBox.rejected.connect(self.reject) |
| 35 | + |
| 36 | + # Setup progress dialog (initially hidden) |
| 37 | + self.progress_dialog = QProgressDialog("Processing...", None, 0, 0, self) |
| 38 | + self.progress_dialog.setWindowModality(Qt.WindowModal) |
| 39 | + self.progress_dialog.setCancelButton(None) |
| 40 | + self.progress_dialog.setMinimumDuration(0) |
| 41 | + self.progress_dialog.hide() |
| 42 | + |
| 43 | + def post_plan(self): |
| 44 | + """Handles the 'Vie kaava' button logic.""" |
| 45 | + print("Starting export...") |
| 46 | + |
| 47 | + # Disable the button to prevent multiple clicks |
| 48 | + self.export_button.setEnabled(False) |
| 49 | + self.progress_dialog.show() |
| 50 | + |
| 51 | + plan_id = get_active_plan_id() |
| 52 | + self.lambda_service = LambdaService() |
| 53 | + self.lambda_service.post_received.connect(self.handle_validation_errors) |
| 54 | + self.lambda_service.post_plan(plan_id) |
| 55 | + |
| 56 | + def handle_validation_errors(self, post_json): |
| 57 | + """Handles the Lambda response.""" |
| 58 | + if not post_json: |
| 59 | + self.progress_dialog.hide() |
| 60 | + QMessageBox.critical(self, "Virhe", "Lambda palautti tyhjän vastauksen.") |
| 61 | + self.export_button.setEnabled(True) |
| 62 | + return |
| 63 | + |
| 64 | + # Prepare errors and warnings for display |
| 65 | + new_errors = [] |
| 66 | + validation_errors = post_json.get("ryhti_responses", {}) |
| 67 | + for error_data in validation_errors.values(): |
| 68 | + if isinstance(error_data, dict): |
| 69 | + # Get the errors |
| 70 | + errors = error_data.get("errors", []) |
| 71 | + for error in errors: |
| 72 | + rule_id = error.get("ruleId", "Tuntematon sääntö") |
| 73 | + message = error.get("message", "Ei viestiä") |
| 74 | + instance = error.get("instance", "Tuntematon instance") |
| 75 | + error_message = f"Validointivirhe - Sääntö: {rule_id}, Viesti: {message}, Instance: {instance}" |
| 76 | + new_errors.append(error_message) |
| 77 | + |
| 78 | + # Get warnings |
| 79 | + warnings = error_data.get("warnings", []) |
| 80 | + new_errors.extend([f"Varoitus: {warning}" for warning in warnings]) |
| 81 | + |
| 82 | + # If no errors or warnings, display a success message |
| 83 | + if not new_errors: |
| 84 | + new_errors.append("Kaava on validi. Ei virheitä tai varoituksia havaittu.") |
| 85 | + |
| 86 | + # Update the list view with errors and warnings |
| 87 | + self.validation_list_model.setStringList(new_errors) |
| 88 | + |
| 89 | + # Hide progress dialog and re-enable the button |
| 90 | + self.progress_dialog.hide() |
| 91 | + self.export_button.setEnabled(True) |
0 commit comments