Skip to content

Commit 033906a

Browse files
authored
Merge pull request #907 from Labelbox/pno/attachment_name
Add attachment_name support to create_attachment()
2 parents 2c688a7 + 385ff1c commit 033906a

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

labelbox/schema/data_row.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ def get_winning_label_id(self, project_id: str) -> Optional[str]:
106106

107107
return res["dataRow"]["labelingActivity"]["selectedLabelId"]
108108

109-
def create_attachment(self, attachment_type,
110-
attachment_value) -> "AssetAttachment":
109+
def create_attachment(self,
110+
attachment_type,
111+
attachment_value,
112+
attachment_name=None) -> "AssetAttachment":
111113
""" Adds an AssetAttachment to a DataRow.
112114
Labelers can view these attachments while labeling.
113115
@@ -117,6 +119,7 @@ def create_attachment(self, attachment_type,
117119
attachment_type (str): Asset attachment type, must be one of:
118120
VIDEO, IMAGE, TEXT, IMAGE_OVERLAY (AssetAttachment.AttachmentType)
119121
attachment_value (str): Asset attachment value.
122+
attachment_name (str): (Optional) Asset attachment name.
120123
Returns:
121124
`AssetAttachment` DB object.
122125
Raises:
@@ -126,19 +129,23 @@ def create_attachment(self, attachment_type,
126129

127130
attachment_type_param = "type"
128131
attachment_value_param = "value"
132+
attachment_name_param = "name"
129133
data_row_id_param = "dataRowId"
134+
130135
query_str = """mutation CreateDataRowAttachmentPyApi(
131-
$%s: AttachmentType!, $%s: String!, $%s: ID!) {
136+
$%s: AttachmentType!, $%s: String!, $%s: String, $%s: ID!) {
132137
createDataRowAttachment(data: {
133-
type: $%s value: $%s dataRowId: $%s}) {%s}} """ % (
134-
attachment_type_param, attachment_value_param, data_row_id_param,
135-
attachment_type_param, attachment_value_param, data_row_id_param,
138+
type: $%s value: $%s name: $%s dataRowId: $%s}) {%s}} """ % (
139+
attachment_type_param, attachment_value_param,
140+
attachment_name_param, data_row_id_param, attachment_type_param,
141+
attachment_value_param, attachment_name_param, data_row_id_param,
136142
query.results_query_part(Entity.AssetAttachment))
137143

138144
res = self.client.execute(
139145
query_str, {
140146
attachment_type_param: attachment_type,
141147
attachment_value_param: attachment_value,
148+
attachment_name_param: attachment_name,
142149
data_row_id_param: self.uid
143150
})
144151
return Entity.AssetAttachment(self.client,

tests/integration/test_data_rows.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def test_lookup_data_rows(client, dataset):
133133
assert all([len(x) == 1 for x in lookup.values()])
134134
assert lookup[uid][0] == dr.uid
135135
assert lookup[uid2][0] == dr2.uid
136-
#1 external id : 2 uid
136+
# 1 external id : 2 uid
137137
dr3 = dataset.create_data_row(row_data="123", external_id=uid2)
138138
lookup = client.get_data_row_ids_for_external_ids([uid2])
139139
assert len(lookup) == 1
@@ -621,16 +621,21 @@ def test_data_row_iteration(dataset, image_url) -> None:
621621

622622

623623
def test_data_row_attachments(dataset, image_url):
624-
attachments = [("IMAGE", image_url), ("TEXT", "test-text"),
625-
("IMAGE_OVERLAY", image_url), ("HTML", image_url)]
624+
attachments = [("IMAGE", image_url, "attachment image"),
625+
("TEXT", "test-text", None),
626+
("IMAGE_OVERLAY", image_url, "Overlay"),
627+
("HTML", image_url, None)]
626628
task = dataset.create_data_rows([{
627-
"row_data": image_url,
628-
"external_id": "test-id",
629+
"row_data":
630+
image_url,
631+
"external_id":
632+
"test-id",
629633
"attachments": [{
630634
"type": attachment_type,
631-
"value": attachment_value
635+
"value": attachment_value,
636+
"name": attachment_name
632637
}]
633-
} for attachment_type, attachment_value in attachments])
638+
} for attachment_type, attachment_value, attachment_name in attachments])
634639

635640
task.wait_till_done()
636641
assert task.status == "COMPLETE"
@@ -652,8 +657,10 @@ def test_data_row_attachments(dataset, image_url):
652657

653658

654659
def test_create_data_rows_sync_attachments(dataset, image_url):
655-
attachments = [("IMAGE", image_url), ("TEXT", "test-text"),
656-
("IMAGE_OVERLAY", image_url), ("HTML", image_url)]
660+
attachments = [("IMAGE", image_url, "image URL"),
661+
("TEXT", "test-text", None),
662+
("IMAGE_OVERLAY", image_url, "Overlay"),
663+
("HTML", image_url, None)]
657664
attachments_per_data_row = 3
658665
dataset.create_data_rows_sync([{
659666
"row_data":
@@ -662,9 +669,10 @@ def test_create_data_rows_sync_attachments(dataset, image_url):
662669
"test-id",
663670
"attachments": [{
664671
"type": attachment_type,
665-
"value": attachment_value
672+
"value": attachment_value,
673+
"name": attachment_name
666674
} for _ in range(attachments_per_data_row)]
667-
} for attachment_type, attachment_value in attachments])
675+
} for attachment_type, attachment_value, attachment_name in attachments])
668676
data_rows = list(dataset.data_rows())
669677
assert len(data_rows) == len(attachments)
670678
for data_row in data_rows:
@@ -685,12 +693,24 @@ def test_create_data_rows_sync_mixed_upload(dataset, image_url):
685693

686694
def test_delete_data_row_attachment(datarow, image_url):
687695
attachments = []
696+
697+
# Anonymous attachment
688698
to_attach = [("IMAGE", image_url), ("TEXT", "test-text"),
689699
("IMAGE_OVERLAY", image_url), ("HTML", image_url)]
690700
for attachment_type, attachment_value in to_attach:
691701
attachments.append(
692702
datarow.create_attachment(attachment_type, attachment_value))
693703

704+
# Attachment with a name
705+
to_attach = [("IMAGE", image_url, "Att. Image"),
706+
("TEXT", "test-text", "Att. Text"),
707+
("IMAGE_OVERLAY", image_url, "Image Overlay"),
708+
("HTML", image_url, "Att. HTML")]
709+
for attachment_type, attachment_value, attachment_name in to_attach:
710+
attachments.append(
711+
datarow.create_attachment(attachment_type, attachment_value,
712+
attachment_name))
713+
694714
for attachment in attachments:
695715
attachment.delete()
696716

@@ -914,7 +934,8 @@ def test_create_tiled_layer(dataset, tile_content):
914934
**tile_content, 'media_type': 'TMS_SIMPLE'
915935
},
916936
tile_content,
917-
tile_content['row_data'] # Old way to check for backwards compatibility
937+
# Old way to check for backwards compatibility
938+
tile_content['row_data']
918939
]
919940
dataset.create_data_rows_sync(examples)
920941
data_rows = list(dataset.data_rows())

0 commit comments

Comments
 (0)