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

Ingest string values and perform listitem lookup #40 #41

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading