diff --git a/darwin/future/meta/objects/item.py b/darwin/future/meta/objects/item.py index 33422e13c..20d954bbd 100644 --- a/darwin/future/meta/objects/item.py +++ b/darwin/future/meta/objects/item.py @@ -5,6 +5,7 @@ from darwin.future.core.items.delete_items import delete_list_of_items from darwin.future.core.items.move_items_to_folder import move_list_of_items_to_folder +from darwin.future.core.items.restore_items import restore_list_of_items from darwin.future.data_objects.item import ItemCore, ItemLayout, ItemSlot from darwin.future.meta.objects.base import MetaBase @@ -66,6 +67,18 @@ def move_to_folder(self, path: str) -> None: filters = {"item_ids": [str(self.id)]} move_list_of_items_to_folder(self.client, team_slug, dataset_id, path, filters) + def restore(self) -> None: + team_slug, dataset_id = ( + self.meta_params["team_slug"], + self.meta_params["dataset_id"] + if "dataset_id" in self.meta_params + else self.meta_params["dataset_ids"], + ) + assert isinstance(team_slug, str) + dataset_id = cast(Union[int, List[int]], dataset_id) + filters = {"item_ids": [str(self.id)]} + restore_list_of_items(self.client, team_slug, dataset_id, filters) + @property def name(self) -> str: return self._element.name diff --git a/darwin/future/meta/queries/item.py b/darwin/future/meta/queries/item.py index e2f120d9e..356278cab 100644 --- a/darwin/future/meta/queries/item.py +++ b/darwin/future/meta/queries/item.py @@ -4,6 +4,7 @@ from darwin.future.core.items.delete_items import delete_list_of_items from darwin.future.core.items.get import list_items from darwin.future.core.items.move_items_to_folder import move_list_of_items_to_folder +from darwin.future.core.items.restore_items import restore_list_of_items from darwin.future.core.types.common import QueryString from darwin.future.core.types.query import PaginatedQuery from darwin.future.meta.objects.item import Item @@ -81,3 +82,22 @@ def move_to_folder(self, path) -> None: ids = [item.id for item in self] filters = {"item_ids": [str(item) for item in ids]} move_list_of_items_to_folder(self.client, team_slug, dataset_ids, path, filters) + + def restore(self) -> None: + if "team_slug" not in self.meta_params: + raise ValueError("Must specify team_slug to query items") + if ( + "dataset_ids" not in self.meta_params + and "dataset_id" not in self.meta_params + ): + raise ValueError("Must specify dataset_ids to query items") + dataset_ids = ( + self.meta_params["dataset_ids"] + if "dataset_ids" in self.meta_params + else self.meta_params["dataset_id"] + ) + team_slug = self.meta_params["team_slug"] + self.collect_all() + ids = [item.id for item in self] + filters = {"item_ids": [str(item) for item in ids]} + restore_list_of_items(self.client, team_slug, dataset_ids, filters) diff --git a/darwin/future/tests/meta/objects/test_itemmeta.py b/darwin/future/tests/meta/objects/test_itemmeta.py index 39fe840e0..6e9093381 100644 --- a/darwin/future/tests/meta/objects/test_itemmeta.py +++ b/darwin/future/tests/meta/objects/test_itemmeta.py @@ -97,3 +97,26 @@ def test_move_to_folder_raises_on_incorrect_parameters(item: Item) -> None: ) with pytest.raises(BadRequest): item.move_to_folder(path) + + +def test_restore(item: Item) -> None: + with responses.RequestsMock() as rsps: + team_slug = item.meta_params["team_slug"] + dataset_id = item.meta_params["dataset_id"] + rsps.add( + rsps.POST, + item.client.config.api_endpoint + f"v2/teams/{team_slug}/items/restore", + status=200, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id)], + "dataset_ids": [dataset_id], + } + } + ) + ], + json={}, + ) + item.restore() diff --git a/darwin/future/tests/meta/queries/test_item.py b/darwin/future/tests/meta/queries/test_item.py index 314a353c8..35fb32092 100644 --- a/darwin/future/tests/meta/queries/test_item.py +++ b/darwin/future/tests/meta/queries/test_item.py @@ -146,3 +146,38 @@ def test_move_to_folder_raises_on_incorrect_parameters( ) with pytest.raises(BadRequest): item_query.move_to_folder(path) + + +def test_restore( + item_query: ItemQuery, items_json: List[dict], items: List[Item] +) -> None: + with responses.RequestsMock() as rsps: + rsps.add( + rsps.GET, + item_query.client.config.api_endpoint + "v2/teams/test/items", + match=[ + query_param_matcher( + {"page[offset]": "0", "page[size]": "500", "dataset_ids": "1"} + ) + ], + json={"items": items_json, "errors": []}, + ) + team_slug = items[0].meta_params["team_slug"] + dataset_id = items[0].meta_params["dataset_id"] + rsps.add( + rsps.POST, + items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/restore", + status=200, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id) for item in items], + "dataset_ids": [dataset_id], + } + } + ) + ], + json={}, + ) + item_query.restore()