Skip to content

Commit

Permalink
feat: change the url for the update endpoint.
Browse files Browse the repository at this point in the history
Added new linkDtoForUpdate object to hide area1 and area2 from the client
  • Loading branch information
TheoPascoli committed Nov 28, 2024
1 parent 729ecee commit c9077e9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 34 deletions.
12 changes: 7 additions & 5 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, LinkInternal
from antarest.study.business.model.link_model import LinkDTO, LinkDtoForUpdate, 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,17 +79,19 @@ def create_link(self, study: Study, link_creation_dto: LinkDTO) -> LinkDTO:

return link_creation_dto

def update_link(self, study: RawStudy, link_dto: LinkDTO) -> LinkDTO:
def update_link(self, study: RawStudy, area_from: str, area_to: str, link_update_dto: LinkDtoForUpdate) -> LinkDTO:
link_dto = LinkDTO(area1=area_from, area2=area_to, **link_update_dto.model_dump())

link = link_dto.to_internal(StudyVersion.parse(study.version))
file_study = self.storage_service.get_storage(study).get_raw(study)

self.get_link_if_exists(file_study, link)

command = UpdateLink(
area1=link.area1,
area2=link.area2,
area1=area_from,
area2=area_to,
parameters=link.model_dump(
include=link_dto.model_fields_set, exclude={"area1", "area2"}, exclude_none=True
include=link_update_dto.model_fields_set, exclude={"area1", "area2"}, exclude_none=True
),
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
Expand Down
12 changes: 11 additions & 1 deletion antarest/study/business/model/link_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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 Down Expand Up @@ -51,7 +52,11 @@ def validate_areas(self) -> t.Self:


class LinkDTO(Area):
model_config = ConfigDict(alias_generator=to_camel_case, populate_by_name=True, extra="forbid")
model_config = ConfigDict(
alias_generator=to_camel_case,
populate_by_name=True,
extra="forbid",
)

hurdles_cost: bool = False
loop_flow: bool = False
Expand Down Expand Up @@ -81,6 +86,11 @@ 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
8 changes: 5 additions & 3 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
from antarest.study.business.model.link_model import LinkDTO, LinkDtoForUpdate
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 @@ -1908,13 +1908,15 @@ def create_link(
def update_link(
self,
uuid: str,
link_update_dto: LinkDTO,
area_from: str,
area_to: str,
link_update_dto: LinkDtoForUpdate,
params: RequestParameters,
) -> LinkDTO:
study = self.get_study(uuid)
assert_permission(params.user, study, StudyPermissionType.WRITE)
self._assert_study_unarchived(study)
updated_link = self.links_manager.update_link(study, link_update_dto)
updated_link = self.links_manager.update_link(study, area_from, area_to, link_update_dto)
self.event_bus.push(
Event(
type=EventType.STUDY_DATA_EDITED,
Expand Down
12 changes: 7 additions & 5 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
from antarest.study.business.model.link_model import LinkDTO, LinkDtoForUpdate
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 @@ -198,22 +198,24 @@ def create_link(
return study_service.create_link(uuid, link_creation_info, params)

@bp.put(
"/studies/{uuid}/links",
"/studies/{uuid}/links/{area_from}/{area_to}",
tags=[APITag.study_data],
summary="Update a link",
response_model=LinkDTO,
)
def update_link(
uuid: str,
link_update_info: LinkDTO,
area_from: str,
area_to: str,
link_update_dto: LinkDtoForUpdate,
current_user: JWTUser = Depends(auth.get_current_user),
) -> t.Any:
logger.info(
f"Updating link {link_update_info.area1} -> {link_update_info.area2} for study {uuid}",
f"Updating link {area_from} -> {area_to} for study {uuid}",
extra={"user": current_user.id},
)
params = RequestParameters(user=current_user)
return study_service.update_link(uuid, link_update_info, params)
return study_service.update_link(uuid, area_from, area_to, link_update_dto, params)

@bp.put(
"/studies/{uuid}/areas/{area_id}/ui",
Expand Down
28 changes: 8 additions & 20 deletions tests/integration/study_data_blueprint/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def test_link_update(self, client: TestClient, user_access_token: str, study_typ
area2_id = preparer.create_area(study_id, name="Area 2")["id"]
client.post(f"/v1/studies/{study_id}/links", json={"area1": area1_id, "area2": area2_id, "hurdlesCost": True})
res = client.put(
f"/v1/studies/{study_id}/links",
json={"area1": area1_id, "area2": area2_id, "colorr": 150},
f"/v1/studies/{study_id}/links/{area1_id}/{area2_id}",
json={"colorr": 150},
)

assert res.status_code == 200
Expand All @@ -63,12 +63,8 @@ def test_link_update(self, client: TestClient, user_access_token: str, study_typ
# Test update link same area

res = client.put(
f"/v1/studies/{study_id}/links",
json={
"area1": area1_id,
"area2": area1_id,
"hurdlesCost": False,
},
f"/v1/studies/{study_id}/links/{area1_id}/{area1_id}",
json={"hurdlesCost": False},
)
assert res.status_code == 422
expected = {
Expand All @@ -80,12 +76,8 @@ def test_link_update(self, client: TestClient, user_access_token: str, study_typ
# Test update link with non existing area

res = client.put(
f"/v1/studies/{study_id}/links",
json={
"area1": area1_id,
"area2": "id_do_not_exist",
"hurdlesCost": False,
},
f"/v1/studies/{study_id}/links/{area1_id}/id_do_not_exist",
json={"hurdlesCost": False},
)
assert res.status_code == 404
expected = {
Expand Down Expand Up @@ -117,12 +109,8 @@ def test_link_update(self, client: TestClient, user_access_token: str, study_typ

if study_type == "variant":
res = client.put(
f"/v1/studies/{study_id}/links",
json={
"area1": area1_id,
"area2": area2_id,
"hurdlesCost": False,
},
f"/v1/studies/{study_id}/links/{area1_id}/{area2_id}",
json={"hurdlesCost": False},
)
assert res.status_code == 200

Expand Down

0 comments on commit c9077e9

Please sign in to comment.