Skip to content

Commit f6b1e00

Browse files
committed
new assign - initial
1 parent a4ae3ee commit f6b1e00

File tree

2 files changed

+160
-20
lines changed

2 files changed

+160
-20
lines changed

superannotate/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def consensus(*args, **kwargs):
5959
)
6060
from .db.project_images import (
6161
assign_images, copy_image, copy_images, delete_images, move_image,
62-
move_images, pin_image, upload_image_to_project
62+
move_images, pin_image, upload_image_to_project, assign_folder,
63+
unassign_folder, unassign_images
6364
)
6465
from .db.projects import (
6566
clone_project, create_project, create_project_from_metadata, delete_project,

superannotate/db/project_images.py

Lines changed: 158 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
)
1717
from .project_api import get_project_and_folder_metadata
1818
from .projects import (
19-
get_project_default_image_quality_in_editor, _get_available_image_counts
19+
get_project_default_image_quality_in_editor, _get_available_image_counts,
20+
get_project_metadata
2021
)
2122
from ..mixp.decorators import Trackable
2223
from .utils import _get_upload_auth_token, _get_boto_session_by_credentials, upload_image_array_to_s3, get_image_array_to_upload, __create_image, __copy_images, __move_images, get_project_folder_string
@@ -563,30 +564,168 @@ def assign_images(project, image_names, user):
563564
:param user: user email
564565
:type user: str
565566
"""
566-
logger.info("Assign %s images to user %s", len(image_names), user)
567-
if len(image_names) == 0:
568-
return
569-
project, project_folder = get_project_and_folder_metadata(project)
570-
images = search_images((project, project_folder), return_metadata=True)
571-
image_dict = {}
572-
for image in images:
573-
image_dict[image["name"]] = image["id"]
574567

575-
image_ids = []
576-
for image_name in image_names:
577-
image_ids.append(image_dict[image_name])
578-
team_id, project_id = project["team_id"], project["id"]
579-
params = {"team_id": team_id, "project_id": project_id}
580-
if project_folder is not None:
581-
params['folder_id'] = project_folder['id']
582-
json_req = {"user_id": user, "image_ids": image_ids}
568+
# TODO:
569+
# logger.info("Assign %s images to user %s", len(image_names), user)
570+
# if len(image_names) == 0:
571+
# return
572+
573+
project, folder = get_project_and_folder_metadata(project)
574+
if not folder:
575+
folder = 'root'
576+
577+
project_meta = get_project_metadata(project)
578+
params = {
579+
"project_id": project_meta['id'],
580+
"team_id": project_meta["team_id"]
581+
}
582+
json_req = {
583+
"image_names": image_names,
584+
"assign_user_id": user,
585+
"folder_name": folder,
586+
}
583587
response = _api.send_request(
584-
req_type='POST',
585-
path='/images/assign',
588+
req_type='PUT',
589+
path='/images/editAssignment',
586590
params=params,
587591
json_req=json_req
588592
)
593+
589594
if not response.ok:
590595
raise SABaseException(
591596
response.status_code, "Couldn't assign images " + response.text
592597
)
598+
599+
# images = search_images((project, project_folder), return_metadata=True)
600+
# image_dict = {}
601+
# for image in images:
602+
# image_dict[image["name"]] = image["id"]
603+
604+
# image_ids = []
605+
# for image_name in image_names:
606+
# image_ids.append(image_dict[image_name])
607+
# team_id, project_id = project["team_id"], project["id"]
608+
# params = {"team_id": team_id, "project_id": project_id}
609+
# if project_folder is not None:
610+
# params['folder_id'] = project_folder['id']
611+
# json_req = {"user_id": user, "image_ids": image_ids}
612+
# response = _api.send_request(
613+
# req_type='POST',
614+
# path='/images/assign',
615+
# params=params,
616+
# json_req=json_req
617+
# )
618+
# if not response.ok:
619+
# raise SABaseException(
620+
# response.status_code, "Couldn't assign images " + response.text
621+
# )
622+
623+
624+
def assign_folder(project, folder_name, users):
625+
"""Assigns folder to users. With SDK, the user can be
626+
assigned to a role in the project with the share_project function.
627+
628+
:param project: project name or metadata of the project
629+
:type project: str or dict
630+
:param folder_name: folder name to assign
631+
:type folder_name: str
632+
:param users: list of user emails
633+
:type user: list of str
634+
"""
635+
636+
project_meta = get_project_metadata(project, include_contributors=True)
637+
project_users = project_meta["contributors"]
638+
project_name = project_meta['name']
639+
project_users = [i['user_id'] for i in project_users]
640+
verified_contributor = []
641+
642+
for user in users:
643+
if user not in project_users:
644+
logging.warn(
645+
f'Skipping {user} from assignees. {user} is not a verified contributor for the {project_name}'
646+
)
647+
continue
648+
verified_contributor.append(user)
649+
650+
params = {
651+
"project_id": project_meta['id'],
652+
"team_id": project_meta["team_id"]
653+
}
654+
json_req = {
655+
"assign_user_ids": verified_contributor,
656+
"folder_name": folder_name
657+
}
658+
response = _api.send_request(
659+
req_type='POST',
660+
path='/folder/editAssignment',
661+
params=params,
662+
json_req=json_req
663+
)
664+
665+
if not response.ok:
666+
raise SABaseException(
667+
response.status_code, "Couldn't assign folder " + response.text
668+
)
669+
logger.info(f'Assigned {folder_name} to users: {verified_contributor}')
670+
671+
672+
def unassign_folder(project, folder_name):
673+
"""Removes assignment of given folder for all assignees.
674+
With SDK, the user can be assigned to a role in the project
675+
with the share_project function.
676+
677+
:param project: project name or folder path (e.g., "project1/folder1")
678+
:type project: str
679+
:param folder_name: folder name to remove assignees
680+
:type folder_name: str
681+
"""
682+
683+
project_meta = get_project_metadata(project)
684+
params = {
685+
"project_id": project_meta['id'],
686+
"team_id": project_meta["team_id"]
687+
}
688+
json_req = {"folder_name": folder_name, "remove_user_ids": ["all"]}
689+
response = _api.send_request(
690+
req_type='POST',
691+
path='/folder/editAssignment',
692+
params=params,
693+
json_req=json_req
694+
)
695+
696+
if not response.ok:
697+
raise SABaseException(
698+
response.status_code, "Couldn't unassign folder " + response.text
699+
)
700+
print('unassign_folder>>>>>>', response.text)
701+
702+
703+
def unassign_images(project, image_names):
704+
"""Removes assignment of given images for all assignees.With SDK,
705+
the user can be assigned to a role in the project with the share_project
706+
function.
707+
708+
:param project: project name or folder path (e.g., "project1/folder1")
709+
:type project: str
710+
:param image_names: list of image unassign
711+
:type image_names: list of str
712+
"""
713+
project_meta = get_project_metadata(project)
714+
params = {
715+
"project_id": project_meta['id'],
716+
"team_id": project_meta["team_id"]
717+
}
718+
json_req = {"image_names": image_names, "remove_user_ids": ["all"]}
719+
response = _api.send_request(
720+
req_type='PUT',
721+
path='/images/editAssignment',
722+
params=params,
723+
json_req=json_req
724+
)
725+
726+
if not response.ok:
727+
raise SABaseException(
728+
response.status_code, "Couldn't unassign images " + response.text
729+
)
730+
731+
print('unassign_images>>>>>>', response.text)

0 commit comments

Comments
 (0)