Skip to content

Commit 90708e9

Browse files
committed
better structure
1 parent f0cdbbd commit 90708e9

File tree

6 files changed

+63
-53
lines changed

6 files changed

+63
-53
lines changed

labelbox/client.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -906,27 +906,3 @@ def create_feature_schema(self, normalized):
906906
# But the features are the same so we just grab the feature schema id
907907
res['id'] = res['normalized']['featureSchemaId']
908908
return Entity.FeatureSchema(self, res)
909-
910-
def create_resource_tag(self, tag=None):
911-
"""
912-
Creates a resource tag.
913-
>>> tag = {'text': 'tag-1', 'color': 'ffffff'}
914-
915-
Args:
916-
tag (dict): A resource tag {'text': 'tag-1', 'color': 'fffff'}
917-
Returns:
918-
The created resource tag.
919-
"""
920-
tag_text_param = "text"
921-
tag_color_param = "color"
922-
923-
query_str = """mutation CreateResourceTagPyApi($text:String!,$color:String!) {
924-
createResourceTag(input:{text:$%s,color:$%s}) {id,text,color}}
925-
""" % (tag_text_param, tag_color_param)
926-
927-
params = {
928-
tag_text_param: tag.get("text", ""),
929-
tag_color_param: tag.get("color", "")
930-
}
931-
res = self.execute(query_str, params)
932-
return Entity.ResourceTag(self, res['createResourceTag'])

labelbox/schema/organization.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from labelbox.orm.db_object import DbObject, query, Entity
77
from labelbox.orm.model import Field, Relationship
88
from labelbox.schema.invite import InviteLimit
9+
from labelbox.schema.resource_tag import ResourceTag
910

1011
if TYPE_CHECKING:
1112
from labelbox import Role, User, ProjectRole, Invite, InviteLimit, IAMIntegration
@@ -43,6 +44,7 @@ def __init__(self, *args, **kwargs):
4344
users = Relationship.ToMany("User", False)
4445
projects = Relationship.ToMany("Project", True)
4546
webhooks = Relationship.ToMany("Webhook", False)
47+
resourceTags = Relationship.ToMany("ResourceTag", False)
4648

4749
def invite_user(
4850
self,
@@ -127,13 +129,42 @@ def remove_user(self, user: "User") -> None:
127129
updateUser(where: {id: $%s}, data: {deleted: true}) { id deleted }
128130
}""" % (user_id_param, user_id_param), {user_id_param: user.uid})
129131

130-
def get_resource_tags(self) -> List["ResourceTag"]:
132+
def create_resource_tag(self, tag=None) -> ResourceTag:
133+
"""
134+
Creates a resource tag.
135+
>>> tag = {'text': 'tag-1', 'color': 'ffffff'}
136+
137+
Args:
138+
tag (dict): A resource tag {'text': 'tag-1', 'color': 'fffff'}
139+
Returns:
140+
The created resource tag.
141+
"""
142+
tag_text_param = "text"
143+
tag_color_param = "color"
144+
145+
query_str = """mutation CreateResourceTagPyApi($text:String!,$color:String!) {
146+
createResourceTag(input:{text:$%s,color:$%s}) {%s}}
147+
""" % (tag_text_param, tag_color_param,
148+
query.results_query_part(ResourceTag))
149+
150+
params = {
151+
tag_text_param: tag.get("text", ""),
152+
tag_color_param: tag.get("color", "")
153+
}
154+
res = self.client.execute(query_str, params)
155+
return ResourceTag(self.client, res['createResourceTag'])
156+
157+
def get_resource_tags(self) -> List[ResourceTag]:
131158
"""
132159
Returns all resource tags for an organization
133160
"""
134-
res = self.client.execute("""query GetOrganizationResourceTagsPyApi{organization{resourceTag{id,text,color}}}""")
161+
query_str = """query GetOrganizationResourceTagsPyApi{organization{resourceTag{%s}}}""" % (
162+
query.results_query_part(ResourceTag))
135163

136-
return res['organization']['resourceTag']
164+
return [
165+
ResourceTag(self.client, resourceTag)
166+
for resourceTag in res['createResourceTag']
167+
]
137168

138169
def get_iam_integrations(self) -> List["IAMIntegration"]:
139170
"""

labelbox/schema/project.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from labelbox.orm.db_object import DbObject, Updateable, Deletable
1919
from labelbox.orm.model import Entity, Field, Relationship
2020
from labelbox.pagination import PaginatedCollection
21+
from labelbox.schema.resource_tag import ResourceTag
2122

2223
if TYPE_CHECKING:
2324
from labelbox import BulkImportRequest
@@ -121,7 +122,8 @@ def members(self) -> PaginatedCollection:
121122
{id_param: str(self.uid)},
122123
["project", "members"], ProjectMember)
123124

