|
16 | 16 | ) |
17 | 17 | from .project_api import get_project_and_folder_metadata |
18 | 18 | 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 |
20 | 21 | ) |
21 | 22 | from ..mixp.decorators import Trackable |
22 | 23 | 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): |
563 | 564 | :param user: user email |
564 | 565 | :type user: str |
565 | 566 | """ |
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"] |
574 | 567 |
|
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 | + } |
583 | 587 | response = _api.send_request( |
584 | | - req_type='POST', |
585 | | - path='/images/assign', |
| 588 | + req_type='PUT', |
| 589 | + path='/images/editAssignment', |
586 | 590 | params=params, |
587 | 591 | json_req=json_req |
588 | 592 | ) |
| 593 | + |
589 | 594 | if not response.ok: |
590 | 595 | raise SABaseException( |
591 | 596 | response.status_code, "Couldn't assign images " + response.text |
592 | 597 | ) |
| 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