From fd77699cdfec87170f251182c202440192713168 Mon Sep 17 00:00:00 2001 From: Christopher Byrd Date: Wed, 13 Nov 2024 21:49:54 -0800 Subject: [PATCH] updates relateable resources logic #11570 --- .../app/media/js/viewmodels/graph-settings.js | 9 ++- arches/app/models/models.py | 68 ++++++++++--------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/arches/app/media/js/viewmodels/graph-settings.js b/arches/app/media/js/viewmodels/graph-settings.js index d681a234bd6..1d79e661d74 100644 --- a/arches/app/media/js/viewmodels/graph-settings.js +++ b/arches/app/media/js/viewmodels/graph-settings.js @@ -15,9 +15,12 @@ define([ self.resource_data = ko.observableArray([]); self.relatable_resources = ko.computed(function() { - return _.each(self.resource_data(), function(resource) { - resource.isRelatable = ko.observable(resource.is_relatable); - }).filter(resource => !resource.graph.source_identifier_id); + return _.each( + self.resource_data().sort((a, b) => a.graph.name.localeCompare(b.graph.name)), + function(resource) { + resource.isRelatable = ko.observable(resource.is_relatable); + } + ).filter(resource => !resource.graph.source_identifier_id); }); self.designerViewModel = params.designerViewModel; diff --git a/arches/app/models/models.py b/arches/app/models/models.py index 01de50bdd3f..bd5217d1bf7 100644 --- a/arches/app/models/models.py +++ b/arches/app/models/models.py @@ -855,61 +855,63 @@ def is_collector(self): ) def get_relatable_resources(self): - primary_id = self.source_identifier_id or self.nodeid - - relatable_resources = Resource2ResourceConstraint.objects.filter( - Q(resourceclassto_id=primary_id) | Q(resourceclassfrom_id=primary_id) + query_id = ( + self.source_identifier_id if self.source_identifier_id else self.nodeid ) - unique_ids = set() - for r2r in relatable_resources: - if r2r.resourceclassfrom is not None: - unique_ids.add(r2r.resourceclassfrom) - if r2r.resourceclassto is not None: - unique_ids.add(r2r.resourceclassto) + constraints = Resource2ResourceConstraint.objects.filter( + Q(resourceclassto_id=query_id) | Q(resourceclassfrom_id=query_id) + ).select_related("resourceclassfrom", "resourceclassto") + + filtered_constraints = set() + for r2r in constraints: + if r2r.resourceclassto_id == query_id and r2r.resourceclassfrom is not None: + filtered_constraints.add(r2r.resourceclassfrom) + elif ( + r2r.resourceclassfrom_id == query_id and r2r.resourceclassto is not None + ): + filtered_constraints.add(r2r.resourceclassto) - return list(unique_ids) + return list(filtered_constraints) def set_relatable_resources(self, new_ids): new_ids = set(new_ids) - old_ids = set() + old_ids = set() for res in self.get_relatable_resources(): - if res.source_identifier_id: + if res.source_identifier_id is not None: old_ids.add(res.source_identifier_id) - if res.nodeid: + if res.nodeid is not None: old_ids.add(res.nodeid) - self_ids = set() - if self.source_identifier_id: - self_ids.add(self.source_identifier_id) - if self.nodeid: - self_ids.add(self.nodeid) - - old_ids.discard(None) - self_ids.discard(None) + self_ids = set( + id for id in (self.source_identifier_id, self.nodeid) if id is not None + ) - old_ids_to_delete = old_ids - new_ids - new_ids_to_create = new_ids - old_ids + ids_to_delete = old_ids - new_ids + ids_to_create = new_ids - old_ids - if old_ids_to_delete and self_ids: + if ids_to_delete and self_ids: Resource2ResourceConstraint.objects.filter( ( Q(resourceclassto_id__in=self_ids) - & Q(resourceclassfrom_id__in=old_ids_to_delete) + & Q(resourceclassfrom_id__in=ids_to_delete) ) | ( - Q(resourceclassto_id__in=old_ids_to_delete) + Q(resourceclassto_id__in=ids_to_delete) & Q(resourceclassfrom_id__in=self_ids) ) ).delete() - for new_id in new_ids_to_create: - new_r2r = Resource2ResourceConstraint( - resourceclassfrom_id=self.source_identifier_id or self.nodeid, - resourceclassto_id=new_id, - ) - new_r2r.save() + if ids_to_create: + new_constraints = [ + Resource2ResourceConstraint( + resourceclassfrom_id=self.source_identifier_id or self.nodeid, + resourceclassto_id=id_to_create, + ) + for id_to_create in ids_to_create + ] + Resource2ResourceConstraint.objects.bulk_create(new_constraints) def serialize(self, fields=None, exclude=None, **kwargs): ret = JSONSerializer().handle_model(