From 841ea41c29880e92025b00aabb9dc021cb541b4e Mon Sep 17 00:00:00 2001 From: Theo Pascoli Date: Thu, 28 Nov 2024 18:07:10 +0100 Subject: [PATCH] feat: change model logic for update endpoint --- antarest/study/business/link_management.py | 4 +- antarest/study/business/model/link_model.py | 45 ++++++++----------- antarest/study/service.py | 4 +- antarest/study/web/study_data_blueprint.py | 4 +- .../study_data_blueprint/test_link.py | 3 -- 5 files changed, 24 insertions(+), 36 deletions(-) diff --git a/antarest/study/business/link_management.py b/antarest/study/business/link_management.py index 40b8d92301..7314934692 100644 --- a/antarest/study/business/link_management.py +++ b/antarest/study/business/link_management.py @@ -18,7 +18,7 @@ from antarest.core.exceptions import ConfigFileNotFound, LinkNotFound, LinkValidationError from antarest.core.model import JSON from antarest.study.business.all_optional_meta import all_optional_model, camel_case_model -from antarest.study.business.model.link_model import LinkDTO, LinkDtoForUpdate, LinkInternal +from antarest.study.business.model.link_model import LinkBaseDto, LinkDTO, LinkInternal from antarest.study.business.utils import execute_or_add_commands from antarest.study.model import RawStudy, Study from antarest.study.storage.rawstudy.model.filesystem.config.links import LinkProperties @@ -79,7 +79,7 @@ def create_link(self, study: Study, link_creation_dto: LinkDTO) -> LinkDTO: return link_creation_dto - def update_link(self, study: RawStudy, area_from: str, area_to: str, link_update_dto: LinkDtoForUpdate) -> LinkDTO: + def update_link(self, study: RawStudy, area_from: str, area_to: str, link_update_dto: LinkBaseDto) -> LinkDTO: link_dto = LinkDTO(area1=area_from, area2=area_to, **link_update_dto.model_dump()) link = link_dto.to_internal(StudyVersion.parse(study.version)) diff --git a/antarest/study/business/model/link_model.py b/antarest/study/business/model/link_model.py index a171631cba..9038270d32 100644 --- a/antarest/study/business/model/link_model.py +++ b/antarest/study/business/model/link_model.py @@ -13,7 +13,6 @@ from antares.study.version import StudyVersion from pydantic import ConfigDict, Field, model_validator -from pydantic.json_schema import SkipJsonSchema from antarest.core.exceptions import LinkValidationError from antarest.core.serialization import AntaresBaseModel @@ -37,26 +36,8 @@ ] -class Area(AntaresBaseModel): - area1: str - area2: str - - @model_validator(mode="after") - def validate_areas(self) -> t.Self: - if self.area1 == self.area2: - raise LinkValidationError(f"Cannot create a link that goes from and to the same single area: {self.area1}") - area_from, area_to = sorted([self.area1, self.area2]) - self.area1 = area_from - self.area2 = area_to - return self - - -class LinkDTO(Area): - model_config = ConfigDict( - alias_generator=to_camel_case, - populate_by_name=True, - extra="forbid", - ) +class LinkBaseDto(AntaresBaseModel): + model_config = ConfigDict(alias_generator=to_camel_case, populate_by_name=True, extra="forbid") hurdles_cost: bool = False loop_flow: bool = False @@ -69,10 +50,25 @@ class LinkDTO(Area): colorg: int = Field(default=DEFAULT_COLOR, ge=0, le=255) link_width: float = 1 link_style: LinkStyle = LinkStyle.PLAIN - filter_synthesis: t.Optional[comma_separated_enum_list] = FILTER_VALUES filter_year_by_year: t.Optional[comma_separated_enum_list] = FILTER_VALUES + +class Area(AntaresBaseModel): + area1: str + area2: str + + @model_validator(mode="after") + def validate_areas(self) -> t.Self: + if self.area1 == self.area2: + raise LinkValidationError(f"Cannot create a link that goes from and to the same single area: {self.area1}") + area_from, area_to = sorted([self.area1, self.area2]) + self.area1 = area_from + self.area2 = area_to + return self + + +class LinkDTO(Area, LinkBaseDto): def to_internal(self, version: StudyVersion) -> "LinkInternal": if version < STUDY_VERSION_8_2 and {"filter_synthesis", "filter_year_by_year"} & self.model_fields_set: raise LinkValidationError("Cannot specify a filter value for study's version earlier than v8.2") @@ -86,11 +82,6 @@ def to_internal(self, version: StudyVersion) -> "LinkInternal": return LinkInternal(**data) -class LinkDtoForUpdate(LinkDTO): - area1: SkipJsonSchema[str] = Field("a", exclude=True) - area2: SkipJsonSchema[str] = Field("b", exclude=True) - - class LinkInternal(AntaresBaseModel): model_config = ConfigDict(alias_generator=to_kebab_case, populate_by_name=True, extra="forbid") diff --git a/antarest/study/service.py b/antarest/study/service.py index ebab8bac69..f0d5074624 100644 --- a/antarest/study/service.py +++ b/antarest/study/service.py @@ -89,7 +89,7 @@ from antarest.study.business.general_management import GeneralManager from antarest.study.business.link_management import LinkManager from antarest.study.business.matrix_management import MatrixManager, MatrixManagerError -from antarest.study.business.model.link_model import LinkDTO, LinkDtoForUpdate +from antarest.study.business.model.link_model import LinkBaseDto, LinkDTO from antarest.study.business.optimization_management import OptimizationManager from antarest.study.business.playlist_management import PlaylistManager from antarest.study.business.scenario_builder_management import ScenarioBuilderManager @@ -1910,7 +1910,7 @@ def update_link( uuid: str, area_from: str, area_to: str, - link_update_dto: LinkDtoForUpdate, + link_update_dto: LinkBaseDto, params: RequestParameters, ) -> LinkDTO: study = self.get_study(uuid) diff --git a/antarest/study/web/study_data_blueprint.py b/antarest/study/web/study_data_blueprint.py index e1a9ed90e9..1ea06a82c3 100644 --- a/antarest/study/web/study_data_blueprint.py +++ b/antarest/study/web/study_data_blueprint.py @@ -68,7 +68,7 @@ ) from antarest.study.business.district_manager import DistrictCreationDTO, DistrictInfoDTO, DistrictUpdateDTO from antarest.study.business.general_management import GeneralFormFields -from antarest.study.business.model.link_model import LinkDTO, LinkDtoForUpdate +from antarest.study.business.model.link_model import LinkBaseDto, LinkDTO from antarest.study.business.optimization_management import OptimizationFormFields from antarest.study.business.playlist_management import PlaylistColumns from antarest.study.business.scenario_builder_management import Rulesets, ScenarioType @@ -207,7 +207,7 @@ def update_link( uuid: str, area_from: str, area_to: str, - link_update_dto: LinkDtoForUpdate, + link_update_dto: LinkBaseDto, current_user: JWTUser = Depends(auth.get_current_user), ) -> t.Any: logger.info( diff --git a/tests/integration/study_data_blueprint/test_link.py b/tests/integration/study_data_blueprint/test_link.py index 027aaea21a..635aedb830 100644 --- a/tests/integration/study_data_blueprint/test_link.py +++ b/tests/integration/study_data_blueprint/test_link.py @@ -15,9 +15,6 @@ from starlette.testclient import TestClient from antarest.study.storage.rawstudy.model.filesystem.config.links import TransmissionCapacity -from antarest.study.storage.variantstudy.model.command.create_link import CreateLink -from antarest.study.storage.variantstudy.model.command.icommand import ICommand -from antarest.study.storage.variantstudy.model.command_context import CommandContext from tests.integration.prepare_proxy import PreparerProxy