|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import json
|
2 | 4 |
|
3 | 5 | from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer
|
4 |
| -from qgis.PyQt.QtCore import QByteArray, QUrl |
5 |
| -from qgis.PyQt.QtNetwork import QNetworkAccessManager, QNetworkProxy, QNetworkReply, QNetworkRequest |
6 | 6 | from qgis.PyQt.QtWidgets import QDialog, QMessageBox
|
7 | 7 | from qgis.utils import iface
|
8 | 8 |
|
| 9 | +from arho_feature_template.core.lambda_service import LambdaService |
9 | 10 | from arho_feature_template.core.update_plan import LandUsePlan, update_selected_plan
|
10 | 11 | from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
|
11 | 12 | from arho_feature_template.gui.serialize_plan import SerializePlan
|
12 | 13 | from arho_feature_template.utils.db_utils import get_existing_database_connection_names
|
13 | 14 | from arho_feature_template.utils.misc_utils import (
|
14 | 15 | get_active_plan_id,
|
15 | 16 | get_layer_by_name,
|
16 |
| - get_settings, |
17 | 17 | handle_unsaved_changes,
|
18 | 18 | )
|
19 | 19 |
|
20 | 20 |
|
21 | 21 | class PlanManager:
|
22 | 22 | def __init__(self):
|
23 |
| - # Init network manager |
24 |
| - self.network_manager = QNetworkAccessManager() |
25 |
| - proxy_host, proxy_port, self.lambda_url = get_settings() |
26 |
| - |
27 |
| - # SOCKS5 Proxy |
28 |
| - proxy = QNetworkProxy() |
29 |
| - proxy.setType(QNetworkProxy.Socks5Proxy) |
30 |
| - proxy.setHostName(proxy_host) |
31 |
| - proxy.setPort(int(proxy_port)) |
32 |
| - self.network_manager.setProxy(proxy) |
33 |
| - |
| 23 | + self.lambda_service = LambdaService() |
| 24 | + self.lambda_service.jsons_received.connect(self.save_plan_jsons) |
| 25 | + self.json_plan_path = None |
| 26 | + self.json_plan_outline_path = None |
34 | 27 | self.kaava_layer = get_layer_by_name("Kaava")
|
35 | 28 |
|
36 | 29 | def add_new_plan(self):
|
@@ -124,82 +117,32 @@ def get_plan_json(self):
|
124 | 117 | """Serializes plan and plan outline to JSON"""
|
125 | 118 | dialog = SerializePlan()
|
126 | 119 | if dialog.exec_() == QDialog.Accepted:
|
127 |
| - json_plan_path = dialog.plan_path_edit.text() |
128 |
| - json_plan_outline_path = dialog.plan_outline_path_edit.text() |
| 120 | + self.json_plan_path = dialog.plan_path_edit.text() |
| 121 | + self.json_plan_outline_path = dialog.plan_outline_path_edit.text() |
129 | 122 |
|
130 | 123 | plan_id = get_active_plan_id()
|
131 |
| - |
132 | 124 | if not plan_id:
|
133 |
| - QMessageBox.critical(None, "Virhe", "Ei aktiivista kaavaa. Luo tai avaa kaava.") |
| 125 | + QMessageBox.critical(None, "Virhe", "Ei aktiivista kaavaa.") |
134 | 126 | return
|
135 | 127 |
|
136 |
| - payload = {"action": "get_plans", "plan_uuid": plan_id} |
137 |
| - payload_bytes = QByteArray(json.dumps(payload).encode("utf-8")) |
| 128 | + self.lambda_service.send_request("get_plans", plan_id) |
138 | 129 |
|
139 |
| - request = QNetworkRequest(QUrl(self.lambda_url)) |
140 |
| - request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") |
141 |
| - |
142 |
| - reply = self.network_manager.post(request, payload_bytes) |
143 |
| - |
144 |
| - # Connect the finished signal to a lambda to pass extra arguments |
145 |
| - reply.finished.connect(lambda: self._handle_response(reply, json_plan_path, json_plan_outline_path)) |
146 |
| - |
147 |
| - def _handle_response(self, reply: QNetworkReply, json_plan_path: str, json_plan_outline_path: str): |
148 |
| - """Handle the API response and process the plan and outline data.""" |
149 |
| - if reply.error() != QNetworkReply.NoError: |
150 |
| - error_string = reply.errorString() |
151 |
| - QMessageBox.critical(None, "Virhe API kutsussa", f"Virhe: {error_string}") |
| 130 | + def save_plan_jsons(self, plan_json, outline_json): |
| 131 | + """This slot saves the plan and outline JSONs to files.""" |
| 132 | + if plan_json is None or outline_json is None: |
| 133 | + QMessageBox.critical(None, "Virhe", "Kaava tai sen ulkoraja ei löytynyt.") |
152 | 134 | return
|
153 | 135 |
|
154 |
| - try: |
155 |
| - response_data = reply.readAll().data().decode("utf-8") |
156 |
| - response_json = json.loads(response_data) |
157 |
| - |
158 |
| - details = response_json.get("details") |
159 |
| - if not details: |
160 |
| - QMessageBox.warning(None, "Varoitus", "Vastauksesta puuttuu 'details' kenttä.") |
161 |
| - return |
162 |
| - |
163 |
| - # Extract plan and write it to file |
164 |
| - plan_id = get_active_plan_id() |
165 |
| - if not plan_id: |
166 |
| - QMessageBox.critical(None, "Virhe", "Ei aktiivista kaavaa.") |
167 |
| - return |
| 136 | + # Retrieve paths |
| 137 | + if self.json_plan_path is None or self.json_plan_outline_path is None: |
| 138 | + QMessageBox.critical(None, "Virhe", "Tiedostopolut eivät ole saatavilla.") |
| 139 | + return |
168 | 140 |
|
169 |
| - plan_json = details.get(plan_id) |
170 |
| - if not plan_json: |
171 |
| - QMessageBox.warning(None, "Varoitus", f"Aktiiviselle kaavalle (id: {plan_id}) ei löydy tietoja.") |
172 |
| - return |
| 141 | + # Save the JSONs |
| 142 | + with open(self.json_plan_path, "w", encoding="utf-8") as full_file: |
| 143 | + json.dump(plan_json, full_file, ensure_ascii=False, indent=2) |
173 | 144 |
|
174 |
| - with open(json_plan_path, "w", encoding="utf-8") as full_file: |
175 |
| - json.dump(plan_json, full_file, ensure_ascii=False, indent=2) |
176 |
| - |
177 |
| - # Process the geographicalArea for the outline |
178 |
| - geographical_area = plan_json.get("geographicalArea") |
179 |
| - if geographical_area: |
180 |
| - try: |
181 |
| - # Build the structured outline JSON and write to file |
182 |
| - outline_json = { |
183 |
| - "type": "Feature", |
184 |
| - "properties": {"name": "Example Polygon"}, |
185 |
| - "srid": geographical_area.get("srid"), |
186 |
| - "geometry": geographical_area.get("geometry"), |
187 |
| - } |
188 |
| - |
189 |
| - with open(json_plan_outline_path, "w", encoding="utf-8") as outline_file: |
190 |
| - json.dump(outline_json, outline_file, ensure_ascii=False, indent=2) |
191 |
| - |
192 |
| - QMessageBox.information(None, "Success", "Kaava ja sen ulkorja tallennettu onnistuneesti.") |
193 |
| - except KeyError as e: |
194 |
| - QMessageBox.critical( |
195 |
| - None, |
196 |
| - "Virhe", |
197 |
| - f"'geographicalArea' ei sisällä vaadittuja tietoja: {e}", |
198 |
| - ) |
199 |
| - else: |
200 |
| - QMessageBox.warning(None, "Varoitus", "Kenttä 'geographicalArea' puuttuu kaavan tiedoista.") |
201 |
| - except json.JSONDecodeError as e: |
202 |
| - QMessageBox.critical(None, "Virhe", f"Vastaus JSON:in purkaminen epäonnistui: {e}") |
| 145 | + with open(self.json_plan_outline_path, "w", encoding="utf-8") as outline_file: |
| 146 | + json.dump(outline_json, outline_file, ensure_ascii=False, indent=2) |
203 | 147 |
|
204 |
| - # Clean up the reply |
205 |
| - reply.deleteLater() |
| 148 | + QMessageBox.information(None, "Tallennus onnistui", "Kaava ja sen ulkoraja tallennettu onnistuneesti.") |
0 commit comments