|
19 | 19 | ) |
20 | 20 | from kili.entrypoints.mutations.asset.queries import ( |
21 | 21 | GQL_ADD_ALL_LABELED_ASSETS_TO_REVIEW, |
| 22 | + GQL_ASSIGN_ASSETS, |
22 | 23 | GQL_DELETE_MANY_FROM_DATASET, |
23 | 24 | GQL_SEND_BACK_ASSETS_TO_QUEUE, |
24 | 25 | GQL_UPDATE_PROPERTIES_IN_ASSETS, |
@@ -203,6 +204,59 @@ def append_many_to_dataset( |
203 | 204 | ) |
204 | 205 | return {"id": project_id, "asset_ids": created_asset_ids} |
205 | 206 |
|
| 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 | + |
206 | 260 | @typechecked |
207 | 261 | def update_properties_in_assets( |
208 | 262 | self, |
@@ -312,6 +366,14 @@ def update_properties_in_assets( |
312 | 366 | ) |
313 | 367 | raise MissingArgumentError("Please provide either `asset_ids` or `external_ids`.") |
314 | 368 |
|
| 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 | + |
315 | 377 | resolved_asset_ids = self._resolve_asset_ids(asset_ids, external_ids, project_id) |
316 | 378 |
|
317 | 379 | properties_to_batch = process_update_properties_in_assets_parameters( |
|
0 commit comments