Skip to content

Commit

Permalink
[PY-400][external] Move Meta Item & ItemQuery to folder (#717)
Browse files Browse the repository at this point in the history
* Fixed missing path parameter in core move items to folder method

* Fixed missing priority parameter in core set item priority method

* Linting

* Meta Item & ItemQuery functions to move between folders

* Update darwin/future/tests/meta/queries/test_item.py

Co-authored-by: Owen Jones <[email protected]>

* Added sad path tests

---------

Co-authored-by: Owen Jones <[email protected]>
  • Loading branch information
JBWilkie and owencjones authored Nov 14, 2023
1 parent 0efb835 commit c638ce8
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 2 deletions.
3 changes: 2 additions & 1 deletion darwin/future/core/items/move_items_to_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def move_list_of_items_to_folder(
if isinstance(dataset_ids, list)
else [dataset_ids],
**filters,
}
},
"path": path,
}

return client.post(f"/v2/teams/{team_slug}/items/path", data=payload)
3 changes: 2 additions & 1 deletion darwin/future/core/items/set_item_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def set_item_priority(
if isinstance(dataset_ids, list)
else [dataset_ids],
**filters,
}
},
"priority": priority,
}

return client.post(
Expand Down
13 changes: 13 additions & 0 deletions darwin/future/meta/objects/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from uuid import UUID

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.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 move_to_folder(self, path: str) -> 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)]}
move_list_of_items_to_folder(self.client, team_slug, dataset_id, path, filters)

@property
def name(self) -> str:
return self._element.name
Expand Down
22 changes: 22 additions & 0 deletions darwin/future/meta/queries/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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.types.common import QueryString
from darwin.future.core.types.query import PaginatedQuery
from darwin.future.meta.objects.item import Item
Expand Down Expand Up @@ -59,3 +60,24 @@ 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 move_to_folder(self, path) -> 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")
if not path:
raise ValueError("Must specify path to move items to")
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]}
move_list_of_items_to_folder(self.client, team_slug, dataset_ids, path, filters)
53 changes: 53 additions & 0 deletions darwin/future/tests/meta/objects/test_itemmeta.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from uuid import UUID

import pytest
import responses
from responses import json_params_matcher

from darwin.future.data_objects.item import ItemLayout, ItemSlot
from darwin.future.exceptions import BadRequest
from darwin.future.meta.objects.item import Item
from darwin.future.tests.meta.objects.fixtures import *

Expand Down Expand Up @@ -44,3 +46,54 @@ def test_delete(item: Item) -> None:
json={},
)
item.delete()


def test_move_to_folder(item: Item) -> None:
with responses.RequestsMock() as rsps:
team_slug = item.meta_params["team_slug"]
dataset_id = item.meta_params["dataset_id"]
path = "/new_folder"
rsps.add(
rsps.POST,
item.client.config.api_endpoint + f"v2/teams/{team_slug}/items/path",
status=200,
match=[
json_params_matcher(
{
"filters": {
"item_ids": [str(item.id)],
"dataset_ids": [dataset_id],
},
"path": path,
}
)
],
json={},
)
item.move_to_folder(path)


def test_move_to_folder_raises_on_incorrect_parameters(item: Item) -> None:
with responses.RequestsMock() as rsps:
team_slug = item.meta_params["team_slug"]
dataset_id = item.meta_params["dataset_id"]
path = 1234
rsps.add(
rsps.POST,
item.client.config.api_endpoint + f"v2/teams/{team_slug}/items/path",
status=400,
match=[
json_params_matcher(
{
"filters": {
"item_ids": [str(item.id)],
"dataset_ids": [dataset_id],
},
"path": path,
}
)
],
json={},
)
with pytest.raises(BadRequest):
item.move_to_folder(path)
76 changes: 76 additions & 0 deletions darwin/future/tests/meta/queries/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from responses.matchers import json_params_matcher, query_param_matcher

from darwin.future.core.client import ClientCore
from darwin.future.exceptions import BadRequest
from darwin.future.meta.objects.item import Item
from darwin.future.meta.queries.item import ItemQuery
from darwin.future.tests.core.fixtures import *
Expand Down Expand Up @@ -70,3 +71,78 @@ def test_delete(
json={},
)
item_query.delete()


def test_move_to_folder(
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"]
path = "/new_folder"
rsps.add(
rsps.POST,
items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/path",
status=200,
match=[
json_params_matcher(
{
"filters": {
"item_ids": [str(item.id) for item in items],
"dataset_ids": [dataset_id],
},
"path": path,
}
)
],
json={},
)
item_query.move_to_folder(path)


def test_move_to_folder_raises_on_incorrect_parameters(
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"]
path = 1234
rsps.add(
rsps.POST,
items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/path",
status=400,
match=[
json_params_matcher(
{
"filters": {
"item_ids": [str(item.id) for item in items],
"dataset_ids": [dataset_id],
},
"path": path,
}
)
],
json={},
)
with pytest.raises(BadRequest):
item_query.move_to_folder(path)

0 comments on commit c638ce8

Please sign in to comment.