From c0f4373f6fb51ed409d3dfc96cdd83568116d40c Mon Sep 17 00:00:00 2001 From: Laurent LAPORTE Date: Mon, 5 Feb 2024 23:05:34 +0100 Subject: [PATCH] fix(thermal,renewable): correct the update command --- .../business/areas/renewable_management.py | 42 ++++++++++--------- .../business/areas/thermal_management.py | 35 +++++++--------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/antarest/study/business/areas/renewable_management.py b/antarest/study/business/areas/renewable_management.py index 48f349e527..ab9a2e9802 100644 --- a/antarest/study/business/areas/renewable_management.py +++ b/antarest/study/business/areas/renewable_management.py @@ -199,7 +199,11 @@ def get_cluster(self, study: Study, area_id: str, cluster_id: str) -> RenewableC return create_renewable_output(study.version, cluster_id, cluster) def update_cluster( - self, study: Study, area_id: str, cluster_id: str, cluster_data: RenewableClusterInput + self, + study: Study, + area_id: str, + cluster_id: str, + cluster_data: RenewableClusterInput, ) -> RenewableClusterOutput: """ Updates the configuration of an existing cluster within an area in the study. @@ -225,33 +229,31 @@ def update_cluster( values = file_study.tree.get(path.split("/"), depth=1) except KeyError: raise ClusterNotFound(cluster_id) from None + else: + old_config = create_renewable_config(study_version, **values) - # merge old and new values - updated_values = { - **create_renewable_config(study_version, **values).dict(exclude={"id"}), - **cluster_data.dict(by_alias=False, exclude_none=True), - "id": cluster_id, - } - new_config = create_renewable_config(study_version, **updated_values) + # use Python values to synchronize Config and Form values + new_values = cluster_data.dict(by_alias=False, exclude_none=True) + new_config = old_config.copy(exclude={"id"}, update=new_values) new_data = json.loads(new_config.json(by_alias=True, exclude={"id"})) - data = { + # create the dict containing the new values using aliases + data: t.Dict[str, t.Any] = { field.alias: new_data[field.alias] for field_name, field in new_config.__fields__.items() - if field_name not in {"id"} - and (field_name in updated_values or getattr(new_config, field_name) != field.get_default()) + if field_name in new_values } - command = UpdateConfig( - target=path, - data=data, - command_context=self.storage_service.variant_study_service.command_factory.command_context, - ) + # create the update config commands with the modified data + command_context = self.storage_service.variant_study_service.command_factory.command_context + commands = [ + UpdateConfig(target=f"{path}/{key}", data=value, command_context=command_context) + for key, value in data.items() + ] + execute_or_add_commands(study, file_study, commands, self.storage_service) - # fixme: The `file_study` is already retrieved at the beginning of the function. - file_study = self.storage_service.get_storage(study).get_raw(study) - execute_or_add_commands(study, file_study, [command], self.storage_service) - return RenewableClusterOutput(**new_config.dict(by_alias=False)) + values = new_config.dict(by_alias=False) + return RenewableClusterOutput(**values, id=cluster_id) def delete_clusters(self, study: Study, area_id: str, cluster_ids: t.Sequence[str]) -> None: """ diff --git a/antarest/study/business/areas/thermal_management.py b/antarest/study/business/areas/thermal_management.py index 29d3e566d4..dfcc52a2a0 100644 --- a/antarest/study/business/areas/thermal_management.py +++ b/antarest/study/business/areas/thermal_management.py @@ -245,32 +245,27 @@ def update_cluster( old_config = create_thermal_config(study_version, **values) # Use Python values to synchronize Config and Form values - old_values = old_config.dict(exclude={"id"}) new_values = cluster_data.dict(by_alias=False, exclude_none=True) - updated = {**old_values, **new_values} - new_config = create_thermal_config(study_version, **updated, id=cluster_id) + new_config = old_config.copy(exclude={"id"}, update=new_values) new_data = json.loads(new_config.json(by_alias=True, exclude={"id"})) - # Create the dict containing the old values (excluding defaults), - # and the updated values (including defaults) - data: t.Dict[str, t.Any] = {} - for field_name, field in new_config.__fields__.items(): - if field_name in {"id"}: - continue - value = getattr(new_config, field_name) - if field_name in new_values or value != field.get_default(): - # use the JSON-converted value - data[field.alias] = new_data[field.alias] - - # create the update config command with the modified data + # create the dict containing the new values using aliases + data: t.Dict[str, t.Any] = { + field.alias: new_data[field.alias] + for field_name, field in new_config.__fields__.items() + if field_name in new_values + } + + # create the update config commands with the modified data command_context = self.storage_service.variant_study_service.command_factory.command_context - command = UpdateConfig(target=path, data=data, command_context=command_context) - # fixme: The `file_study` is already retrieved at the beginning of the function. - file_study = self.storage_service.get_storage(study).get_raw(study) - execute_or_add_commands(study, file_study, [command], self.storage_service) + commands = [ + UpdateConfig(target=f"{path}/{key}", data=value, command_context=command_context) + for key, value in data.items() + ] + execute_or_add_commands(study, file_study, commands, self.storage_service) values = new_config.dict(by_alias=False) - return ThermalClusterOutput(**values) + return ThermalClusterOutput(**values, id=cluster_id) def delete_clusters(self, study: Study, area_id: str, cluster_ids: t.Sequence[str]) -> None: """