Skip to content

Commit

Permalink
Merge pull request #125 from opendata-swiss/feat/harvest-qualified-re…
Browse files Browse the repository at this point in the history
…lations-field

fix: Fix mapping of see_alsos
  • Loading branch information
bellisk authored Nov 8, 2023
2 parents 1d4955b + bfceadf commit d1bff43
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ jobs:
- name: Create ckan container
run: |
docker create --name test_ckan --network ${{ job.container.network }} --network-alias ckan \
-e "HOME=/github/home" -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" \
-e "HOME=/github/home" -e GITHUB_ACTIONS=true -e CI=true -e CKAN_SITE_URL=http://test.ckan.net \
-v "/var/run/docker.sock":"/var/run/docker.sock" \
-v "/home/runner/work":"/__w" -v "/home/runner/work/_temp":"/__w/_temp" \
-v "/home/runner/work/_actions":"/__w/_actions" -v "/opt/hostedtoolcache":"/__t" \
-v "/home/runner/work/_temp/_github_home":"/github/home" \
Expand Down
14 changes: 10 additions & 4 deletions ckanext/geocat/tests/test_dataset_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_fields(self):
'spatial',
'coverage',
'accrual_periodicity',
'see_alsos',
'qualified_relations',
'owner_org',
'resources'
]
Expand Down Expand Up @@ -205,9 +205,15 @@ def test_fields_values(self):
self.assertEquals('', dataset.get('accrual_periodicity'))

# see alsos
self.assertTrue(hasattr(dataset['see_alsos'], '__iter__'))
self.assertEquals(1, len(dataset['see_alsos']))
self.assertEquals('8454f7d9-e3f2-4cc7-be6d-a82196660ccd@swisstopo', dataset['see_alsos'][0]) # noqa
self.assertTrue(hasattr(dataset['qualified_relations'], '__iter__'))
self.assertEquals(1, len(dataset['qualified_relations']))
self.assertEquals(
{
'relation': 'http://test.ckan.net/perma/8454f7d9-e3f2-4cc7-be6d-a82196660ccd@swisstopo',
'had_role': 'http://www.iana.org/assignments/relation/related',
},
dataset['qualified_relations'][0]
)

