Skip to content

Commit 622d760

Browse files
Sihem Tchabibaptiste-olivier
authored andcommitted
fix(LAB-3326): creation of a new method to assign labelers to assets + warning deprecate for ToBeLabeledByArray parameter
1 parent 04f8b63 commit 622d760

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/kili/entrypoints/mutations/asset/__init__.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020
from kili.entrypoints.mutations.asset.queries import (
2121
GQL_ADD_ALL_LABELED_ASSETS_TO_REVIEW,
22+
GQL_ASSIGN_ASSETS,
2223
GQL_DELETE_MANY_FROM_DATASET,
2324
GQL_SEND_BACK_ASSETS_TO_QUEUE,
2425
GQL_UPDATE_PROPERTIES_IN_ASSETS,
@@ -203,6 +204,59 @@ def append_many_to_dataset(
203204
)
204205
return {"id": project_id, "asset_ids": created_asset_ids}
205206

207+
@typechecked
208+
def assign_assets_to_labelers(
209+
self,
210+
to_be_labeled_by_array: List[List[str]],
211+
asset_ids: Optional[List[str]] = None,
212+
external_ids: Optional[List[str]] = None,
213+
) -> List[Dict[str, Any]]:
214+
# pylint: disable=line-too-long
215+
"""Assign a list of assets to a list of labelers.
216+
217+
Args:
218+
asset_ids: The internal asset IDs to assign.
219+
external_ids: The external asset IDs to assign (if `asset_ids` is not already provided).
220+
to_be_labeled_by_array: The array of list of labelers to assign per labelers (list of userIds).
221+
222+
Returns:
223+
A list of dictionaries with the asset ids.
224+
225+
Examples:
226+
>>> kili.assign_assets_to_labelers(
227+
asset_ids=["ckg22d81r0jrg0885unmuswj8", "ckg22d81s0jrh0885pdxfd03n"],
228+
to_be_labeled_by_array=[['cm3yja6kv0i698697gcil9rtk','cm3yja6kv0i000000gcil9rtk'],
229+
['cm3yja6kv0i698697gcil9rtk']]
230+
)
231+
232+
# The following call resets the assignees on the asset_ids given.
233+
>>> kili.assign_assets_to_labelers(
234+
asset_ids=["ckg22d81r0jrg0885unmuswj8", "ckg22d81s0jrh0885pdxfd03n"],
235+
to_be_labeled_by_array=[[], []]
236+
)
237+
"""
238+
if is_empty_list_with_warning(
239+
"assign_assets_to_labelers", "asset_ids", asset_ids
240+
) and is_empty_list_with_warning("assign_assets_to_labelers", "external_ids", external_ids):
241+
return []
242+
243+
if (asset_ids is not None and external_ids is not None) or (
244+
asset_ids is None and external_ids is None
245+
):
246+
raise MissingArgumentError("Please provide either `asset_ids` or `external_ids`.")
247+
248+
resolved_asset_ids = self._resolve_asset_ids(asset_ids, external_ids, project_id=None)
249+
250+
if len(resolved_asset_ids) != len(to_be_labeled_by_array):
251+
raise MutationError("There must be as many assets as there are lists of labelers.")
252+
253+
formated_results = []
254+
for asset_id, to_be_labeled_by in zip(resolved_asset_ids, to_be_labeled_by_array):
255+
payload = {"userIds": to_be_labeled_by, "where": {"id": asset_id}}
256+
results = self.graphql_client.execute(GQL_ASSIGN_ASSETS, payload)
257+
formated_results.append(results)
258+
return formated_results
259+
206260
@typechecked
207261
def update_properties_in_assets(
208262
self,
@@ -312,6 +366,14 @@ def update_properties_in_assets(
312366
)
313367
raise MissingArgumentError("Please provide either `asset_ids` or `external_ids`.")
314368

369+
if to_be_labeled_by_array is not None:
370+
warnings.warn(
371+
"to_be_labeled_by_array is going to be deprecated. Please use"
372+
" `kili.assign_assets_to_labelers()` method instead to assign assets",
373+
DeprecationWarning,
374+
stacklevel=1,
375+
)
376+
315377
resolved_asset_ids = self._resolve_asset_ids(asset_ids, external_ids, project_id)
316378

317379
properties_to_batch = process_update_properties_in_assets_parameters(

src/kili/entrypoints/mutations/asset/queries.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
from kili.entrypoints.mutations.project.fragments import PROJECT_FRAGMENT_ID
44

5+
GQL_ASSIGN_ASSETS = """
6+
mutation assignAssetsToLabelers(
7+
$where: AssetWhere!,
8+
$userIds: [String!]!
9+
) {
10+
data: assignAssetsToLabelers(
11+
where: $where,
12+
userIds: $userIds
13+
) {
14+
id
15+
}
16+
}
17+
"""
18+
519
GQL_UPDATE_PROPERTIES_IN_ASSETS = """
620
mutation(
721
$whereArray: [AssetWhere!]!

0 commit comments

Comments
 (0)