diff --git a/antarest/study/business/timeseries_config_management.py b/antarest/study/business/timeseries_config_management.py index 1a27d30a09..9af99968fb 100644 --- a/antarest/study/business/timeseries_config_management.py +++ b/antarest/study/business/timeseries_config_management.py @@ -10,23 +10,28 @@ # # This file is part of the Antares project. +from antarest.core.serialization import AntaresBaseModel from antarest.study.business.all_optional_meta import all_optional_model -from antarest.study.business.utils import GENERAL_DATA_PATH, FormFieldsBaseModel, execute_or_add_commands +from antarest.study.business.utils import GENERAL_DATA_PATH, execute_or_add_commands from antarest.study.model import Study from antarest.study.storage.storage_service import StudyStorageService from antarest.study.storage.variantstudy.model.command.update_config import UpdateConfig +class TSConfigFields(AntaresBaseModel, extra="forbid", validate_assignment=True, populate_by_name=True): + number: int + + @all_optional_model -class TSFormFields(FormFieldsBaseModel): - thermal: int +class TSConfigDTO(AntaresBaseModel, extra="forbid", validate_assignment=True, populate_by_name=True): + thermal: TSConfigFields class TimeSeriesConfigManager: def __init__(self, storage_service: StudyStorageService) -> None: self.storage_service = storage_service - def get_values(self, study: Study) -> TSFormFields: + def get_values(self, study: Study) -> TSConfigDTO: """ Get Time-Series generation values """ @@ -35,10 +40,10 @@ def get_values(self, study: Study) -> TSFormFields: url.extend(["general", "nbtimeseriesthermal"]) nb_ts_gen_thermal = file_study.tree.get(url) - args = {"thermal": nb_ts_gen_thermal} - return TSFormFields.model_validate(args) + args = {"thermal": TSConfigFields(number=nb_ts_gen_thermal)} + return TSConfigDTO.model_validate(args) - def set_values(self, study: Study, field_values: TSFormFields) -> None: + def set_values(self, study: Study, field_values: TSConfigDTO) -> None: """ Set Time-Series generation values """ @@ -48,7 +53,7 @@ def set_values(self, study: Study, field_values: TSFormFields) -> None: url = f"{GENERAL_DATA_PATH}/general/nbtimeseriesthermal" command = UpdateConfig( target=url, - data=field_values.thermal, + data=field_values.thermal.number, command_context=self.storage_service.variant_study_service.command_factory.command_context, study_version=file_study.config.version, ) diff --git a/antarest/study/web/study_data_blueprint.py b/antarest/study/web/study_data_blueprint.py index c13075a069..1ab2814e5b 100644 --- a/antarest/study/web/study_data_blueprint.py +++ b/antarest/study/web/study_data_blueprint.py @@ -74,7 +74,7 @@ from antarest.study.business.scenario_builder_management import Rulesets, ScenarioType from antarest.study.business.table_mode_management import TableDataDTO, TableModeType from antarest.study.business.thematic_trimming_field_infos import ThematicTrimmingFormFields -from antarest.study.business.timeseries_config_management import TSFormFields +from antarest.study.business.timeseries_config_management import TSConfigDTO from antarest.study.model import PatchArea, PatchCluster from antarest.study.service import StudyService from antarest.study.storage.rawstudy.model.filesystem.config.binding_constraint import ( @@ -968,13 +968,13 @@ def set_adequacy_patch_form_values( path="/studies/{uuid}/timeseries/config", tags=[APITag.study_data], summary="Gets the TS Generation config", - response_model=TSFormFields, + response_model=TSConfigDTO, response_model_exclude_none=True, ) def get_timeseries_form_values( uuid: str, current_user: JWTUser = Depends(auth.get_current_user), - ) -> TSFormFields: + ) -> TSConfigDTO: logger.info( msg=f"Getting Time-Series generation config for study {uuid}", extra={"user": current_user.id}, @@ -990,7 +990,7 @@ def get_timeseries_form_values( summary="Sets the TS Generation config", ) def set_ts_generation_config( - uuid: str, field_values: TSFormFields, current_user: JWTUser = Depends(auth.get_current_user) + uuid: str, field_values: TSConfigDTO, current_user: JWTUser = Depends(auth.get_current_user) ) -> None: logger.info( f"Updating Time-Series generation config for study {uuid}", diff --git a/tests/integration/study_data_blueprint/test_generate_thermal_cluster_timeseries.py b/tests/integration/study_data_blueprint/test_generate_thermal_cluster_timeseries.py index 9b0e5955d4..c489a17ae5 100644 --- a/tests/integration/study_data_blueprint/test_generate_thermal_cluster_timeseries.py +++ b/tests/integration/study_data_blueprint/test_generate_thermal_cluster_timeseries.py @@ -41,7 +41,7 @@ def test_lifecycle_nominal(self, client: TestClient, user_access_token: str) -> area2_id = preparer.create_area(study_id, name="Area 2")["id"] # Set nb timeseries thermal to 3. nb_years = 3 - res = client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": nb_years}) + res = client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": {"number": nb_years}}) assert res.status_code in {200, 201} # Create 1 cluster in area1 @@ -157,7 +157,7 @@ def test_advanced_results(self, client: TestClient, user_access_token: str) -> N preparer = PreparerProxy(client, user_access_token) study_id = preparer.create_study("foo", version=860) area_id = preparer.create_area(study_id, name="test")["id"] - res = client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": 10}) + res = client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": {"number": 10}}) cluster_id = "cluster_test" assert res.status_code in {200, 201} diff --git a/tests/integration/test_integration.py b/tests/integration/test_integration.py index 7fa1d30953..003aca1828 100644 --- a/tests/integration/test_integration.py +++ b/tests/integration/test_integration.py @@ -1225,11 +1225,11 @@ def test_area_management(client: TestClient, admin_access_token: str) -> None: res_ts_config = client.get(f"/v1/studies/{study_id}/timeseries/config") res_ts_config_json = res_ts_config.json() - assert res_ts_config_json == {"thermal": 1} - client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": 2}) + assert res_ts_config_json == {"thermal": {"number": 1}} + client.put(f"/v1/studies/{study_id}/timeseries/config", json={"thermal": {"number": 2}}) res_ts_config = client.get(f"/v1/studies/{study_id}/timeseries/config") res_ts_config_json = res_ts_config.json() - assert res_ts_config_json == {"thermal": 2} + assert res_ts_config_json == {"thermal": {"number": 2}} # Renewable form diff --git a/tests/storage/business/test_timeseries_config_manager.py b/tests/storage/business/test_timeseries_config_manager.py index 15b6355647..b404f36f4d 100644 --- a/tests/storage/business/test_timeseries_config_manager.py +++ b/tests/storage/business/test_timeseries_config_manager.py @@ -18,7 +18,7 @@ import pytest -from antarest.study.business.timeseries_config_management import TimeSeriesConfigManager, TSFormFields +from antarest.study.business.timeseries_config_management import TimeSeriesConfigManager, TSConfigDTO, TSConfigFields from antarest.study.model import RawStudy from antarest.study.storage.rawstudy.model.filesystem.config.files import build from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy @@ -53,9 +53,9 @@ def test_ts_field_values( study = RawStudy(id="test", path=str(file_study_820.config.path)) # Asserts the get method returns the right value - assert config_manager.get_values(study) == TSFormFields(thermal=1) + assert config_manager.get_values(study) == TSConfigDTO(thermal=TSConfigFields(number=1)) # Modifies the value and asserts the get takes the modification into account - new_value = TSFormFields(thermal=2) + new_value = TSConfigDTO(thermal=TSConfigFields(number=2)) config_manager.set_values(study, new_value) assert config_manager.get_values(study) == new_value