Skip to content

Commit

Permalink
More efficient & readable code #40
Browse files Browse the repository at this point in the history
  • Loading branch information
johnatawnclementawn committed Oct 10, 2024
1 parent 31c34a4 commit 54b2d7d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
31 changes: 11 additions & 20 deletions arches_references/datatypes/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions arches_references/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 54b2d7d

Please sign in to comment.