Skip to content

Commit

Permalink
feat(hooks): hook relationships, first attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Apr 19, 2024
1 parent 2d95258 commit 581fdae
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
15 changes: 13 additions & 2 deletions arches_orm/arches_django/datatypes/semantic.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import logging
import uuid
from arches.app.models.models import ResourceInstance
from arches.app.models.resource import Resource

from arches_orm.adapter import get_adapter
from arches_orm.view_models import (
WKRI,
SemanticViewModel,
)
from ._register import REGISTER


logger = logging.getLogger(__name__)


@REGISTER("semantic")
def semantic(
tile,
Expand All @@ -19,7 +24,7 @@ def semantic(
child_nodes,
datatype,
):
child_keys = {key: value[1] for key, value in child_nodes.items()}
child_keys = {key: child_value[1] for key, child_value in child_nodes.items()}

def make_pseudo_node(key):
child = parent_cls._make_pseudo_node_cls(
Expand Down Expand Up @@ -74,7 +79,13 @@ def get_child_values(svm):
get_child_values,
)
if value:
svm.update(value)
try:
svm.update(value)
except Exception as exc:
if not get_adapter().config.get("suppress-tile-loading-errors"):
raise exc
elif not get_adapter().config.get("silence-tile-loading-errors"):
logging.warning("Suppressed a tile loading error (tile: %s; node: %s): %s", str(tile), str(node), exc)
svm.get_children()

return svm
Expand Down
46 changes: 40 additions & 6 deletions arches_orm/arches_django/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@
from arches.app.models.tile import Tile
from arches.app.models.models import ResourceXResource, ResourceInstance, GraphModel

from arches_orm.wkrm import get_well_known_resource_model_by_graph_id
from arches_orm.wkrm import get_well_known_resource_model_by_graph_id, attempt_well_known_resource_model


@receiver(post_save, sender=Tile)
def check_resource_instance_on_tile_save(sender, instance, **kwargs):
"""Catch saves on tiles for resources."""
if instance.data:
for key, values in instance.data.items():
print(values, key)
if values:
if not isinstance(values, list):
values = [values]
for value in values:
if value and isinstance(value, dict):
print(value, "VALUE")
if (rXr_id := value.get("resourceXresourceId")) and (rto_id := value.get("resourceId")):
# TODO: inefficient
rto = ResourceInstance.objects.get(resourceinstanceid=rto_id)
if rto:
relationship = ResourceXResource(
resourcexid=rXr_id,
resourceinstanceidfrom=instance.resourceinstance,
resourceinstanceidto=rto,
resourceinstancefrom_graphid=instance.resourceinstance.graph,
resourceinstanceto_graphid=rto.graph
)
if relationship.resourceinstanceto_graphid:
check_related_to(sender, relationship, "relationship-to saved", tile=instance, nodeid=key, **kwargs)
if instance.resourceinstance and instance.resourceinstance.resourceinstanceid:
check_resource_instance(sender, instance, "tile saved", **kwargs)

Expand All @@ -23,7 +45,7 @@ def check_resource_instance_on_tile_delete(sender, instance, **kwargs):

@receiver(post_save, sender=ResourceXResource)
def check_resource_instance_on_related_to_save(sender, instance, **kwargs):
"""Catch deletions on tiles for resources."""
"""Catch saves on tiles for resources."""
if instance.resourceinstanceto_graphid:
check_related_to(sender, instance, "relationship-to saved", **kwargs)

Expand All @@ -33,7 +55,7 @@ def check_resource_instance_on_related_to_delete(sender, instance, **kwargs):
if instance.resourceinstanceto_graphid:
check_related_to(sender, instance, "relationship-to deleted", **kwargs)

def check_related_to(sender: type[ResourceInstance], instance: ResourceXResource, reason: str, **kwargs: Any) -> None:
def check_related_to(sender: type[ResourceInstance], instance: ResourceXResource, reason: str, tile = None, nodeid = None, **kwargs: Any) -> None:
graph_id = (
instance.resourceinstanceto_graphid.graphid
if isinstance(instance.resourceinstanceto_graphid, GraphModel) else
Expand All @@ -43,10 +65,22 @@ def check_related_to(sender: type[ResourceInstance], instance: ResourceXResource
model_cls = get_well_known_resource_model_by_graph_id(
graph_id
)
if model_cls and model_cls.post_related_to.has_listeners():
resource_instance = model_cls.from_resource_instance(instance.resourceinstanceidto)
if model_cls and model_cls.post_related_to.has_listeners() and instance.resourceinstanceidto:
resource_instance_to = model_cls.from_resource_instance(instance.resourceinstanceidto)
resource_instance_from = None
graph_id_from = (
instance.resourceinstancefrom_graphid.graphid
if isinstance(instance.resourceinstancefrom_graphid, GraphModel) else
instance.resourceinstancefrom_graphid.graphid
)
if graph_id_from:
model_cls_from = get_well_known_resource_model_by_graph_id(
graph_id_from
)
if model_cls_from and instance.resourceinstanceidfrom:
resource_instance_from = model_cls_from.from_resource_instance(instance.resourceinstanceidfrom)
model_cls.post_related_to.send(
model_cls, relationship=resource_instance, reason=reason, tile=instance
model_cls, resource_instance_to=resource_instance_to, resource_instance_from=resource_instance_from, relationship=instance, reason=reason, tile=tile, nodeid=nodeid
)


Expand Down
3 changes: 2 additions & 1 deletion arches_orm/arches_django/pseudo_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def _update_value(self):
else:
data = self._value

print("DATA", data, self.node.datatype, self.node.nodeid, self.tile.data)
self._value, self._as_tile_data, self._datatype, self._multiple = get_view_model_for_datatype(
self.tile,
self.node,
Expand All @@ -168,7 +169,7 @@ def _update_value(self):
parent_cls=self._parent_cls,
child_nodes=self._child_nodes,
)
if self._value is not None:
if self._value is not None and isinstance(self._value, ViewModel):
self._value._parent_pseudo_node = self
if self._value is not None:
self._value_loaded = True
Expand Down
14 changes: 2 additions & 12 deletions arches_orm/view_models/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@


class RelatedResourceInstanceViewModelMixin(ViewModel):
"""Wraps a resource instance.
Subclasses str, so it can be handled like a string enum, but keeps
the `.value`, `.lang` and `.text` properties cached, so you can
find out more.
"""
"""Wraps a resource instance."""


class RelatedResourceInstanceListViewModel(UserList, ViewModel):
"""Wraps a concept list, allowing interrogation.
Subclasses list, so its members can be handled like a string enum, but keeps
the `.value`, `.lang` and `.text` properties cached, so you can
find out more.
"""
"""Wraps a resource list, allowing interrogation."""

def __init__(
self,
Expand Down

0 comments on commit 581fdae

Please sign in to comment.