diff --git a/src/metax_api/api/oaipmh/base/metax_oai_server.py b/src/metax_api/api/oaipmh/base/metax_oai_server.py index 606ec711..c2a82aa0 100644 --- a/src/metax_api/api/oaipmh/base/metax_oai_server.py +++ b/src/metax_api/api/oaipmh/base/metax_oai_server.py @@ -10,6 +10,8 @@ from metax_api.models.catalog_record import CatalogRecord from metax_api.services import CatalogRecordService as CRS +syke_url_prefix_template = 'http://metatieto.ymparisto.fi:8080/geoportal/catalog/search/resource/details.page?uuid=%s' + class MetaxOAIServer(ResumptionOAIPMH): @@ -48,22 +50,41 @@ def _get_filtered_records(self, set, cursor, batch_size, from_=None, until=None) query_set = query_set.filter(data_catalog__catalog_json__identifier__in=self._get_default_set_filter()) return query_set[cursor:batch_size] + def _handle_syke_urnresolver_metadata(self, record): + identifiers = [] + preferred_identifier = record.research_dataset.get('preferred_identifier') + identifiers.append(preferred_identifier) + for id_obj in record.research_dataset.get('other_identifier', []): + if id_obj.get('notation', '').startswith('{'): + uuid = id_obj['notation'] + identifiers.append(syke_url_prefix_template % uuid) + return identifiers + def _get_oai_dc_urnresolver_metadata(self, record): """ Preferred identifier is added only for ida and att catalog records other identifiers are added for all. + + Special handling for SYKE catalog. """ + identifiers = [] - identifiers.append(settings.OAI['ETSIN_URL_TEMPLATE'] % record.identifier) - # assuming ida and att catalogs are not harvested - if not record.catalog_is_harvested(): - preferred_identifier = record.research_dataset.get('preferred_identifier') - identifiers.append(preferred_identifier) - for id_obj in record.research_dataset.get('other_identifier', []): - if id_obj.get('notation', '').startswith('urn:nbn:fi:csc-kata'): - other_urn = id_obj['notation'] - identifiers.append(other_urn) + data_catalog = record.data_catalog.catalog_json.get('identifier') + if data_catalog == 'urn:nbn:fi:att:data-catalog-harvest-syke': + identifiers = self._handle_syke_urnresolver_metadata(record) + + else: + identifiers.append(settings.OAI['ETSIN_URL_TEMPLATE'] % record.identifier) + + # assuming ida and att catalogs are not harvested + if not record.catalog_is_harvested(): + preferred_identifier = record.research_dataset.get('preferred_identifier') + identifiers.append(preferred_identifier) + for id_obj in record.research_dataset.get('other_identifier', []): + if id_obj.get('notation', '').startswith('urn:nbn:fi:csc-kata'): + other_urn = id_obj['notation'] + identifiers.append(other_urn) meta = { 'identifier': identifiers @@ -189,7 +210,7 @@ def getRecord(self, metadataPrefix, identifier): """Implement OAI-PMH verb GetRecord.""" try: record = CatalogRecord.objects.get( - data_catalog__catalog_json__identifier__in=self._get_default_set_filter(), + # data_catalog__catalog_json__identifier__in=self._get_default_set_filter(), identifier__exact=identifier ) except CatalogRecord.DoesNotExist: diff --git a/src/metax_api/tests/api/oaipmh/syke.py b/src/metax_api/tests/api/oaipmh/syke.py new file mode 100644 index 00000000..b73c397f --- /dev/null +++ b/src/metax_api/tests/api/oaipmh/syke.py @@ -0,0 +1,62 @@ +from django.core.management import call_command +import lxml.etree +from rest_framework import status +from rest_framework.test import APITestCase +from metax_api.models import CatalogRecord +from metax_api.api.oaipmh.base.metax_oai_server import syke_url_prefix_template + +from metax_api.tests.utils import test_data_file_path, TestClassUtils + + +class SYKEOAIPMHReadTests(APITestCase, TestClassUtils): + + _namespaces = {'o': 'http://www.openarchives.org/OAI/2.0/', + 'oai_dc': "http://www.openarchives.org/OAI/2.0/oai_dc/", + 'dc': "http://purl.org/dc/elements/1.1/", + 'dct': "http://purl.org/dc/terms/", + 'datacite': 'http://schema.datacite.org/oai/oai-1.0/'} + + @classmethod + def setUpClass(cls): + """ + Loaded only once for test cases inside this class. + """ + call_command('loaddata', test_data_file_path, verbosity=0) + super(SYKEOAIPMHReadTests, cls).setUpClass() + + def setUp(self): + cr = CatalogRecord.objects.get(pk=1) + cr.data_catalog.catalog_json["identifier"] = "urn:nbn:fi:att:data-catalog-harvest-syke" + cr.data_catalog.force_save() + cr.research_dataset.update({ + "preferred_identifier": "urn:nbn:fi:csc-kata20170613100856741858", + "other_identifier": [ + { + "notation": "{55AB842F-9CED-4E80-A7E5-07A54F0AE4A4}" + } + ] + }) + cr.force_save() + self.identifier = cr.identifier + self.pref_identifier = cr.research_dataset['preferred_identifier'] + self.dc = cr.data_catalog.catalog_json["identifier"] + self._use_http_authorization() + + def _get_results(self, data, xpath): + root = data + if isinstance(data, bytes): + root = lxml.etree.fromstring(data) + return root.xpath(xpath, namespaces=self._namespaces) + + def test_get_urn_resolver_record(self): + response = self.client.get( + '/oai/?verb=GetRecord&identifier=%s&metadataPrefix=oai_dc_urnresolver' % self.identifier) + self.assertEqual(response.status_code, status.HTTP_200_OK) + identifiers = self._get_results(response.content, + '//o:record/o:metadata/oai_dc:dc/dc:identifier[text()="%s"]' % self.pref_identifier) + self.assertTrue(len(identifiers) == 1, response.content) + + syke_url = syke_url_prefix_template % '{55AB842F-9CED-4E80-A7E5-07A54F0AE4A4}' + identifiers = self._get_results(response.content, + '//o:record/o:metadata/oai_dc:dc/dc:identifier[text()="%s"]' % syke_url) + self.assertTrue(len(identifiers) == 1, response.content)