2323"""
2424
2525from copy import deepcopy
26- from datetime import date , datetime , timezone
26+ from datetime import datetime
2727import os
2828from wsgiref import headers
2929import requests
3030import yaml
3131
32+ from .utils .helper_functions import datetime_to_string
3233from .utils .data_diff import diff_yaml_dict
3334
3435from .ui_widgets .utils import get_url_status
@@ -149,6 +150,15 @@ class CustomDumper(yaml.SafeDumper):
149150 ),
150151 )
151152
153+ # make sure datetime items are not saved as strings with quotes
154+ def represent_datetime_as_timestamp (dumper , data : datetime ):
155+ value = datetime_to_string (data )
156+
157+ # emit as YAML timestamp → plain scalar, no quotes
158+ return dumper .represent_scalar ("tag:yaml.org,2002:timestamp" , value )
159+
160+ self .dumper .add_representer (datetime , represent_datetime_as_timestamp )
161+
152162 # custom assignments
153163 self .model = QStringListModel ()
154164 self .proxy = QSortFilterProxyModel ()
@@ -179,7 +189,7 @@ def on_button_clicked(self, button):
179189 else :
180190 # check #1: show diff with "Procced" and "Cancel" options
181191 diff_approved , processed_config_data = (
182- self ._diff_original_and_current_data ()
192+ self ._diff_original_and_current_data (get_yaml_output = True )
183193 )
184194 if not diff_approved :
185195 return
@@ -396,7 +406,9 @@ def _set_validate_ui_data(self) -> tuple[bool, list]:
396406 QMessageBox .warning (f"Error deserializing: { e } " )
397407 return
398408
399- def _diff_original_and_current_data (self ) -> tuple [bool , dict ]:
409+ def _diff_original_and_current_data (
410+ self , get_yaml_output = False
411+ ) -> tuple [bool , dict ]:
400412 """Before saving the file, show the diff and give an option to proceed or cancel."""
401413
402414 new_config_data = self .config_data .asdict_enum_safe (
@@ -412,6 +424,14 @@ def _diff_original_and_current_data(self) -> tuple[bool, dict]:
412424 new_config_data ,
413425 )
414426
427+ # if get_yaml_output, preserve datetime objects without string conversion.
428+ # This is needed so the yaml dumper is using representer removing quotes from datetime strings
429+ if get_yaml_output :
430+ new_config_data = self .config_data .asdict_enum_safe (
431+ self .config_data , datetime_to_str = False
432+ )
433+
434+ # if no diff detected, directly accept the changes
415435 if (
416436 len (diff_data ["added" ])
417437 + len (diff_data ["removed" ])
@@ -420,7 +440,7 @@ def _diff_original_and_current_data(self) -> tuple[bool, dict]:
420440 ):
421441 return True , new_config_data
422442
423- # add a window with the choice
443+ # if diff detected, show a window with the choice to approve the diff
424444 QgsMessageLog .logMessage (f"{ diff_data } " )
425445 dialog = ReadOnlyTextDialog (self , "Warning" , diff_data , True )
426446 result = dialog .exec_ () # returns QDialog.Accepted (1) or QDialog.Rejected (0)
0 commit comments