Skip to content

Commit

Permalink
Separate template helpers into their own module
Browse files Browse the repository at this point in the history
amercader committed Jan 8, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a2166e0 commit f5786b1
Showing 4 changed files with 59 additions and 52 deletions.
5 changes: 3 additions & 2 deletions ckanext/dcat/blueprints.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
from flask import Blueprint, jsonify, make_response
from flask import Blueprint, jsonify

from ckantoolkit import config

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

dcat = Blueprint(
'dcat',
@@ -23,7 +24,7 @@ def read_dataset(_id, _format=None, package_type=None):
return utils.read_dataset_page(_id, _format)


if utils.endpoints_enabled():
if endpoints_enabled():

# requirements={'_format': 'xml|rdf|n3|ttl|jsonld'}
dcat.add_url_rule(config.get('ckanext.dcat.catalog_endpoint',
52 changes: 52 additions & 0 deletions ckanext/dcat/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Helpers used by templates
"""
import simplejson as json

import ckantoolkit as toolkit

from ckanext.dcat.processors import RDFSerializer

config = toolkit.config


ENABLE_RDF_ENDPOINTS_CONFIG = "ckanext.dcat.enable_rdf_endpoints"


def endpoints_enabled():
return toolkit.asbool(config.get(ENABLE_RDF_ENDPOINTS_CONFIG, True))


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"):
"""
Returns a string containing the structured data of the given
dataset id and using the given profiles (if no profiles are supplied
the default profiles are used).
This string can be used in the frontend.
"""

if not profiles:
profiles = ["schemaorg"]

serializer = RDFSerializer(profiles=profiles)

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
8 changes: 4 additions & 4 deletions ckanext/dcat/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
dcat_datasets_list,
dcat_auth,
)
from ckanext.dcat import helpers
from ckanext.dcat import utils
from ckanext.dcat.validators import dcat_validators

@@ -102,9 +103,8 @@ def update_config(self, config):

def get_helpers(self):
return {
'helper_available': utils.helper_available,
'dcat_get_endpoint': utils.get_endpoint,
'dcat_endpoints_enabled': utils.endpoints_enabled,
'dcat_get_endpoint': helpers.get_endpoint,
'dcat_endpoints_enabled': helpers.endpoints_enabled,
}

# IActions
@@ -256,5 +256,5 @@ def update_config(self, config):

def get_helpers(self):
return {
'structured_data': utils.structured_data,
'structured_data': helpers.structured_data,
}
46 changes: 0 additions & 46 deletions ckanext/dcat/utils.py
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@
DCAT_CLEAN_TAGS = 'ckanext.dcat.clean_tags'

DEFAULT_CATALOG_ENDPOINT = '/catalog.{_format}'
ENABLE_RDF_ENDPOINTS_CONFIG = 'ckanext.dcat.enable_rdf_endpoints'
ENABLE_CONTENT_NEGOTIATION_CONFIG = 'ckanext.dcat.enable_content_negotiation'


@@ -95,43 +94,6 @@ def field_labels():
'created': _('Created'),
}

def helper_available(helper_name):
'''
Checks if a given helper name is available on `h`
'''
try:
getattr(h, helper_name)
except (AttributeError, HelperError):
return False
return True

def structured_data(dataset_id, profiles=None, _format='jsonld'):
'''
Returns a string containing the structured data of the given
dataset id and using the given profiles (if no profiles are supplied
the default profiles are used).
This string can be used in the frontend.
'''
if not profiles:
profiles = ['schemaorg']

data = toolkit.get_action('dcat_dataset_show')(
{},
{
'id': dataset_id,
'profiles': profiles,
'format': _format,
}
)
# parse result again to prevent UnicodeDecodeError and add formatting
try:
json_data = json.loads(data)
return json.dumps(json_data, sort_keys=True,
indent=4, separators=(',', ': '), cls=json.JSONEncoderForHTML)
except ValueError:
# result was not JSON, return anyway
return data

def catalog_uri():
'''
@@ -459,11 +421,3 @@ def read_catalog_page(_format):
response.headers['Content-type'] = CONTENT_TYPES[_format]

return response


def endpoints_enabled():
return toolkit.asbool(config.get(ENABLE_RDF_ENDPOINTS_CONFIG, True))


def get_endpoint(_type='dataset'):
return 'dcat.read_dataset' if _type == 'dataset' else 'dcat.read_catalog'

0 comments on commit f5786b1

Please sign in to comment.