Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/webapp/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ class ModelTable(Base):
deleted: Mapped[bool] = mapped_column(nullable=True)
# If true, the model has been approved and is ready for use.
valid: Mapped[bool] = mapped_column(nullable=True)
archived: Mapped[int] = mapped_column(Integer, default=0)
# The time the deletion request was set.
deleted_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=True
Expand Down
41 changes: 41 additions & 0 deletions src/webapp/routers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,47 @@ def delete_model(
}


@router.patch("/{inst_id}/models/{model_name}/archive")
def archive_model(
inst_id: str,
model_name: str,
current_user: Annotated[BaseUser, Depends(get_current_active_user)],
sql_session: Annotated[Session, Depends(get_session)],
) -> Any:
"""Archive a model by setting ``archived`` from 0 to 1."""
transformed_model_name = str(decode_url_piece(model_name)).strip()
has_access_to_inst_or_err(inst_id, current_user)

local_session.set(sql_session)
sess = local_session.get()

model = sess.execute(
select(ModelTable).where(
ModelTable.name == transformed_model_name,
ModelTable.inst_id == str_to_uuid(inst_id),
)
).scalar_one_or_none()
if model is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Model not found."
)
if model.archived:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Model is already archived.",
)

model.archived = 1
sess.commit()

return {
"inst_id": inst_id,
"model_name": transformed_model_name,
"archived": 1,
"status": "Model archived",
}


@router.get("/{inst_id}/models/{model_name}/runs", response_model=list[RunInfo])
def read_inst_model_outputs(
inst_id: str,
Expand Down
19 changes: 19 additions & 0 deletions src/webapp/routers/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ def test_read_inst_model(client: TestClient) -> None:
assert same_model_orderless(response_model, expected_model)


def test_archive_model(client: TestClient, session: sqlalchemy.orm.Session) -> None:
"""Test PATCH /institutions/{inst_id}/models/{model_name}/archive."""
base = "/institutions/" + uuid_to_str(USER_VALID_INST_UUID) + "/models/"
assert client.patch(base + "missing_model/archive").status_code == 404

response = client.patch(base + "sample_model_for_school_1/archive")
assert response.status_code == 200
assert response.json() == {
"inst_id": uuid_to_str(USER_VALID_INST_UUID),
"model_name": "sample_model_for_school_1",
"archived": 1,
"status": "Model archived",
}
model_row = session.get(ModelTable, SAMPLE_UUID)
assert model_row is not None
assert model_row.archived == 1
assert client.patch(base + "sample_model_for_school_1/archive").status_code == 409


def test_read_inst_model_outputs(client: TestClient) -> None:
"""Test GET /institutions/345/models/10/output."""
MOCK_STORAGE.list_blobs_in_folder.return_value = []
Expand Down
Loading