Skip to content

Commit

Permalink
Add croissant endpoint at /dataset/<id>/croissant.jsonld
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jan 8, 2025
1 parent 4be90d3 commit 104b1ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
41 changes: 38 additions & 3 deletions ckanext/dcat/blueprints.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# -*- coding: utf-8 -*-
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, make_response

from ckantoolkit import config
import ckantoolkit as toolkit

from ckan.views.dataset import CreateView

import ckan.plugins.toolkit as toolkit
import ckanext.dcat.utils as utils
from ckanext.dcat.helpers import endpoints_enabled
from ckanext.dcat.helpers import endpoints_enabled, croissant as croissant_serialization

config = toolkit.config


dcat = Blueprint(
'dcat',
Expand Down Expand Up @@ -50,3 +53,35 @@ def dcat_json():
dcat_json_interface.add_url_rule(config.get('ckanext.dcat.json_endpoint',
'/dcat.json'),
view_func=dcat_json)


croissant = Blueprint('croissant', __name__)


def read_dataset_croissant(_id):

try:
user_name = (
toolkit.current_user.name
if hasattr(toolkit, "current_user")
else toolkit.g.user
)

context = {
'user': user_name,
}
data_dict = {'id': _id}

dataset_dict = toolkit.get_action("package_show")(context, data_dict)
except (toolkit.ObjectNotFound, toolkit.NotAuthorized):
return toolkit.abort(
404,
toolkit._("Dataset not found or you have no permission to view it")
)

response = make_response(croissant_serialization(dataset_dict))
response.headers["Content-type"] = "application/ld+json"

return response

croissant.add_url_rule('/dataset/<_id>/croissant.jsonld', view_func=read_dataset_croissant)
6 changes: 6 additions & 0 deletions ckanext/dcat/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class CroissantPlugin(p.SingletonPlugin):

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

# IConfigurer

Expand All @@ -276,3 +277,8 @@ def get_helpers(self):
return {
'croissant': helpers.croissant,
}

# IBlueprint

def get_blueprint(self):
return [blueprints.croissant]

0 comments on commit 104b1ae

Please sign in to comment.