Skip to content

Commit

Permalink
implement the worker call for pushing the catalog to sparql
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya-Oladazimi committed Nov 2, 2023
1 parent 1740638 commit 855c925
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
55 changes: 38 additions & 17 deletions ckanext/crc1153/controllers/crcDcatProfileController.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,41 @@
from flask import send_file
import io
from ckanext.crc1153.libs.crc_profile.helpers import Crc1153DcatProfileHelper as Helper
from ckanext.crc1153.libs.auth_helpers import AuthHelpers
from ckan.model import Package



class Crc1153DcatProfileController:


def load_admin_view():

context = {'model': model,
'user': toolkit.g.user, 'auth_user_obj': toolkit.g.userobj}
try:
logic.check_access('sysadmin', context, {})
except logic.NotAuthorized:
toolkit.abort(403, 'Need to be system administrator to administer')

AuthHelpers.abort_if_not_admin()
return render_template('crc_profile/admin_panel.html')



def export_catalog():

def push_to_sparql():
AuthHelpers.abort_if_not_admin()
all_datasets = Package.search_by_name('')
all_graphs = []
for dataset in all_datasets:
if dataset.state == 'active':
package = toolkit.get_action('package_show')({}, {'name_or_id': dataset.name})
package = Helper.setDatasetUri(package)
graph = Helper.get_dataset_graph(package)
all_graphs.append(graph)

context = {'model': model,
'user': toolkit.g.user, 'auth_user_obj': toolkit.g.userobj}
try:
logic.check_access('sysadmin', context, {})
except logic.NotAuthorized:
toolkit.abort(403, 'Need to be system administrator to administer')

toolkit.enqueue_job(push_catalog_to_sparql, kwargs={'catalog_graphs': all_graphs})

all_datasets = Package.search_by_name('')
return '0'



def export_catalog():
AuthHelpers.abort_if_not_admin()

ElementTree.register_namespace("dc", "http://purl.org/dc/terms/")
ElementTree.register_namespace("dct", "http://purl.org/dc/dcmitype/")
Expand All @@ -57,6 +64,8 @@ def export_catalog():
ElementTree.register_namespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
ElementTree.register_namespace("dr", "http://www.w3id.org/ecsel-dr-PROD#")


all_datasets = Package.search_by_name('')
xml = ElementTree.fromstring("<RDF></RDF>")
dataset_dicts = []
for dataset in all_datasets:
Expand All @@ -68,4 +77,16 @@ def export_catalog():
serializer = RDFSerializer(profiles=package.get('profiles'))
rdf_output = serializer.serialize_catalog(dataset_dicts=dataset_dicts, _format="ttl")
file = io.BytesIO(rdf_output.encode())
return send_file(file, mimetype='application/ttl', attachment_filename="ckancatlog.ttl", as_attachment = True)
return send_file(file, mimetype='application/ttl', attachment_filename="ckancatlog.ttl", as_attachment = True)





def push_catalog_to_sparql(catalog_graphs):
for graph in catalog_graphs:
try:
res_d = Helper.delete_from_sparql(graph)
res_i = Helper.insert_to_sparql(graph)
except:
continue
12 changes: 12 additions & 0 deletions ckanext/crc1153/libs/crc_profile/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def get_apache_jena_endpoint():
return toolkit.config.get('ckanext.apacheJena.endpoint')



def get_linked_publication(dataset_name):
'''
The functions get all the linked publications for a dataset in ckan.
Expand All @@ -48,6 +49,7 @@ def get_linked_publication(dataset_name):
return linked_pubs



@staticmethod
def get_linked_machines(resource_id):
if not Commons.check_plugin_enabled("machine_link"):
Expand All @@ -56,6 +58,7 @@ def get_linked_machines(resource_id):
return mediaWikiHelper.get_machine_link(resource_id)



@staticmethod
def get_linked_samples(resource_id):
if not Commons.check_plugin_enabled("sample_link"):
Expand All @@ -65,6 +68,7 @@ def get_linked_samples(resource_id):
return SampleLinkHelper.get_sample_link(resource_id)



@staticmethod
def insert_to_sparql(graph):
for s,p,o in graph:
Expand All @@ -78,6 +82,7 @@ def insert_to_sparql(graph):
return results



@staticmethod
def delete_from_sparql(graph):
for s,p,o in graph:
Expand All @@ -101,6 +106,7 @@ def delete_from_sparql(graph):
return results



@staticmethod
def get_dataset_graph(dataset_dict):
dataset_dict = Crc1153DcatProfileHelper.setDatasetUri(dataset_dict)
Expand All @@ -109,6 +115,7 @@ def get_dataset_graph(dataset_dict):
return serializer.g



@staticmethod
def clean_triples(s,p,o):
if "http" in s:
Expand All @@ -130,6 +137,7 @@ def clean_triples(s,p,o):




@staticmethod
def setDatasetUri(package):
ckan_root_path = toolkit.config.get('ckan.root_path')
Expand All @@ -145,3 +153,7 @@ def setDatasetUri(package):
res["uri"] = ckan_base_url + "/dataset/" + package['name'] + "/resource/" + res['id']

return package




7 changes: 7 additions & 0 deletions ckanext/crc1153/plugins/crc_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def get_blueprint(self):
u'export_catalog',
Crc1153DcatProfileController.export_catalog,
methods=['GET']
)

blueprint.add_url_rule(
u'/dcatapcrc1153/push_to_sparql',
u'push_to_sparql',
Crc1153DcatProfileController.push_to_sparql,
methods=['GET']
)

return blueprint
Expand Down
11 changes: 9 additions & 2 deletions ckanext/crc1153/templates/crc_profile/admin_panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@

{% block primary_content_inner %}
{% if h.is_plugin_enabled('crc1153_dcat_profile') %}
{% set action = h.url_for('crc1153_dcat_profile.export_catalog', _external=True) %}
{% set export_action = h.url_for('crc1153_dcat_profile.export_catalog', _external=True) %}
{% set push_sparql_action = h.url_for('crc1153_dcat_profile.push_to_sparql', _external=True) %}
<div class="row">
<div class="col-sm-12">
<a class="btn btn-primary" href="{{action}}" id="export-catalog-btn" target="_blank">Export Catalog</a>
<a class="btn btn-primary" href="{{export_action}}" id="export-catalog-btn" target="_blank">Export Catalog</a>
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-12">
<a class="btn btn-primary" href="{{push_sparql_action}}" id="push-to-sparql-btn">Push CKAN Datasets Metadata To SPARQL</a>
</div>
</div>
{% endif %}
Expand Down

0 comments on commit 855c925

Please sign in to comment.