Skip to content

Commit

Permalink
feat(bc): add endpoint for bc deletion and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Jan 5, 2024
1 parent dc673d8 commit 85a7856
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 75 deletions.
25 changes: 25 additions & 0 deletions antarest/study/business/binding_constraint_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pydantic import BaseModel

from antarest.core.exceptions import (
CommandApplicationError,
ConstraintAlreadyExistError,
ConstraintIdNotFoundError,
DuplicateConstraintName,
Expand Down Expand Up @@ -35,7 +36,9 @@
BindingConstraintProperties870,
CreateBindingConstraint,
)
from antarest.study.storage.variantstudy.model.command.remove_binding_constraint import RemoveBindingConstraint
from antarest.study.storage.variantstudy.model.command.update_binding_constraint import UpdateBindingConstraint
from antarest.study.storage.variantstudy.model.dbmodel import VariantStudy


class LinkInfoDTO(BaseModel):
Expand Down Expand Up @@ -227,6 +230,11 @@ def create_binding_constraint(
group=data.group,
command_context=self.storage_service.variant_study_service.command_factory.command_context,
)

# Validates the matrices. Needed when the study is a variant because we only append the command to the list
if isinstance(study, VariantStudy):
command.fill_matrices(version, True)

file_study = self.storage_service.get_storage(study).get_raw(study)
execute_or_add_commands(study, file_study, [command], self.storage_service)

Expand Down Expand Up @@ -270,6 +278,23 @@ def update_binding_constraint(
)

command = UpdateBindingConstraint(**args)
# Validates the matrices. Needed when the study is a variant because we only append the command to the list
if isinstance(study, VariantStudy):
command.fill_matrices(study_version, False)

execute_or_add_commands(study, file_study, [command], self.storage_service)

def remove_binding_constraint(self, study: Study, binding_constraint_id: str) -> None:
command = RemoveBindingConstraint(
id=binding_constraint_id,
command_context=self.storage_service.variant_study_service.command_factory.command_context,
)
file_study = self.storage_service.get_storage(study).get_raw(study)

# Needed when the study is a variant because we only append the command to the list
if isinstance(study, VariantStudy) and not self.get_binding_constraint(study, binding_constraint_id):
raise CommandApplicationError("Binding constraint not found")

execute_or_add_commands(study, file_study, [command], self.storage_service)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ def get_corresponding_matrices(self, v: Optional[Union[MatrixType, str]], old: b
# pragma: no cover
raise TypeError(repr(v))

def fill_matrices(self, version: int, create: bool) -> None:
if version < 870:
self.values = self.get_corresponding_matrices(self.values, old=True, create=create)
else:
self.less_term_matrix = self.get_corresponding_matrices(self.less_term_matrix, old=False, create=create)
self.greater_term_matrix = self.get_corresponding_matrices(
self.greater_term_matrix, old=False, create=create
)
self.equal_term_matrix = self.get_corresponding_matrices(self.equal_term_matrix, old=False, create=create)


class CreateBindingConstraint(AbstractBindingConstraintCommand):
"""
Expand All @@ -199,13 +209,7 @@ def _apply(self, study_data: FileStudy) -> CommandOutput:
binding_constraints = study_data.tree.get(["input", "bindingconstraints", "bindingconstraints"])
new_key = len(binding_constraints)
bd_id = transform_name_to_id(self.name)

if study_data.config.version < 870:
self.values = self.get_corresponding_matrices(self.values, old=True, create=True)
else:
self.less_term_matrix = self.get_corresponding_matrices(self.less_term_matrix, old=False, create=True)
self.greater_term_matrix = self.get_corresponding_matrices(self.greater_term_matrix, old=False, create=True)
self.equal_term_matrix = self.get_corresponding_matrices(self.equal_term_matrix, old=False, create=True)
self.fill_matrices(study_data.config.version, True)

return apply_binding_constraint(
study_data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ def _apply(self, study_data: FileStudy) -> CommandOutput:
message="Failed to retrieve existing binding constraint",
)

if study_data.config.version < 870:
self.values = self.get_corresponding_matrices(self.values, old=True, create=False)
else:
self.less_term_matrix = self.get_corresponding_matrices(self.less_term_matrix, old=False, create=False)
self.greater_term_matrix = self.get_corresponding_matrices(
self.greater_term_matrix, old=False, create=False
)
self.equal_term_matrix = self.get_corresponding_matrices(self.equal_term_matrix, old=False, create=False)
self.fill_matrices(study_data.config.version, False)

return apply_binding_constraint(
study_data,
Expand Down
19 changes: 18 additions & 1 deletion antarest/study/web/study_data_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,23 @@ def create_binding_constraint(
study = study_service.check_study_access(uuid, StudyPermissionType.READ, params)
return study_service.binding_constraint_manager.create_binding_constraint(study, data)

@bp.delete(
"/studies/{uuid}/bindingconstraints/{binding_constraint_id}",
tags=[APITag.study_data],
summary="Delete a binding constraint",
response_model=None,
)
def delete_binding_constraint(
uuid: str, binding_constraint_id: str, current_user: JWTUser = Depends(auth.get_current_user)
) -> None:
logger.info(
f"Deleting the binding constraint {binding_constraint_id} for study {uuid}",
extra={"user": current_user.id},
)
params = RequestParameters(user=current_user)
study = study_service.check_study_access(uuid, StudyPermissionType.WRITE, params)
return study_service.binding_constraint_manager.remove_binding_constraint(study, binding_constraint_id)

@bp.post(
"/studies/{uuid}/bindingconstraints/{binding_constraint_id}/term",
tags=[APITag.study_data],
Expand Down Expand Up @@ -919,7 +936,7 @@ def update_constraint_term(
@bp.delete(
"/studies/{uuid}/bindingconstraints/{binding_constraint_id}/term/{term_id}",
tags=[APITag.study_data],
summary="update constraint term",
summary="Delete a constraint term",
)
def remove_constraint_term(
uuid: str,
Expand Down
Loading

0 comments on commit 85a7856

Please sign in to comment.