Skip to content

Commit 602cf2a

Browse files
centralize stringifying datetime strings for Diff and Save
1 parent 9bd10af commit 602cf2a

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

pygeoapi_config_dialog.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,6 @@ class CustomDumper(yaml.SafeDumper):
149149
),
150150
)
151151

152-
def represent_datetime_as_timestamp(dumper, data: datetime):
153-
value = self.config_data.datetime_to_string(data)
154-
155-
# emit as YAML timestamp → plain scalar, no quotes
156-
return dumper.represent_scalar("tag:yaml.org,2002:timestamp", value)
157-
158-
self.dumper.add_representer(datetime, represent_datetime_as_timestamp)
159-
160152
# custom assignments
161153
self.model = QStringListModel()
162154
self.proxy = QSortFilterProxyModel()
@@ -177,25 +169,31 @@ def on_button_clicked(self, button):
177169

178170
if self.serverRadio.isChecked():
179171
# check #1: show diff with "Procced" and "Cancel" options
180-
if not self._diff_original_and_current_data():
172+
diff_approved, processed_config_data = (
173+
self._diff_original_and_current_data()
174+
)
175+
if not diff_approved:
181176
return
182177

183-
self.server_config(save=True)
178+
self.server_config(data_to_push=processed_config_data)
184179
else:
185180
# check #1: show diff with "Procced" and "Cancel" options
186-
if not self._diff_original_and_current_data():
181+
diff_approved, processed_config_data = (
182+
self._diff_original_and_current_data()
183+
)
184+
if not diff_approved:
187185
return
188186

189187
file_path, _ = QFileDialog.getSaveFileName(
190188
self, "Save File", "", "YAML Files (*.yml);;All Files (*)"
191189
)
192190
# check #2: valid file path
193191
if file_path:
194-
self.save_to_file(file_path)
192+
self.save_to_file(processed_config_data, file_path)
195193

196194
elif button == self.buttonBox.button(QDialogButtonBox.Open):
197195
if self.serverRadio.isChecked():
198-
self.server_config(save=False)
196+
self.server_config(data_to_push=None)
199197
else:
200198
file_name, _ = QFileDialog.getOpenFileName(
201199
self, "Open File", "", "YAML Files (*.yml);;All Files (*)"
@@ -206,33 +204,29 @@ def on_button_clicked(self, button):
206204
self.reject()
207205
return
208206

209-
def server_config(self, save):
207+
def server_config(self, data_to_push: dict | None = None):
210208

211209
dialog = ServerConfigDialog(self)
212210

213211
if dialog.exec_():
214212
url = dialog.get_server_url()
215-
if save:
216-
self.push_to_server(url)
213+
if data_to_push is not None:
214+
self.push_to_server(url, data_to_push)
217215
else:
218216
self.pull_from_server(url)
219217

220-
def push_to_server(self, url):
218+
def push_to_server(self, url, data_to_push: dict):
221219

222220
QMessageBox.information(
223221
self,
224222
"Information",
225223
f"Pushing configuration to: {url}",
226224
)
227225

228-
processed_config_dict = self.config_data.asdict_enum_safe(
229-
self.config_data, datetime_to_str=True
230-
)
231-
232226
# TODO: support authentication through the QT framework
233227
try:
234228
# Send the PUT request to Admin API
235-
response = requests.put(url, headers=headers, json=processed_config_dict)
229+
response = requests.put(url, headers=headers, json=data_to_push)
236230
response.raise_for_status()
237231

238232
QgsMessageLog.logMessage(f"Success! Status Code: {response.status_code}")
@@ -248,7 +242,7 @@ def push_to_server(self, url):
248242
QMessageBox.critical(
249243
self,
250244
"Error",
251-
f"An error occurred pulling the configuration from the server: {e}",
245+
f"An error occurred pushing the configuration to the server: {e}",
252246
)
253247

254248
def pull_from_server(self, url):
@@ -287,14 +281,14 @@ def pull_from_server(self, url):
287281
f"An error occurred pulling the configuration from the server: {e}",
288282
)
289283

