Skip to content

Commit

Permalink
feat: change model logic for update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPascoli committed Nov 28, 2024
1 parent c9077e9 commit 841ea41
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
4 changes: 2 additions & 2 deletions antarest/study/business/link_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
45 changes: 18 additions & 27 deletions antarest/study/business/model/link_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions antarest/study/web/study_data_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions tests/integration/study_data_blueprint/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 841ea41

Please sign in to comment.