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

11583 leverage cache for related resource relationship preflabel lookup #11584

Merged
merged 9 commits into from
Nov 15, 2024
16 changes: 13 additions & 3 deletions arches/app/models/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ def get_relations(

ret["total"] = {"value": resource_relations["total"]}
instanceids = set()
preflabel_lookup = dict()

readable_graphids = set(
permission_backend.get_resource_types_by_perm(
Expand Down Expand Up @@ -901,9 +902,18 @@ def get_relations(
and str(resourceinstancefrom_graphid) in readable_graphids
):
try:
preflabel = get_preflabel_from_valueid(
relation["relationshiptype"], lang
)
if f'{relation["relationshiptype"]}{lang}' in preflabel_lookup:
preflabel = preflabel_lookup[
f'{relation["relationshiptype"]}{lang}'
]
else:
preflabel = get_preflabel_from_valueid(
relation["relationshiptype"], lang
)
preflabel_lookup[f'{relation["relationshiptype"]}{lang}'] = (
preflabel
)

relation["relationshiptype_label"] = preflabel["value"] or ""
except:
relation["relationshiptype_label"] = (
Expand Down
59 changes: 58 additions & 1 deletion tests/views/resource_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import os
from unittest.mock import MagicMock, patch
from arches.app.views.resource import ResourcePermissionDataView
from tests.base_test import ArchesTestCase
from django.urls import reverse
from arches.app.models.models import EditLog, ResourceInstance
from arches.app.utils.betterJSONSerializer import JSONSerializer
from arches.app.models.resource import Resource
from arches.app.models.tile import Tile
from tests.utils.search_test_utils import sync_es
from arches.app.search.search_engine_factory import SearchEngineFactory
from arches.app.utils.betterJSONSerializer import JSONSerializer, JSONDeserializer
from arches.app.utils.data_management.resource_graphs.importer import (
import_graph as ResourceGraphImporter,
)
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from guardian.shortcuts import (
Expand All @@ -39,6 +47,9 @@ class CommandLineTests(ArchesTestCase):
graph_fixtures = ["Data_Type_Model"]
data_type_graphid = "330802c5-95bd-11e8-b7ac-acde48001122"
resource_instance_id = "f562c2fa-48d3-4798-a723-10209806c068"
reference_graphid = "e3d4505e-bfa7-11e9-b4dc-0242ac160002"
reference_nodeid = "fc3c8080-bfa7-11e9-b4dc-0242ac160002"
person_graphid = "0c6269e8-bfa8-11e9-bd39-0242ac160002"

@classmethod
def setUpTestData(cls):
Expand All @@ -54,6 +65,19 @@ def setUpTestData(cls):
edit.userid = user.id
edit.save()

with open(
os.path.join("tests/fixtures/jsonld_base/models/4564-referenced.json"),
"r",
) as f:
archesfile = JSONDeserializer().deserialize(f)
ResourceGraphImporter(archesfile["graph"])
with open(
os.path.join("tests/fixtures/jsonld_base/models/4564-person.json"),
"r",
) as f:
archesfile = JSONDeserializer().deserialize(f)
ResourceGraphImporter(archesfile["graph"])

jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
def test_resource_instance_permission_assignment(self):
"""
Test that we can assign resource instance permissions
Expand Down Expand Up @@ -314,3 +338,36 @@ def test_crud_allowed_if_no_explicit_permissions(self):
self.assertEqual(view.status_code, 200)
self.assertEqual(edit.status_code, 200)
self.assertEqual(delete.status_code, 200)

def test_get_related_resource(self):
se = SearchEngineFactory().create()
user = User.objects.get(username="admin")
is_related_to_valueid = "ac41d9be-79db-4256-b368-2f4559cfbe55"
en_preflabel = "is related to"
person_resourceid = "b6754e7a-7f18-40d1-93fe-61763d37d55e"
person_resource = Resource(
graph_id=self.person_graphid, resourceinstanceid=person_resourceid
)
person_resource.save()

reference_resourceid = "380b8364-50e6-4f5b-af08-0ea3ab56a406"
reference_resource = Resource(
graph_id=self.reference_graphid, resourceinstanceid=reference_resourceid
)
reference_resource.save()
reference_tile = Tile.get_blank_tile(
self.reference_nodeid, reference_resourceid
)
reference_tile.data[self.reference_nodeid] = [
{
"resourceName": "",
"ontologyProperty": is_related_to_valueid,
"inverseOntologyProperty": is_related_to_valueid,
"resourceId": person_resourceid,
}
]
reference_tile.save()
sync_es(se)
ret = reference_resource.get_related_resources(user=user)
relationship = ret["resource_relationships"][0]["relationshiptype_label"]
self.assertEqual(relationship, en_preflabel)