290-
def save_to_file(self, file_path):
284+
def save_to_file(self, new_config_data: dict, file_path: str):
291285

292286
if file_path:
293287
QApplication.setOverrideCursor(Qt.WaitCursor)
294288
try:
295289
with open(file_path, "w", encoding="utf-8") as file:
296290
yaml.dump(
297-
self.config_data.asdict_enum_safe(self.config_data),
291+
new_config_data,
298292
file,
299293
Dumper=self.dumper,
300294
default_flow_style=False,
@@ -402,14 +396,20 @@ def _set_validate_ui_data(self) -> tuple[bool, list]:
402396
QMessageBox.warning(f"Error deserializing: {e}")
403397
return
404398

405-
def _diff_original_and_current_data(self) -> tuple[bool, list]:
399+
def _diff_original_and_current_data(self) -> tuple[bool, dict]:
406400
"""Before saving the file, show the diff and give an option to proceed or cancel."""
401+
402+
new_config_data = self.config_data.asdict_enum_safe(
403+
self.config_data, datetime_to_str=True
404+
)
405+
406+
# if created from skratch, no original data to compare to
407407
if not self.yaml_original_data:
408-
return True
408+
return True, new_config_data
409409

410410
diff_data = diff_yaml_dict(
411411
self.yaml_original_data,
412-
self.config_data.asdict_enum_safe(self.config_data),
412+
new_config_data,
413413
)
414414

415415
if (
@@ -418,17 +418,17 @@ def _diff_original_and_current_data(self) -> tuple[bool, list]:
418418
+ len(diff_data["changed"])
419419
== 0
420420
):
421-
return True
421+
return True, new_config_data
422422

423423
# add a window with the choice
424424
QgsMessageLog.logMessage(f"{diff_data}")
425425
dialog = ReadOnlyTextDialog(self, "Warning", diff_data, True)
426426
result = dialog.exec_() # returns QDialog.Accepted (1) or QDialog.Rejected (0)
427427

428428
if result == QDialog.Accepted:
429-
return True
429+
return True, new_config_data
430430
else:
431-
return False
431+
return False, None
432432

433433
def open_templates_path_dialog(self):
434434
"""Defining Server.templates.path path, called from .ui file."""

tests/test_yaml_save.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ def test_json_schema_on_open_save(qtbot, sample_yaml: str):
4141
dialog.open_file(sample_yaml) # now dialog.config_data has the data stored
4242

4343
# Save YAML
44+
processed_config_data = dialog.config_data.asdict_enum_safe(
45+
dialog.config_data, datetime_to_str=True
46+
)
4447
abs_new_yaml_path = sample_yaml.with_name(f"saved_{sample_yaml.name}")
45-
dialog.save_to_file(abs_new_yaml_path)
48+
dialog.save_to_file(processed_config_data, abs_new_yaml_path)
4649

4750
result = subprocess.run(
4851
[
@@ -103,8 +106,11 @@ def test_open_file_validate_ui_data_save_file(qtbot, sample_yaml: str):
103106
yaml1_missing_props = deepcopy(dialog.config_data.all_missing_props)
104107

105108
# Save YAML - EVEN THOUGH some mandatory fields might be missing and recorded as empty strings/lists
109+
processed_config_data = dialog.config_data.asdict_enum_safe(
110+
dialog.config_data, datetime_to_str=True
111+
)
106112
abs_new_yaml_path = sample_yaml.with_name(f"saved_updated_{sample_yaml.name}")
107-
dialog.save_to_file(abs_new_yaml_path)
113+
dialog.save_to_file(processed_config_data, abs_new_yaml_path)
108114

109115
# open the new file
110116
dialog.open_file(abs_new_yaml_path) # now dialog.config_data has the data stored

0 commit comments

Comments
 (0)