From b7ce13d903ac02360b7ca93fedb50552fbd8b606 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Thu, 10 Oct 2024 09:27:12 -0400 Subject: [PATCH] Fix field error in card API, reduce queries --- arches/app/views/api/__init__.py | 38 +++++++++++++++++--------------- releases/8.0.0.md | 1 + 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/arches/app/views/api/__init__.py b/arches/app/views/api/__init__.py index 5a40f5f7054..a6e025db76a 100644 --- a/arches/app/views/api/__init__.py +++ b/arches/app/views/api/__init__.py @@ -979,29 +979,29 @@ def get(self, request, conceptid=None): class Card(APIBase): def get(self, request, resourceid): + resource_query = Resource.objects.filter(pk=resourceid).select_related("graph") try: - resource_instance = Resource.objects.get(pk=resourceid) + resource_instance = resource_query.get() graph = resource_instance.graph except Resource.DoesNotExist: graph = models.GraphModel.objects.get(pk=resourceid) resourceid = None resource_instance = None - pass - nodegroups = [] - editable_nodegroups = [] + permitted_nodegroups = [] + editable_nodegroup_ids: set[str] = set() nodes = graph.node_set.all().select_related("nodegroup") for node in nodes: if node.is_collector: added = False if request.user.has_perm("write_nodegroup", node.nodegroup): - editable_nodegroups.append(node.nodegroup) - nodegroups.append(node.nodegroup) + editable_nodegroup_ids.add(str(node.nodegroup.pk)) + permitted_nodegroups.append(node.nodegroup) added = True if not added and request.user.has_perm( "read_nodegroup", node.nodegroup ): - nodegroups.append(node.nodegroup) + permitted_nodegroups.append(node.nodegroup) user_is_reviewer = user_is_resource_reviewer(request.user) @@ -1018,9 +1018,9 @@ def get(self, request, resourceid): ): displayname = _("System Settings") - tiles = resource_instance.tilemodel_set.order_by("sortorder").filter( - nodegroup__in=nodegroups - ) + tiles = resource_instance.tilemodel_set.filter( + nodegroup_id__in=[ng.pk for ng in permitted_nodegroups] + ).order_by("sortorder") provisionaltiles = [] for tile in tiles: append_tile = True @@ -1045,7 +1045,7 @@ def get(self, request, resourceid): # we don't send that tile back to the client. append_tile = False else: - # if the tile has authoritaive data and the current user is not the owner, + # if the tile has authoritative data and the current user is not the owner, # we don't send the provisional data of other users back to the client. tile.provisionaledits = None if append_tile is True: @@ -1071,7 +1071,7 @@ def get(self, request, resourceid): else: cards = ( graph.cardmodel_set.order_by("sortorder") - .filter(nodegroup__in=nodegroups) + .filter(nodegroup__in=permitted_nodegroups) .prefetch_related("cardxnodexwidget_set") ) serialized_cards = JSONSerializer().serializeToPython(cards) @@ -1083,21 +1083,23 @@ def get(self, request, resourceid): ] ] - editable_nodegroup_ids = [ - str(nodegroup.pk) for nodegroup in editable_nodegroups - ] for card in serialized_cards: card["is_writable"] = False - if str(card["nodegroup_id"]) in editable_nodegroup_ids: + if card["nodegroup_id"] in editable_nodegroup_ids: card["is_writable"] = True + permitted_nodes = [ + node + for node in nodes._result_cache + if node.nodegroup in permitted_nodegroups + ] context = { "resourceid": resourceid, "displayname": displayname, "tiles": tiles, "cards": serialized_cards, - "nodegroups": nodegroups, - "nodes": nodes.filter(nodegroup__in=nodegroups), + "nodegroups": permitted_nodegroups, + "nodes": permitted_nodes, "cardwidgets": cardwidgets, "datatypes": models.DDataType.objects.all(), "userisreviewer": user_is_reviewer, diff --git a/releases/8.0.0.md b/releases/8.0.0.md index d8e203ec284..649485bd8e3 100644 --- a/releases/8.0.0.md +++ b/releases/8.0.0.md @@ -14,6 +14,7 @@ Arches 8.0.0 Release Notes - `user_can_edit_resource()` - `user_can_delete_resource()` - `check_resource_instance_permissions()` +- Reduce queries in card API [#11534](https://github.com/archesproject/arches/pull/11534) ### Additional highlights