Skip to content

Commit

Permalink
Ingest string values and perform listitem lookup #40
Browse files Browse the repository at this point in the history
  • Loading branch information
johnatawnclementawn committed Oct 9, 2024
1 parent d7a338b commit 31c34a4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
23 changes: 23 additions & 0 deletions arches_references/datatypes/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

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


class ReferenceDataType(BaseDataType):
Expand Down Expand Up @@ -76,8 +79,28 @@ 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()]
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
)
return list_item_value_choice.list_item

def clean(self, tile, nodeid):
super().clean(tile, nodeid)
if tile.data[nodeid] == []:
Expand Down
10 changes: 9 additions & 1 deletion arches_references/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ def serialize(self, depth_map=None, flat=False):
)
return data

def build_tile_value(self):
tile_value = {
"uri": self.uri,
"labels": [value.serialize() for value in self.list_item_values.labels()],
"listid": str(self.id),
}
return tile_value


class ListItemValue(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Expand Down Expand Up @@ -274,7 +282,7 @@ def serialize(self):
"valuetype_id": self.valuetype_id,
"language_id": self.language_id,
"value": self.value,
"list_item_id": self.list_item_id,
"list_item_id": str(self.list_item_id),
}

def delete(self):
Expand Down
3 changes: 3 additions & 0 deletions arches_references/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class ListItemValueQuerySet(models.QuerySet):
def values_without_images(self):
return self.exclude(valuetype="image")

def labels(self):
return self.filter(valuetype__category="label")

def images(self):
return self.filter(valuetype="image")

Expand Down

0 comments on commit 31c34a4

Please sign in to comment.