Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against null values #42 #43

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions arches_references/datatypes/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ def get_display_value(self, tile, node, **kwargs):
labels = []
requested_language = kwargs.pop("language", None)
current_language = requested_language or get_language()
for item in self.get_tile_data(tile)[str(node.nodeid)]:
for label in item["labels"]:
if (
label["language_id"] == current_language
and label["valuetype_id"] == "prefLabel"
):
labels.append(label.get("value", ""))
node_data = self.get_tile_data(tile)[str(node.nodeid)]
if node_data:
for item in node_data:
for label in item["labels"]:
if (
label["language_id"] == current_language
and label["valuetype_id"] == "prefLabel"
):
labels.append(label.get("value", ""))
return ", ".join(labels)

def collects_multiple_values(self):
Expand Down
2 changes: 1 addition & 1 deletion arches_references/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def build_tile_value(self):
tile_value = {
"uri": self.uri or self.generate_uri(),
"labels": [label.serialize() for label in self.list_item_values.labels()],
"listid": str(self.id),
"listid": str(self.list_id),
}
return tile_value

Expand Down
55 changes: 55 additions & 0 deletions tests/reference_datatype_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from arches.app.models.tile import Tile
from arches_references.models import List, ListItem, ListItemValue
from django.test import TestCase
from types import SimpleNamespace

# these tests can be run from the command line via
# python manage.py test tests.reference_datatype_tests --settings="tests.test_settings"
Expand Down Expand Up @@ -123,3 +124,57 @@ def test_transform_value_for_tile(self):
self.assertEqual(
tile_value2[0]["labels"][0]["list_item_id"], expected_list_item_pk
)

def test_get_display_value(self):
reference = DataTypeFactory().get_instance("reference")
mock_node = SimpleNamespace(nodeid="72048cb3-adbc-11e6-9ccf-14109fd34195")
mock_tile1 = Tile(
{
"resourceinstance_id": "40000000-0000-0000-0000-000000000000",
"parenttile_id": "",
"nodegroup_id": "72048cb3-adbc-11e6-9ccf-14109fd34195",
"tileid": "",
"data": {
"72048cb3-adbc-11e6-9ccf-14109fd34195": [
{
"uri": "https://rdm.dev.fargeo.com/plugins/controlled-list-manager/item/9baf3cd5-33d4-4fbc-b1d1-a2d218732f1e",
"labels": [
{
"id": "ea5c8af7-9933-4356-b537-0330c9da4690",
"value": "identifier",
"language_id": "en",
"list_item_id": "d8ba08f9-b265-4288-9412-857c77fe2581",
"valuetype_id": "prefLabel",
},
{
"id": "ea5c8af7-9933-4356-b537-0330c9da4690",
"value": "identificateur",
"language_id": "fr",
"list_item_id": "d8ba08f9-b265-4288-9412-857c77fe2692",
"valuetype_id": "prefLabel",
},
],
"listid": "a8da34eb-575b-498c-ada7-161ee745fd16",
}
]
},
}
)
self.assertEqual(
reference.get_display_value(mock_tile1, mock_node), "identifier"
)
self.assertEqual(
reference.get_display_value(mock_tile1, mock_node, language="fr"),
"identificateur",
)

mock_tile2 = Tile(
{
"resourceinstance_id": "50000000-0000-0000-0000-000000000000",
"parenttile_id": "",
"nodegroup_id": "72048cb3-adbc-11e6-9ccf-14109fd34195",
"tileid": "",
"data": {"72048cb3-adbc-11e6-9ccf-14109fd34195": None},
}
)
self.assertEqual(reference.get_display_value(mock_tile2, mock_node), "")