55from labelbox .pagination import PaginatedCollection
66from labelbox .orm .db_object import DbObject , beta
77from labelbox .orm .model import Field , Relationship
8- from labelbox .schema .invite import Invite , InviteLimit , UserLimit , ProjectRole
8+ from labelbox .schema .invite import Invite , InviteLimit , ProjectRole
99from labelbox .schema .user import User
1010from labelbox .schema .role import Role
1111import logging
1212
1313logger = logging .getLogger (__name__ )
1414
15+
1516class Organization (DbObject ):
1617 """ An Organization is a group of Users.
1718
@@ -45,61 +46,6 @@ def __init__(self, *args, **kwargs):
4546 projects = Relationship .ToMany ("Project" , True )
4647 webhooks = Relationship .ToMany ("Webhook" , False )
4748
48- @beta
49- def invites (self ) -> PaginatedCollection :
50- """ List all current invitees
51-
52- Returns:
53- A PaginatedCollection of Invite objects
54-
55- """
56- query_str = """query GetOrgInvitationsPyApi($from: ID, $first: PageSize) {
57- organization { id invites(from: $from, first: $first) {
58- nodes { id createdAt organizationRoleName inviteeEmail } nextCursor }}}"""
59- return PaginatedCollection (
60- self .client ,
61- query_str , {}, ['organization' , 'invites' , 'nodes' ],
62- Invite ,
63- cursor_path = ['organization' , 'invites' , 'nextCursor' ])
64-
65- def _assign_user_role (self , email : str , role : Role ,
66- project_roles : List [ProjectRole ]) -> Dict [str , Any ]:
67- """
68- Creates or updates users. This function shouldn't directly be called.
69- Use `Organization.invite_user`
70-
71- Note: that there is a really unclear foreign key error if you use an unsupported role.
72- - This means that the `Roles` object is not getting the right ids
73- """
74- if self .client .get_user ().email == email :
75- raise ValueError ("Cannot update your own role" )
76-
77- data_param = "data"
78- query_str = """mutation createInvitesPyApi($%s: [CreateInviteInput!]){
79- createInvites(data: $%s){ invite { id createdAt organizationRoleName inviteeEmail}}}""" % (
80- data_param , data_param )
81-
82- projects = [{
83- "projectId" : project_role .project .uid ,
84- "projectRoleId" : project_role .role .uid
85- } for project_role in project_roles ]
86-
87- res = self .client .execute (
88- query_str , {
89- data_param : [{
90- "inviterId" : self .client .get_user ().uid ,
91- "inviteeEmail" : email ,
92- "organizationId" : self .uid ,
93- "organizationRoleId" : role .uid ,
94- "projects" : projects
95- }]
96- },
97- )
98- # We prob want to return an invite
99- # Could support bulk ops in the future
100- invite_info = res ['createInvites' ][0 ]['invite' ]
101- return invite_info
102-
10349 @beta
10450 def invite_user (
10551 self ,
@@ -117,18 +63,18 @@ def invite_user(
11763 Returns:
11864 Invite for the user
11965
66+ Creates or updates users. This function shouldn't directly be called.
67+ Use `Organization.invite_user`
68+
69+ Note: that there is a really unclear foreign key error if you use an unsupported role.
70+ - This means that the `Roles` object is not getting the right ids
12071 """
12172 remaining_invites = self .invite_limit ().remaining
12273 if remaining_invites == 0 :
12374 raise LabelboxError (
12475 "Invite(s) cannot be sent because you do not have enough available seats in your organization. "
12576 "Please upgrade your account, revoke pending invitations or remove other users."
12677 )
127- for invite in self .invites ():
128- if invite .email == email :
129- raise ValueError (
130- f"Invite already exists for { email } . Please revoke the invite if you want to update the role or resend."
131- )
13278
13379 if not isinstance (role , Role ):
13480 raise TypeError (f"role must be Role type. Found { role } " )
@@ -145,26 +91,35 @@ def invite_user(
14591 f"project_roles must be a list of `ProjectRole`s. Found { project_role } "
14692 )
14793
148- invite_response = self ._assign_user_role ( email , role , _project_roles )
149- return Invite ( self . client , invite_response )
94+ if self .client . get_user (). email == email :
95+ raise ValueError ( "Cannot update your own role" )
15096
151- @beta
152- def user_limit (self ) -> UserLimit :
153- """ Retrieve user limits for the org
97+ data_param = "data"
98+ query_str = """mutation createInvitesPyApi($%s: [CreateInviteInput!]){
99+ createInvites(data: $%s){ invite { id createdAt organizationRoleName inviteeEmail}}}""" % (
100+ data_param , data_param )
154101
155- Returns:
156- UserLimit
157-
158- """
159- query_str = """query UsersLimitPyApi {
160- organization {id account { id usersLimit { dateLimitWasReached remaining used limit }}}}
161- """
162- res = self .client .execute (query_str )
163- return UserLimit (
164- ** {
165- utils .snake_case (k ): v for k , v in res ['organization' ]
166- ['account' ]['usersLimit' ].items ()
167- })
102+ projects = [{
103+ "projectId" : project_role .project .uid ,
104+ "projectRoleId" : project_role .role .uid
105+ } for project_role in _project_roles ]
106+
107+ res = self .client .execute (
108+ query_str ,
109+ {
110+ data_param : [{
111+ "inviterId" : self .client .get_user ().uid ,
112+ "inviteeEmail" : email ,
113+ "organizationId" : self .uid ,
114+ "organizationRoleId" : role .uid ,
115+ "projects" : projects
116+ }]
117+ },
118+ )
119+ # We prob want to return an invite
120+ # Could support bulk ops in the future
121+ invite_response = res ['createInvites' ][0 ]['invite' ]
122+ return Invite (self .client , invite_response )
168123
169124 @beta
170125 def invite_limit (self ) -> InviteLimit :
@@ -177,13 +132,13 @@ def invite_limit(self) -> InviteLimit:
177132
178133 """
179134 org_id_param = "organizationId"
180- res = self .client .execute ("""query InvitesLimitPyApi($%s: ID!) {
135+ res = self .client .execute (
136+ """query InvitesLimitPyApi($%s: ID!) {
181137 invitesLimit(where: {id: $%s}) { used limit remaining }
182138 }""" % (org_id_param , org_id_param ), {org_id_param : self .uid })
183139 return InviteLimit (
184140 ** {utils .snake_case (k ): v for k , v in res ['invitesLimit' ].items ()})
185141
186- @beta
187142 def remove_user (self , user : User ):
188143 """
189144 Deletes a user from the organization. This cannot be undone without sending another invite.
0 commit comments