124-
def create_project_resource_tags(self, resource_tag_ids: List[str]) -> List[str]:
125+
def update_project_resource_tags(
126+
self, resource_tag_ids: List[str]) -> List[ResourceTag]:
125127
""" Creates project resource tags
126128
127129
Args:
@@ -132,20 +134,15 @@ def create_project_resource_tags(self, resource_tag_ids: List[str]) -> List[str]
132134
project_id_param = "projectId"
133135
tag_ids_param = "resourceTagIds"
134136

135-
query_str = """mutation AttatchProjectResourceTagsPyApi($%s:ID!,$%s:[String!]) {
136-
project(where:{id:$%s}){updateProjectResourceTags(input:{%s:$%s}){id}}}""" % (
137-
project_id_param,
138-
tag_ids_param,
139-
project_id_param,
140-
tag_ids_param,
141-
tag_ids_param
142-
)
143-
144-
res = self.client.execute(
145-
query_str, {
146-
project_id_param: self.uid,
147-
tag_ids_param: resource_tag_ids
148-
})
137+
query_str = """mutation CreateProjectResourceTagsPyApi($%s:ID!,$%s:[String!]) {
138+
project(where:{id:$%s}){updateProjectResourceTags(input:{%s:$%s}){%s}}}""" % (
139+
project_id_param, tag_ids_param, project_id_param, tag_ids_param,
140+
tag_ids_param, query.results_query_part(ResourceTag))
141+
142+
res = self.client.execute(query_str, {
143+
project_id_param: self.uid,
144+
tag_ids_param: resource_tag_ids
145+
})
149146

150147
return res["project"]["updateProjectResourceTags"]
151148

labelbox/schema/project_resource_tag.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from labelbox.orm.db_object import DbObject, Updateable
22
from labelbox.orm.model import Field, Relationship
33

4+
45
class ProjectResourceTag(DbObject, Updateable):
56
""" Project resource tag to associate ProjectResourceTag to Project.
67

labelbox/schema/resource_tag.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from labelbox.orm.db_object import DbObject, Updateable
22
from labelbox.orm.model import Field, Relationship
33

4+
45
class ResourceTag(DbObject, Updateable):
56
""" Resource tag to label and identify your labelbox resources easier.
67

tests/integration/test_project.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ def test_project(client, rand_gen):
4242
assert project not in final
4343
assert set(final) == set(before)
4444

45-
def test_create_project_resource_tags(client, rand_gen):
45+
46+
def test_update_project_resource_tags(client, rand_gen):
4647
before = list(client.get_projects())
4748
for o in before:
4849
assert isinstance(o, Project)
4950

51+
org = client.get_organization()
52+
assert org.uid is not None
53+
54+
project_name = rand_gen(str)
55+
p1 = client.create_project(name=project_name)
56+
assert p1.uid is not None
57+
5058
colorA = "#ffffff"
5159
textA = rand_gen(str)
5260
tag = {"text": textA, "color": colorA}
@@ -55,35 +63,31 @@ def test_create_project_resource_tags(client, rand_gen):
5563
textB = rand_gen(str)
5664
tagB = {"text": textB, "color": colorB}
5765

58-
project_name = rand_gen(str)
59-
60-
tagA = client.create_resource_tag(tag)
66+
tagA = client.get_organization().create_resource_tag(tag)
6167
assert tagA.text == textA
6268
assert '#' + tagA.color == colorA
6369
assert tagA.uid is not None
6470

65-
org = client.get_organization()
6671
tags = org.get_resource_tags()
6772
lenA = len(tags)
6873
assert lenA > 0
6974

70-
tagB = client.create_resource_tag(tagB)
75+
tagB = client.get_organization().create_resource_tag(tagB)
7176
assert tagB.text == textB
7277
assert '#' + tagB.color == colorB
7378
assert tagB.uid is not None
7479

75-
tags = org.get_resource_tags()
80+
tags = client.get_organization().get_resource_tags()
7681
lenB = len(tags)
7782
assert lenB > 0
7883
assert lenB > lenA
7984

80-
p1 = client.create_project(name=project_name)
81-
assert p1.uid is not None
82-
83-
project_resource_tag = p1.create_project_resource_tags([tagA.uid])
85+
project_resource_tag = client.get_project(
86+
p1.uid).update_project_resource_tags([str(tagA.uid)])
8487
assert len(project_resource_tag) == 1
8588
assert project_resource_tag[0].get("id") == tagA.uid
8689

90+
8791
def test_project_filtering(client, rand_gen):
8892
name_1 = rand_gen(str)
8993
name_2 = rand_gen(str)

0 commit comments

Comments
 (0)