Skip to content

Commit 56db34d

Browse files
author
Adrian Chang
committed
add users
1 parent 64adee5 commit 56db34d

File tree

4 files changed

+82
-53
lines changed

4 files changed

+82
-53
lines changed

libs/labelbox/src/labelbox/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@
4040
from labelbox.schema.identifiable import UniqueId, GlobalKey
4141
from labelbox.schema.ontology_kind import OntologyKind
4242
from labelbox.schema.project_overview import ProjectOverview, ProjectOverviewDetailed
43+
from labelbox.schema.user_group import UserGroup, UserGroupColor, UserGroupUser, UserGroupProject

libs/labelbox/src/labelbox/client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,32 @@ def get_projects(self, where=None) -> PaginatedCollection:
541541
"""
542542
return self._get_all(Entity.Project, where)
543543

544+
def get_users(self, where=None) -> PaginatedCollection:
545+
""" Fetches all the users.
546+
547+
>>> users = client.get_users(where=User.email == "<user_email>")
548+
549+
Args:
550+
where (Comparison, LogicalOperation or None): The `where` clause
551+
for filtering.
552+
Returns:
553+
An iterable of Users (typically a PaginatedCollection).
554+
"""
555+
return self._get_all(Entity.User, where, filter_deleted=False)
556+
557+
def get_groups(self, where=None) -> PaginatedCollection:
558+
""" Fetches all the users.
559+
560+
>>> users = client.get_users(where=User.email == "<user_email>")
561+
562+
Args:
563+
where (Comparison, LogicalOperation or None): The `where` clause
564+
for filtering.
565+
Returns:
566+
An iterable of Users (typically a PaginatedCollection).
567+
"""
568+
return self._get_all(Entity.UserGroup, None)
569+
544570
def get_datasets(self, where=None) -> PaginatedCollection:
545571
""" Fetches one or more datasets.
546572

libs/labelbox/src/labelbox/orm/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ class Entity(metaclass=EntityMeta):
382382
CatalogSlice: Type[labelbox.CatalogSlice]
383383
ModelSlice: Type[labelbox.ModelSlice]
384384
TaskQueue: Type[labelbox.TaskQueue]
385+
UserGroup: Type[labelbox.UserGroup]
385386

386387
@classmethod
387388
def _attributes_of_type(cls, attr_type):

libs/labelbox/src/labelbox/schema/group.py renamed to libs/labelbox/src/labelbox/schema/user_group.py

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from enum import Enum
2-
from typing import Set, List, TypedDict, Optional
2+
from typing import Set, List, Optional, Union, TypedDict
3+
34

45
from labelbox import Client
56
from labelbox.exceptions import ResourceCreationError
7+
from labelbox.pydantic_compat import BaseModel
68
from labelbox.schema.user import User
79
from labelbox.schema.project import Project
8-
from labelbox.pydantic_compat import BaseModel, PrivateAttr
910

10-
class GroupColor(Enum):
11+
12+
class UserGroupColor(Enum):
1113
"""
1214
Enum representing the colors available for a group.
1315
@@ -33,7 +35,7 @@ class GroupColor(Enum):
3335
GRAY = "B8C4D3"
3436

3537

36-
class GroupUser(BaseModel):
38+
class UserGroupUser(BaseModel):
3739
"""
3840
Represents a user in a group.
3941
@@ -44,17 +46,16 @@ class GroupUser(BaseModel):
4446
id: str
4547
email: str
4648

47-
4849
def __hash__(self):
4950
return hash((self.id))
5051

5152
def __eq__(self, other):
52-
if not isinstance(other, GroupUser):
53+
if not isinstance(other, UserGroupUser):
5354
return False
5455
return self.id == other.id
5556

5657

57-
class GroupProject(BaseModel):
58+
class UserGroupProject(BaseModel):
5859
"""
5960
Represents a project in a group.
6061
@@ -78,76 +79,77 @@ def __eq__(self, other):
7879
Returns:
7980
bool: True if the two GroupProject objects are equal, False otherwise.
8081
"""
81-
if not isinstance(other, GroupProject):
82+
if not isinstance(other, UserGroupProject):
8283
return False
8384
return self.id == other.id
8485

