Skip to content

Commit

Permalink
Add support for Hydra collection type PartialCollectionView
Browse files Browse the repository at this point in the history
  • Loading branch information
seitenbau-govdata committed Sep 3, 2024
1 parent 5058f37 commit bebd796
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [Unreleased](https://github.com/ckan/ckanext-dcat/compare/v2.0.0...HEAD)

* ...
* Add support for hydra collection type PartialCollectionView

## [v2.0.0](https://github.com/ckan/ckanext-dcat/compare/v1.7.0...v2.0.0) - 2024-08-30

Expand Down
19 changes: 11 additions & 8 deletions ckanext/dcat/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def _get_default_rdf_profiles():
"""Helper function used fo documenting the rdf profiles config option"""
return " ".join(DEFAULT_RDF_PROFILES)

SUPPORTED_PAGINATION_COLLECTION_DESIGNS = [HYDRA.PartialCollectionView, HYDRA.PagedCollection]


class RDFProcessor(object):

Expand Down Expand Up @@ -125,14 +127,15 @@ def next_page(self):
'''
Returns the URL of the next page or None if there is no next page
'''
for pagination_node in self.g.subjects(RDF.type, HYDRA.PagedCollection):
# Try to find HYDRA.next first
for o in self.g.objects(pagination_node, HYDRA.next):
return str(o)

# If HYDRA.next is not found, try HYDRA.nextPage (deprecated)
for o in self.g.objects(pagination_node, HYDRA.nextPage):
return str(o)
for supported_collection_type in SUPPORTED_PAGINATION_COLLECTION_DESIGNS:
for pagination_node in self.g.subjects(RDF.type, supported_collection_type):
# Try to find HYDRA.next first
for o in self.g.objects(pagination_node, HYDRA.next):
return str(o)

# If HYDRA.next is not found, try HYDRA.nextPage (deprecated)
for o in self.g.objects(pagination_node, HYDRA.nextPage):
return str(o)
return None

def parse(self, data, _format=None):
Expand Down
32 changes: 31 additions & 1 deletion ckanext/dcat/tests/profiles/base/test_base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
RDFParserException,
RDFProfileException,
DEFAULT_RDF_PROFILES,
RDF_PROFILES_CONFIG_OPTION
RDF_PROFILES_CONFIG_OPTION,
SUPPORTED_PAGINATION_COLLECTION_DESIGNS
)

from ckanext.dcat.profiles import RDFProfile

DCT = Namespace("http://purl.org/dc/terms/")
DCAT = Namespace("http://www.w3.org/ns/dcat#")
HYDRA = Namespace('http://www.w3.org/ns/hydra/core#')


def _default_graph():
Expand Down Expand Up @@ -207,6 +209,34 @@ def test_parse_pagination_next_page_both_vocabularies(self):

assert p.next_page() == 'http://example.com/catalog.xml?page=next'

@pytest.mark.parametrize("collection_design", SUPPORTED_PAGINATION_COLLECTION_DESIGNS + [HYDRA.Unsupported])
def test_parse_pagination_next_page_different_collection_designs(self, collection_design):
design = collection_design.lstrip().split('#')[1]

data = f'''<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:hydra="http://www.w3.org/ns/hydra/core#">
<hydra:{design} rdf:about="http://example.com/catalog.xml?page=1">
<hydra:totalItems rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">245</hydra:totalItems>
<hydra:lastPage>http://example.com/catalog.xml?page=3</hydra:lastPage>
<hydra:itemsPerPage rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">100</hydra:itemsPerPage>
<hydra:nextPage>http://example.com/catalog.xml?page=2</hydra:nextPage>
<hydra:firstPage>http://example.com/catalog.xml?page=1</hydra:firstPage>
</hydra:{design}>
</rdf:RDF>
'''

p = RDFParser()

p.parse(data)

if collection_design in SUPPORTED_PAGINATION_COLLECTION_DESIGNS:
assert p.next_page() == 'http://example.com/catalog.xml?page=2'
else:
assert p.next_page() == None

def test_parse_without_pagination(self):

data = '''<?xml version="1.0" encoding="utf-8" ?>
Expand Down

0 comments on commit bebd796

Please sign in to comment.