Skip to content

Commit

Permalink
Vb/member export plt 2099 (#1925)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrodsky authored Dec 10, 2024
1 parent 78a7e3d commit b6427ed
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/labelbox/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ Labelbox Python SDK Documentation
task
task-queue
user
user-group-upload
user-group-v2
webhook
6 changes: 6 additions & 0 deletions docs/labelbox/user-group-v2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
User Group
===============================================================================================

.. automodule:: labelbox.schema.user_group_v2
:members:
:show-inheritance:
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ class UploadReport:
lines: List[UploadReportLine]


class UserGroupUpload:
@dataclass
class Member:
"""A member of a user group."""

email: str


class UserGroupV2:
"""Upload members to a user group."""

def __init__(self, client: Client):
Expand All @@ -80,7 +87,7 @@ def upload_members(
For indicvidual email errors, the error message is available in the UploadReport.
"""
warnings.warn(
"The upload_members for UserGroupUpload is in beta. The method name and signature may change in the future.”",
"The upload_members for UserGroupV2 is in beta. The method name and signature may change in the future.”",
)

if len(emails) == 0:
Expand Down Expand Up @@ -109,7 +116,7 @@ def upload_members(
"text/csv",
)
}
query = """mutation ImportMembersToGroup(
query = """mutation ImportMembersToGroupPyPi(
$roleId: ID!
$file: Upload!
$where: WhereUniqueIdInput!
Expand Down Expand Up @@ -183,6 +190,47 @@ def upload_members(
csv_report = file_data["importUsersAsCsvToGroup"]["csvReport"]
return self._parse_csv_report(csv_report)

def export_members(self, group_id: str) -> Optional[List[Member]]:
warnings.warn(
"The export_members for UserGroupV2 is in beta. The method name and signature may change in the future.",
)

if not group_id:
raise ValueError("Group id is required")

query = """query GetExportMembersAsCSVPyPi(
$id: ID!
) {
userGroupV2(where: { id: $id }) {
id
membersAsCSV
}
}
"""
params = {
"id": group_id,
}

result = self.client.execute(query, params)
if result["userGroupV2"] is None:
raise ResourceNotFoundError(message="The user group is not found.")
data = result["userGroupV2"]

return self._parse_members_csv(data["membersAsCSV"])

def _parse_members_csv(self, csv_data: str) -> List[Member]:
csv_lines = csv_data.strip().split("\n")
if not csv_lines:
return []

members_list = []
# Skip header row
for email in csv_lines[1:]:
if email.strip(): # Skip empty lines
members_list.append(Member(email=email.strip()))

return members_list

def _get_role_id(self, role_name: str) -> Optional[str]:
role_id = None
query = """query GetAvailableUserRolesPyPi {
Expand Down
50 changes: 50 additions & 0 deletions libs/labelbox/tests/unit/schema/test_user_group_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from unittest.mock import MagicMock

import pytest

from labelbox.schema.user_group_v2 import Member, UserGroupV2


@pytest.fixture
def client():
return MagicMock()


def test_parse_members_csv_empty(client):
group = UserGroupV2(client)
assert group._parse_members_csv("") == []
assert group._parse_members_csv("\n") == []


def test_parse_members_csv_header_only(client):
group = UserGroupV2(client)
assert group._parse_members_csv("email\n") == []


def test_parse_members_csv_single_member(client):
group = UserGroupV2(client)
result = group._parse_members_csv("email\n[email protected]")
assert len(result) == 1
assert isinstance(result[0], Member)
assert result[0].email == "[email protected]"


def test_parse_members_csv_multiple_members(client):
group = UserGroupV2(client)
csv_data = "email\n[email protected]\n[email protected]\n[email protected]"
result = group._parse_members_csv(csv_data)
assert len(result) == 3
assert [m.email for m in result] == [
"[email protected]",
"[email protected]",
"[email protected]",
]


def test_parse_members_csv_handles_whitespace(client):
group = UserGroupV2(client)
csv_data = "email\n [email protected] \n\n[email protected]\n"
result = group._parse_members_csv(csv_data)
assert len(result) == 2
assert result[0].email == "[email protected]"
assert result[1].email == "[email protected]"

0 comments on commit b6427ed

Please sign in to comment.