Skip to content

Commit

Permalink
Add plugin for croissant functionality
Browse files Browse the repository at this point in the history
Register a template folder and automatically override the dataset read
page to add the Croissant serialization.
  • Loading branch information
amercader committed Jan 8, 2025
1 parent c69c639 commit 4be90d3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
52 changes: 36 additions & 16 deletions ckanext/dcat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,30 @@ def get_endpoint(_type="dataset"):
return "dcat.read_dataset" if _type == "dataset" else "dcat.read_catalog"


def structured_data(dataset_dict, profiles=None, _format="jsonld"):
def _get_serialization(dataset_dict, profiles=None, _format="jsonld"):

serializer = RDFSerializer(profiles=profiles)

output = serializer.serialize_dataset(dataset_dict, _format=_format)

# parse result again to prevent UnicodeDecodeError and add formatting
if _format == "jsonld":
try:
json_data = json.loads(output)
return json.dumps(
json_data,
sort_keys=True,
indent=4,
separators=(",", ": "),
cls=json.JSONEncoderForHTML,
)
except ValueError:
# result was not JSON, return anyway
pass
return output


def structured_data(dataset_dict, profiles=None):
"""
Returns a string containing the structured data of the given
dataset id and using the given profiles (if no profiles are supplied
Expand All @@ -33,20 +56,17 @@ def structured_data(dataset_dict, profiles=None, _format="jsonld"):
if not profiles:
profiles = ["schemaorg"]

serializer = RDFSerializer(profiles=profiles)
return _get_serialization(dataset_dict, profiles, "jsonld")

output = serializer.serialize_dataset(dataset_dict, _format=_format)

# parse result again to prevent UnicodeDecodeError and add formatting
try:
json_data = json.loads(output)
return json.dumps(
json_data,
sort_keys=True,
indent=4,
separators=(",", ": "),
cls=json.JSONEncoderForHTML,
)
except ValueError:
# result was not JSON, return anyway
return output
def croissant(dataset_dict, profiles=None):
"""
Returns a string containing the Croissant ML representation of the given
dataset using the `croissant` profile.
This string can be used in the frontend.
"""

if not profiles:
profiles = ["croissant"]

return _get_serialization(dataset_dict, profiles, "jsonld")
18 changes: 18 additions & 0 deletions ckanext/dcat/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,21 @@ def get_helpers(self):
return {
'structured_data': helpers.structured_data,
}


class CroissantPlugin(p.SingletonPlugin):

p.implements(p.IConfigurer, inherit=True)
p.implements(p.ITemplateHelpers, inherit=True)

# IConfigurer

def update_config(self, config):
p.toolkit.add_template_directory(config, '../templates/croissant')

# ITemplateHelpers

def get_helpers(self):
return {
'croissant': helpers.croissant,
}
13 changes: 13 additions & 0 deletions ckanext/dcat/templates/croissant/package/read_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% ckan_extends %}

{% block scripts %}

{{ super() }}

{% block croissant %}
<!-- Croissant ML -->
<script type="application/ld+json">
{{ h.croissant(pkg) | safe }}
</script>
{% endblock %}
{% endblock %}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dcat_json_interface = "ckanext.dcat.plugins:DCATJSONInterface"
dcat = "ckanext.dcat.plugins:DCATPlugin"

structured_data = "ckanext.dcat.plugins:StructuredDataPlugin"
croissant = "ckanext.dcat.plugins:CroissantPlugin"
# Test plugins
test_rdf_harvester = "ckanext.dcat.tests.harvester.test_harvester:TestRDFHarvester"
test_rdf_null_harvester = "ckanext.dcat.tests.harvester.test_harvester:TestRDFNullHarvester"
Expand Down

0 comments on commit 4be90d3

Please sign in to comment.