def test_fields_values_de_only(self):
xml = self._load_xml('only_de.xml')
Expand Down
26 changes: 14 additions & 12 deletions ckanext/geocat/utils/csw_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
]
GMD_DESCRIPTION = '//gmd:identificationInfo//gmd:abstract'
GMD_RIGHTS = './/gmd:resourceConstraints//gmd:otherConstraints'
GMD_SEE_ALSOS = '//gmd:identificationInfo//gmd:aggregationInfo//gmd:aggregateDataSetIdentifier/gmd:MD_Identifier/gmd:code/gco:CharacterString/text()' # noqa
GMD_QUALIFIED_RELATIONS = '//gmd:identificationInfo//gmd:aggregationInfo//gmd:aggregateDataSetIdentifier/gmd:MD_Identifier/gmd:code/gco:CharacterString/text()' # noqa
GMD_TEMPORAL_START = '//gmd:identificationInfo//gmd:extent//gmd:temporalElement//gml:TimePeriod/gml:beginPosition/text()' # noqa
GMD_TEMPORAL_END = '//gmd:identificationInfo//gmd:extent//gmd:temporalElement//gml:TimePeriod/gml:endPosition/text()' # noqa
GMD_LANGUAGE = ['//gmd:identificationInfo//gmd:language/gco:CharacterString/text()', # noqa
Expand Down Expand Up @@ -123,10 +123,11 @@ def get_metadata(self, csw_record_as_string, geocat_id):
dataset_dict['coverage'] = _map_dataset_coverage()
dataset_dict['spatial'] = _map_dataset_spatial(node=root_node)
dataset_dict['temporals'] = _map_dataset_temporals(node=root_node)
dataset_dict['see_alsos'] = \
_map_dataset_see_alsos(node=root_node,
organization_slug=self.organization_slug,
valid_identifiers=self.valid_identifiers)
dataset_dict['qualified_relations'] = \
_map_dataset_qualified_relations(
node=root_node,
organization_slug=self.organization_slug,
valid_identifiers=self.valid_identifiers)
dataset_dict['owner_org'] = self.organization_slug

rights = \
Expand Down Expand Up @@ -162,7 +163,7 @@ def get_metadata(self, csw_record_as_string, geocat_id):
dataset_dict['resources'].append(ogdch_service)

# Map geocat permalink as relation
dataset_dict['relations'].append(ogdch_map_utils.get_permalink(
dataset_dict['relations'].append(ogdch_map_utils.get_geocat_permalink(
geocat_id=geocat_id,
geocat_perma_link=self.geocat_perma_link,
geocat_perma_label=self.geocat_perma_label,
Expand Down Expand Up @@ -389,15 +390,16 @@ def _map_dataset_temporals(node):
geocat_temporal_end)


def _map_dataset_see_alsos(node, organization_slug, valid_identifiers):
geocat_see_alsos = \
def _map_dataset_qualified_relations(node, organization_slug,
valid_identifiers):
geocat_qualified_relations = \
xpath_utils.xpath_get_all_sub_nodes_for_node_and_path(
node=node,
path=GMD_SEE_ALSOS)
if geocat_see_alsos:
path=GMD_QUALIFIED_RELATIONS)
if geocat_qualified_relations:
return \
ogdch_map_utils.map_see_alsos(
geocat_see_alsos,
ogdch_map_utils.map_qualified_relations(
geocat_qualified_relations,
organization_slug,
valid_identifiers)
return []
Expand Down
23 changes: 17 additions & 6 deletions ckanext/geocat/utils/ogdch_map_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime
from ckan.lib.munge import munge_tag
from ckanext.geocat.utils import xpath_utils # noqa
import ckan.plugins.toolkit as tk
import ckanext.geocat.utils.mapping_utils as mu

ORGANIZATION_URI_BASE = 'https://opendata.swiss/organization/'
Expand Down Expand Up @@ -142,12 +143,17 @@ def map_language(geocat_language):
return language_mapping.get(geocat_language, '')


def map_see_alsos(geocat_see_alsos, organization_slug, valid_identifiers):
ogdch_see_alsos = [
def map_qualified_relations(geocat_qualified_relations, organization_slug,
valid_identifiers):
ogdch_qualified_relations = [
map_geocat_to_ogdch_identifier(geocat_identifier, organization_slug)
for geocat_identifier in geocat_see_alsos]
return [see_also for see_also in ogdch_see_alsos
if see_also in valid_identifiers]
for geocat_identifier in geocat_qualified_relations]
return [
{'relation': get_ogdch_permalink(qualified_relation),
'had_role': "http://www.iana.org/assignments/relation/related"}
for qualified_relation in ogdch_qualified_relations
if qualified_relation in valid_identifiers
]


def map_temporals(geocat_temporal_start, geocat_temporal_end):
Expand All @@ -162,11 +168,16 @@ def map_temporals(geocat_temporal_start, geocat_temporal_end):
return []


def get_permalink(geocat_id, geocat_perma_link, geocat_perma_label):
def get_geocat_permalink(geocat_id, geocat_perma_link, geocat_perma_label):
permalink = geocat_perma_link + geocat_id
return {'url': permalink, 'label': geocat_perma_label}


def get_ogdch_permalink(identifier):
site_url = tk.config.get('ckan.site_url')
return u'{0}/perma/{1}'.format(site_url, identifier)


def get_legal_basis_link(legal_basis_url):
LEGAL_BASIS_LABEL = 'legal_basis'
return {'url': legal_basis_url, 'label': LEGAL_BASIS_LABEL}
Expand Down

0 comments on commit d1bff43

Please sign in to comment.