8586

86-
class GroupParmeters(TypedDict):
87+
class UserGroupParameters(TypedDict):
8788
"""
88-
Represents the parameters for a group.
89+
Represents the parameters for a user group.
8990
9091
Attributes:
91-
id (Optional[str]): The ID of the group.
92-
name (Optional[str]): The name of the group.
93-
color (Optional[GroupColor]): The color of the group.
94-
users (Optional[Set[GroupUser]]): The users in the group.
95-
projects (Optional[Set[GroupProject]]): The projects associated with the group.
92+
id (Optional[str]): The ID of the user group.
93+
name (Optional[str]): The name of the user group.
94+
color (Optional[UserGroupColor]): The color of the user group.
95+
users (Optional[Set[Union[UserGroupUser, User]]]): The users in the user group.
96+
projects (Optional[Set[Union[UserGroupProject, Project]]]): The projects associated with the user group.
9697
"""
9798
id: Optional[str]
9899
name: Optional[str]
99-
color: Optional[GroupColor]
100-
users: Optional[Set[GroupUser]]
101-
projects: Optional[Set[GroupProject]]
100+
color: Optional[UserGroupColor]
101+
users: Optional[Set[Union[UserGroupUser, User]]]
102+
projects: Optional[Set[Union[UserGroupProject, Project]]]
102103

103104

104-
class Group():
105+
class UserGroup:
105106
"""
106-
Represents a group of users in Labelbox.
107+
Represents a user group in Labelbox.
107108
108109
Args:
109110
client (Client): The Labelbox client.
110-
data (dict): The data dictionary containing group information.
111+
**kwargs: Additional keyword arguments for initializing the UserGroup object.
111112
112113
Attributes:
113-
id (str): The ID of the group.
114-
name (str): The name of the group.
115-
color (GroupColor): The color of the group.
116-
users (List[str]): The list of user IDs in the group.
117-
projects (List[str]): The list of project IDs in the group.
118-
client (Client): The Labelbox client.
114+
_id (str): The ID of the user group.
115+
_name (str): The name of the user group.
116+
_color (UserGroupColor): The color of the user group.
117+
_users (Set[Union[UserGroupUser, User]]): The set of user IDs in the user group.
118+
_projects (Set[Union[UserGroupProject, Project]]): The set of project IDs in the user group.
119+
_client (Client): The Labelbox client.
119120
"""
120-
_id: str
121+
_id: str = None
121122
_name: str = None
122-
_color: GroupColor = None
123-
_users: Set[GroupUser] = None
124-
_projects: Set[GroupProject] = None
125-
_client: Client
123+
_color: UserGroupColor = None
124+
_users: Set[Union[UserGroupUser, User]] = None
125+
_projects: Set[Union[UserGroupProject, Project]] = None
126+
_client: Client
126127

127-
def __init__(self, client: Client, **kwargs: GroupParmeters):
128+
def __init__(self, client: Client, **kwargs: UserGroupParameters):
128129
"""
129130
Initializes a Group object.
130131
131132
Args:
132133
client (Client): The Labelbox client.
133134
**kwargs: Additional keyword arguments for initializing the Group object.
134135
"""
135-
self.id = kwargs['id']
136-
self.color = kwargs.get('color', GroupColor.BLUE)
136+
super().__init__()
137+
self.color = kwargs.get('color', UserGroupColor.BLUE)
137138
self.users = kwargs.get('users', {})
138139
self.projects = kwargs.get('projects', {})
139140
self.client = client
140141

141142
# runs against _gql
142-
if client.enable_experimental is False:
143+
if not client.enable_experimental:
143144
raise RuntimeError("Experimental features are not enabled. Please enable them in the client to use this feature.")
144145

