Skip to content

Commit

Permalink
feat(dev): org dict cache, test fix, new var name;
Browse files Browse the repository at this point in the history
- Renamed `all_translated` to `translated`.
- Added a class cache variable for org dicts.
- Updated org test for serializing.
  • Loading branch information
JVickery-TBS committed Nov 8, 2023
1 parent ab55002 commit 1d1b155
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
34 changes: 22 additions & 12 deletions ckanext/dcat/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def __init__(self, graph, compatibility_mode=False):
# _license().
self._licenceregister_cache = None

# Cache for Organizations
self._org_cache = None

def _datasets(self):
'''
Generator that returns all DCAT datasets on the graph
Expand Down Expand Up @@ -728,15 +731,15 @@ def _add_list_triples_from_dict(self, _dict, subject, items):
def _add_triples_from_dict(self, _dict, subject, items,
list_value=False,
date_value=False,
all_translated=False):
translated=False):
for item in items:
key, predicate, fallbacks, _type = item
self._add_triple_from_dict(_dict, subject, predicate, key,
fallbacks=fallbacks,
list_value=list_value,
date_value=date_value,
_type=_type,
all_translated=all_translated)
translated=translated)

def _add_triple_from_dict(self, _dict, subject, predicate, key,
fallbacks=None,
Expand All @@ -745,7 +748,7 @@ def _add_triple_from_dict(self, _dict, subject, predicate, key,
_type=Literal,
_datatype=None,
value_modifier=None,
all_translated=False):
translated=False):
'''
Adds a new triple to the graph with the provided parameters
Expand Down Expand Up @@ -779,7 +782,7 @@ def _add_triple_from_dict(self, _dict, subject, predicate, key,
self._add_date_triple(subject, predicate, value, _type)
elif value:
# Normal text value
if all_translated and isinstance(value, dict):
if translated and isinstance(value, dict):
# We assume that all translated field values are Literals
for lang, translated_value in value.items():
object = Literal(translated_value, lang=lang)
Expand Down Expand Up @@ -1223,7 +1226,7 @@ def graph_from_dataset(self, dataset_dict, dataset_ref):
(title_key, DCT.title, None, Literal),
(notes_key, DCT.description, None, Literal),
]
self._add_triples_from_dict(dataset_dict, dataset_ref, items, all_translated=True)
self._add_triples_from_dict(dataset_dict, dataset_ref, items, translated=True)

# Basic fields
items = [
Expand Down Expand Up @@ -1328,14 +1331,21 @@ def graph_from_dataset(self, dataset_dict, dataset_ref):
# If no name but an URI is available, the name literal remains empty to
# avoid mixing organization and dataset values.
if not publisher_name and not publisher_uri and dataset_dict.get('organization'):
try:
org_dict = toolkit.get_action(u'organization_show')({u'user': toolkit.g.user},
{u'id': dataset_dict['organization']['id']})
org_id = dataset_dict["organization"]["id"]
org_dict = None
if org_id in self._org_cache:
org_dict = self._org_cache[org_id]
else:
try:
org_dict = toolkit.get_action(u'organization_show')({u'ignore_auth': True},
{u'id': org_id})
self._org_cache[org_id] = org_dict
except toolkit.ObjectNotFound:
pass
if org_dict:
title_key = 'title_translated' if 'title_translated' in org_dict else 'title'
items = [(title_key, FOAF.name, None, Literal)]
self._add_triples_from_dict(org_dict, publisher_details, items, all_translated=True)
except toolkit.ObjectNotFound:
pass
self._add_triples_from_dict(org_dict, publisher_details, items, translated=True)
else:
g.add((publisher_details, FOAF.name, Literal(publisher_name)))
# TODO: It would make sense to fallback these to organization
Expand Down Expand Up @@ -1400,7 +1410,7 @@ def graph_from_dataset(self, dataset_dict, dataset_ref):
(name_key, DCT.title, None, Literal),
(description_key, DCT.description, None, Literal),
]
self._add_triples_from_dict(resource_dict, distribution, items, all_translated=True)
self._add_triples_from_dict(resource_dict, distribution, items, translated=True)

# Simple values
items = [
Expand Down
10 changes: 6 additions & 4 deletions ckanext/dcat/tests/test_euro_dcatap_profile_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ckanext.dcat import utils
from ckanext.dcat.processors import RDFSerializer
from ckanext.dcat.profiles import (DCAT, DCT, ADMS, XSD, VCARD, FOAF, SCHEMA,
SKOS, LOCN, GSP, OWL, SPDX, GEOJSON_IMT,
SKOS, LOCN, GSP, OWL, SPDX, GEOJSON_IMT,
DISTRIBUTION_LICENSE_FALLBACK_CONFIG)
from ckanext.dcat.utils import DCAT_EXPOSE_SUBCATALOGS
from ckanext.dcat.tests.utils import BaseSerializeTest
Expand Down Expand Up @@ -398,13 +398,15 @@ def test_publisher_extras(self):
assert self._triple(g, publisher, DCT.type, URIRef(extras['publisher_type']))

def test_publisher_org(self):
org = factories.Organization()

dataset = {
'id': '4b6fe9ca-dc77-4cec-92a4-55c6624a5bd6',
'name': 'test-dataset',
'organization': {
'id': '',
'name': 'publisher1',
'title': 'Example Publisher from Org',
'id': org['id'],
'name': org['name'],
'title': org['title'],
}
}

Expand Down

0 comments on commit 1d1b155

Please sign in to comment.