Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PY-402][external] Archive Meta Item & ItemQuery functions #719

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions darwin/future/meta/objects/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict, List, Optional, Union, cast
from uuid import UUID

from darwin.future.core.items.archive_items import archive_list_of_items
from darwin.future.core.items.delete_items import delete_list_of_items
from darwin.future.data_objects.item import ItemCore, ItemLayout, ItemSlot
from darwin.future.meta.objects.base import MetaBase
Expand Down Expand Up @@ -53,6 +54,18 @@ def delete(self) -> None:
filters = {"item_ids": [str(self.id)]}
delete_list_of_items(self.client, team_slug, dataset_id, filters)

def archive(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)]}
archive_list_of_items(self.client, team_slug, dataset_id, filters)

@property
def name(self) -> str:
return self._element.name
Expand Down
20 changes: 20 additions & 0 deletions darwin/future/meta/queries/item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import reduce
from typing import Dict

from darwin.future.core.items.archive_items import archive_list_of_items
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.types.common import QueryString
Expand Down Expand Up @@ -59,3 +60,22 @@ def delete(self) -> None:
ids = [item.id for item in self]
filters = {"item_ids": [str(item) for item in ids]}
delete_list_of_items(self.client, team_slug, dataset_ids, filters)

def archive(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]}
archive_list_of_items(self.client, team_slug, dataset_ids, filters)
23 changes: 23 additions & 0 deletions darwin/future/tests/meta/objects/test_itemmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,26 @@ def test_delete(item: Item) -> None:
json={},
)
item.delete()


def test_archive(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/archive",
status=200,
match=[
json_params_matcher(
{
"filters": {
"item_ids": [str(item.id)],
"dataset_ids": [dataset_id],
}
}
)
],
json={},
)
item.archive()
JBWilkie marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions darwin/future/tests/meta/queries/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,38 @@ def test_delete(
json={},
)
item_query.delete()


def test_archive(
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/archive",
status=200,
match=[
json_params_matcher(
{
"filters": {
"item_ids": [str(item.id) for item in items],
"dataset_ids": [dataset_id],
}
}
)
],
json={},
)
item_query.archive()
JBWilkie marked this conversation as resolved.
Show resolved Hide resolved