From 54b2d7db76b24d4c25c6b1d4af93c8b42c7444d7 Mon Sep 17 00:00:00 2001 From: Johnathan Clementi Date: Thu, 10 Oct 2024 12:11:16 -0400 Subject: [PATCH] More efficient & readable code #40 --- arches_references/datatypes/datatypes.py | 31 +++++++++--------------- arches_references/models.py | 6 ++--- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/arches_references/datatypes/datatypes.py b/arches_references/datatypes/datatypes.py index 039d4f5..b1078f4 100644 --- a/arches_references/datatypes/datatypes.py +++ b/arches_references/datatypes/datatypes.py @@ -4,9 +4,8 @@ from arches.app.datatypes.base import BaseDataType from arches.app.models.graph import GraphValidationError -from arches.app.models.models import Node -from arches_references.models import ListItem, ListItemValue +from arches_references.models import ListItem, ListItemValue, NodeProxy class ReferenceDataType(BaseDataType): @@ -79,27 +78,19 @@ def validate( return errors def transform_value_for_tile(self, value, **kwargs): - controlled_list = kwargs.get("controlledList") - nodeid = kwargs.get("nodeid") - config = {"controlledList": controlled_list, "nodeid": nodeid} - if type(value) == str and config: - value = [self.lookup_listitem_from_label(value, config).build_tile_value()] + list_id = kwargs.get("controlledList") + if isinstance(value, str): + found_item = self.lookup_listitem_from_label(value, list_id) + if found_item: + value = [found_item.build_tile_value()] return value - def lookup_listitem_from_label(self, value, config): - if "controlledList" in config: - list_id = config["controlledList"] - elif "nodeid" in config: - nodeid = config["nodeid"] - if nodeid not in self.listitems_by_list_lookup: - controlled_list = Node.objects.get(nodeid=nodeid).config[ - "controlledList" - ] - list_items_choices = ListItem.objects.filter(list_id=list_id) - list_item_value_choice = ListItemValue.objects.get( - list_item_id__in=list_items_choices, value=value + def lookup_listitem_from_label(self, value, list_id): + return ( + ListItem.objects.filter(list_id=list_id, list_item_values__value=value) + .order_by("sortorder") + .first() ) - return list_item_value_choice.list_item def clean(self, tile, nodeid): super().clean(tile, nodeid) diff --git a/arches_references/models.py b/arches_references/models.py index 012095c..be92282 100644 --- a/arches_references/models.py +++ b/arches_references/models.py @@ -213,8 +213,8 @@ def serialize(self, depth_map=None, flat=False): def build_tile_value(self): tile_value = { - "uri": self.uri, - "labels": [value.serialize() for value in self.list_item_values.labels()], + "uri": self.uri or self.generate_uri(), + "labels": [label.serialize() for label in self.list_item_values.labels()], "listid": str(self.id), } return tile_value @@ -321,7 +321,7 @@ class Meta: def serialize(self): return { "id": str(self.id), - "list_item_id": self.list_item_id, + "list_item_id": str(self.list_item_id), "url": self.value.url, "metadata": [ metadata.serialize() for metadata in self.list_item_image_metadata.all()