146+
if 'id' not in kwargs and 'name' not in kwargs:
147+
raise ValueError("Either 'id' or 'name' must be provided")
148+
145149
# partial respentation of the group, reload
146-
if self.id is not None:
150+
if 'id' in kwargs:
151+
self.id = kwargs['id']
147152
self._reload()
148-
else:
149-
self.name = kwargs['name']
150-
super().__init__()
151153

152154
def _reload(self):
153155
"""
@@ -188,9 +190,9 @@ def _reload(self):
188190
}
189191
result = self.client.execute(query, params)
190192
self.name = result["userGroup"]["name"]
191-
self.color = GroupColor(result["userGroup"]["color"])
192-
self.projects = {GroupProject(id=project["id"], name=project["name"]) for project in result["userGroup"]["projects"]["nodes"]}
193-
self.users = {GroupUser(id=member["id"], email=member["email"]) for member in result["userGroup"]["members"]["nodes"]}
193+
self.color = UserGroupColor(result["userGroup"]["color"])
194+
self.projects = {UserGroupProject(id=project["id"], name=project["name"]) for project in result["userGroup"]["projects"]["nodes"]}
195+
self.users = {UserGroupUser(id=member["id"], email=member["email"]) for member in result["userGroup"]["members"]["nodes"]}
194196

195197
@property
196198
def id(self) -> str:
@@ -233,7 +235,7 @@ def name(self, value: str) -> None:
233235
self._name = value
234236

235237
@property
236-
def color(self) -> GroupColor:
238+
def color(self) -> UserGroupColor:
237239
"""
238240
Gets the color of the group.
239241
@@ -243,18 +245,17 @@ def color(self) -> GroupColor:
243245
return self._color
244246

245247
@color.setter
246-
def color(self, value: GroupColor) -> None:
248+
def color(self, value: UserGroupColor) -> None:
247249
"""
248250
Sets the color of the group.
249251
250252
Args:
251253
value (GroupColor): The color to set.
252254
"""
253255
self._color = value
254-
self._update()
255256

256257
@property
257-
def users(self) -> Set[GroupUser]:
258+
def users(self) -> Set[Union[UserGroupUser, User]]:
258259
"""
259260
Gets the list of user IDs in the group.
260261
@@ -264,7 +265,7 @@ def users(self) -> Set[GroupUser]:
264265
return self._users
265266

266267
@users.setter
267-
def users(self, value: Set[GroupUser]) -> None:
268+
def users(self, value: Set[Union[UserGroupUser, User]]) -> None:
268269
"""
269270
Sets the list of user IDs in the group.
270271
@@ -274,7 +275,7 @@ def users(self, value: Set[GroupUser]) -> None:
274275
self._users = value
275276

276277
@property
277-
def projects(self) -> Set[GroupProject]:
278+
def projects(self) -> Set[UserGroupProject]:
278279
"""
279280
Gets the list of project IDs in the group.
280281
@@ -284,7 +285,7 @@ def projects(self) -> Set[GroupProject]:
284285
return self._projects
285286

286287
@projects.setter
287-
def projects(self, value: Set[GroupProject]) -> None:
288+
def projects(self, value: Set[UserGroupProject]) -> None:
288289
"""
289290
Sets the list of project IDs in the group.
290291
@@ -451,9 +452,9 @@ def groups(client: Client) -> List["Group"]:
451452
client,
452453
group["id"],
453454
group["name"],
454-
GroupColor(group["color"]),
455-
{GroupUser(id=member["id"], email=member["email"]) for member in group["members"]["nodes"]},
456-
{GroupProject(id=project["id"], name=project["name"]) for project in group["projects"]["nodes"]}
455+
UserGroupColor(group["color"]),
456+
{UserGroupUser(id=member["id"], email=member["email"]) for member in group["members"]["nodes"]},
457+
{UserGroupProject(id=project["id"], name=project["name"]) for project in group["projects"]["nodes"]}
457458
)
458459
for group in groups
459460
]

0 commit comments

Comments
 (0)