From a22210e9ab3ca647a95fb561f789ea678ee09a64 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 12:02:34 +0100 Subject: [PATCH 01/44] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 5efd989..769ef83 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -Jinja2==2.11.3 +Jinja2==3.1.2 From 3d362355d72ab38f0743bcff2b1fa432ad2d3305 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 12:07:24 +0100 Subject: [PATCH 02/44] py2 --> py3 --- ckanext/relation/plugin.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index 410cbd3..2468bb9 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -70,7 +70,7 @@ def get_child_package(id): relationships = p.toolkit.get_action("package_relationships_list")( data_dict={"id": id, "rel": "parent_of"} ) - except Exception, e: + except Exception as e: return {} children = [] @@ -99,7 +99,7 @@ def get_parent_package(id): relationships = p.toolkit.get_action("package_relationships_list")( data_dict={"id": id, "rel": "child_of"} ) - except Exception, e: + except Exception as e: return {} parents = [] @@ -118,7 +118,7 @@ def get_dependency_package(id): relationships = p.toolkit.get_action("package_relationships_list")( data_dict={"id": id, "rel": "depends_on"} ) - except Exception, e: + except Exception as e: return {} dependencies = [] @@ -139,7 +139,7 @@ def get_depend_package(id): relationships = p.toolkit.get_action("package_relationships_list")( data_dict={"id": id, "rel": "dependency_of"} ) - except Exception, e: + except Exception as e: return {} depend = [] @@ -168,7 +168,7 @@ def get_linked_package(id): relationships = p.toolkit.get_action("package_relationships_list")( data_dict={"id": id, "rel": "links_to"} ) - except Exception, e: + except Exception as e: return {} linked = [] @@ -190,7 +190,7 @@ def get_links_package(id): relationships = p.toolkit.get_action("package_relationships_list")( data_dict={"id": id, "rel": "linked_from"} ) - except Exception, e: + except Exception as e: return {} links = [] @@ -387,4 +387,4 @@ def i18n_domain(self): This implementation assumes the gettext domain is ckanext-{extension name}, hence your pot, po and mo files should be named ckanext-{extension name}.mo''' - return 'ckanext-{name}'.format(name=self.name) \ No newline at end of file + return 'ckanext-{name}'.format(name=self.name) From 8a68a82bd85a1148d62dfb72c5a8f70ea0a4a6f5 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 12:09:19 +0100 Subject: [PATCH 03/44] Update controller.py --- ckanext/relation/controller.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 7b23be0..f830dac 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -3,7 +3,7 @@ from ckan.lib.base import BaseController import ckan.lib.helpers as h from ckan.common import OrderedDict, _, json, request, c, g, response, config -from urllib import urlencode +from urllib.parse import urlencode import cgi from paste.deploy.converters import asbool import ckan.logic as logic @@ -151,7 +151,7 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): # see if we have any data that we are trying to save data_provided = False - for key, value in data.iteritems(): + for key, value in data.items(): if ( value or isinstance(value, cgi.FieldStorage) ) and key != "resource_type": @@ -220,7 +220,7 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): get_action("resource_update")(context, data) else: get_action("resource_create")(context, data) - except ValidationError, e: + except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.new_resource_ext(id, data, errors, error_summary) From c8ad0e1b84e2a540c223859771800fa22bbdab66 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 12:15:11 +0100 Subject: [PATCH 04/44] Update plugin.py --- ckanext/relation/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index 2468bb9..9345d44 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -220,7 +220,7 @@ class RelationPlugin(p.SingletonPlugin): p.implements(p.ITranslation) p.implements(p.IConfigurable) p.implements(p.ITemplateHelpers) - p.implements(p.IRoutes, inherit=True) + p.implements(p.IBlueprint, inherit=True) package_link = "/dataset/" From b5011b76ad927c16048ca80ebee8dd1ce1db0f6b Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:18:35 +0100 Subject: [PATCH 05/44] Update resource_form.html --- ckanext/relation/templates/package/snippets/resource_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/templates/package/snippets/resource_form.html b/ckanext/relation/templates/package/snippets/resource_form.html index 60849b3..196d9a2 100644 --- a/ckanext/relation/templates/package/snippets/resource_form.html +++ b/ckanext/relation/templates/package/snippets/resource_form.html @@ -2,7 +2,7 @@ {% set data = data or {} %} {% set errors = errors or {} %} -{% set action = form_action or h.url_for(c.linkResource) %} +{% set action = form_action or h.url_for(linkResource) %}
{% block stages %} From 50163ebbf153f025879d0857962c30d7975e9c8d Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:27:30 +0100 Subject: [PATCH 06/44] c.linkResource --- ckanext/relation/controller.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index f830dac..a285b0c 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -46,11 +46,13 @@ def _resource_form(self, package_type): def finalrel(self, id, data=None, errors=None): if request.method == "POST": pass - c.link = str("/dataset/relationship/edit/" + id) + #c.link = str("/dataset/relationship/edit/" + id) + link = str("/dataset/relationship/edit/" + id) return render("package/new_data_relation.html", extra_vars={"package_id": id}) def new_relation(self, id): - c.link = str("/dataset/relationship/edit/" + id) + #c.link = str("/dataset/relationship/edit/" + id) + link = str("/dataset/relationship/edit/" + id) if request.method == "POST": save_action = request.params.get("save") print("new data dictionary !!!!!!!!!!!!!!!!") @@ -118,7 +120,8 @@ def new_relation(self, id): def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ FIXME: This is a temporary action to allow styling of the forms. """ - c.linkResource = str("/dataset/new_resource/" + id) + #c.linkResource = str("/dataset/new_resource/" + id) + link = str("/dataset/new_resource/" + id) if request.method == "POST" and not data: save_action = request.params.get("save") @@ -354,7 +357,8 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): def edit_relation(self, id, data=None, errors=None): try: - c.link = str("/dataset/relationship/new_relationship/" + id) + #c.link = str("/dataset/relationship/new_relationship/" + id) + link = str("/dataset/relationship/new_relationship/" + id) """context = { "model": model, "session": model.Session, From 802cc816bd6aa7e016b33e4e1592fa0302ba80ff Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:27:59 +0100 Subject: [PATCH 07/44] Update resource_form.html --- ckanext/relation/templates/package/snippets/resource_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/templates/package/snippets/resource_form.html b/ckanext/relation/templates/package/snippets/resource_form.html index 196d9a2..b2484db 100644 --- a/ckanext/relation/templates/package/snippets/resource_form.html +++ b/ckanext/relation/templates/package/snippets/resource_form.html @@ -2,7 +2,7 @@ {% set data = data or {} %} {% set errors = errors or {} %} -{% set action = form_action or h.url_for(linkResource) %} +{% set action = form_action or h.url_for(link) %} {% block stages %} From d6487eeedbf1782a12acb5be447ca936b09b0cf3 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:31:31 +0100 Subject: [PATCH 08/44] Update resource_form.html --- ckanext/relation/templates/package/snippets/resource_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/templates/package/snippets/resource_form.html b/ckanext/relation/templates/package/snippets/resource_form.html index b2484db..196d9a2 100644 --- a/ckanext/relation/templates/package/snippets/resource_form.html +++ b/ckanext/relation/templates/package/snippets/resource_form.html @@ -2,7 +2,7 @@ {% set data = data or {} %} {% set errors = errors or {} %} -{% set action = form_action or h.url_for(link) %} +{% set action = form_action or h.url_for(linkResource) %} {% block stages %} From e4ef58c6d2b1302005c5d2e7d86f4e8e2148a16d Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:32:31 +0100 Subject: [PATCH 09/44] Update controller.py --- ckanext/relation/controller.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index a285b0c..895361d 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -46,13 +46,11 @@ def _resource_form(self, package_type): def finalrel(self, id, data=None, errors=None): if request.method == "POST": pass - #c.link = str("/dataset/relationship/edit/" + id) - link = str("/dataset/relationship/edit/" + id) + c.link = str("/dataset/relationship/edit/" + id) return render("package/new_data_relation.html", extra_vars={"package_id": id}) def new_relation(self, id): - #c.link = str("/dataset/relationship/edit/" + id) - link = str("/dataset/relationship/edit/" + id) + c.link = str("/dataset/relationship/edit/" + id) if request.method == "POST": save_action = request.params.get("save") print("new data dictionary !!!!!!!!!!!!!!!!") @@ -120,8 +118,7 @@ def new_relation(self, id): def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ FIXME: This is a temporary action to allow styling of the forms. """ - #c.linkResource = str("/dataset/new_resource/" + id) - link = str("/dataset/new_resource/" + id) + linkResource = str("/dataset/new_resource/" + id) if request.method == "POST" and not data: save_action = request.params.get("save") @@ -357,8 +354,7 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): def edit_relation(self, id, data=None, errors=None): try: - #c.link = str("/dataset/relationship/new_relationship/" + id) - link = str("/dataset/relationship/new_relationship/" + id) + c.link = str("/dataset/relationship/new_relationship/" + id) """context = { "model": model, "session": model.Session, From ef4941c9df14b5fe2b3b017546a9f2dbb13898ef Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:33:00 +0100 Subject: [PATCH 10/44] Update controller.py --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 895361d..848a898 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -298,7 +298,7 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ def delete_ext(self, id): - # c.linkResource = str("/dataset/edit/" + id) + # linkResource = str("/dataset/edit/" + id) print("here is delete ext") if "cancel" in request.params: From f76a8aec446591a063c2b4357570a51f0fc75330 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 16:34:11 +0100 Subject: [PATCH 11/44] p --> plugins --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 848a898..2206b41 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -1,5 +1,5 @@ import logging -import ckan.plugins as p +import ckan.plugins as plugins from ckan.lib.base import BaseController import ckan.lib.helpers as h from ckan.common import OrderedDict, _, json, request, c, g, response, config From fbc1981a920620e9e7dbdc491ffbf47f134ff387 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 16:42:40 +0100 Subject: [PATCH 12/44] c --> common --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 2206b41..09f48c9 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -2,7 +2,7 @@ import ckan.plugins as plugins from ckan.lib.base import BaseController import ckan.lib.helpers as h -from ckan.common import OrderedDict, _, json, request, c, g, response, config +from ckan.common import OrderedDict, _, json, request, common, g, response, config from urllib.parse import urlencode import cgi from paste.deploy.converters import asbool From 3978be6cbb2e597cb89af95f3f46548046b894e4 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 16:53:12 +0100 Subject: [PATCH 13/44] Update controller.py --- ckanext/relation/controller.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 09f48c9..cd99421 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -16,9 +16,10 @@ log = logging.getLogger(__name__) -render = base.render -abort = base.abort +#render = base.render +#abort = base.abort #redirect = base.redirect + redirect = h.redirect_to NotFound = logic.NotFound NotAuthorized = logic.NotAuthorized From ceb7232f34f375c4b90ac1294bdbb06b1502bfbb Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:00:39 +0100 Subject: [PATCH 14/44] Update controller.py --- ckanext/relation/controller.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index cd99421..59f7e9a 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -47,11 +47,11 @@ def _resource_form(self, package_type): def finalrel(self, id, data=None, errors=None): if request.method == "POST": pass - c.link = str("/dataset/relationship/edit/" + id) + link = str("/dataset/relationship/edit/" + id) return render("package/new_data_relation.html", extra_vars={"package_id": id}) def new_relation(self, id): - c.link = str("/dataset/relationship/edit/" + id) + link = str("/dataset/relationship/edit/" + id) if request.method == "POST": save_action = request.params.get("save") print("new data dictionary !!!!!!!!!!!!!!!!") @@ -112,7 +112,7 @@ def new_relation(self, id): if save_action == "go-metadata": # XXX race condition if another user edits/deletes - h.redirect_to(controller="package", action="read", id=id) + h.redirect_to('package.read', id=id) redirect("/dataset/relationship/edit/" + id) @@ -179,7 +179,7 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): if save_action == "go-dataset": # go to final stage of adddataset - redirect(h.url_for(controller="package", action="edit", id=id)) + redirect(h.url_for('package.edit', id=id)) # see if we have added any resources try: data_dict = get_action("package_show")(context, {"id": id}) @@ -212,7 +212,7 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): dict(context, allow_state_change=True), dict(data_dict, state="active"), ) - redirect(h.url_for(controller="package", action="read", id=id)) + redirect(h.url_for('package.read', id=id)) data["package_id"] = id try: @@ -237,7 +237,7 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): dict(data_dict, state="active"), ) h.flash_notice(_("Dataset has been deleted.")) - h.redirect_to(controller="package", action="read", id=id) + h.redirect_to('package.read', id=id) elif save_action == "go-datadict": data_dict = get_action("package_show")(context, {"id": id}) @@ -253,13 +253,13 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): # redirect(h.url_for(controller='package', action='finaldict', id=id)) elif save_action == "go-dataset": # go to first stage of add dataset - h.redirect_to(controller="package", action="edit", id=id) + h.redirect_to('package.edit', id=id) elif save_action == "go-dataset-complete": # go to first stage of add dataset - h.redirect_to(controller="package", action="read", id=id) + h.redirect_to('package.edit', id=id) else: # add more resources - h.redirect_to(controller="package", action="new_resource", id=id) + h.redirect_to('package.new_resource', id=id) # get resources for sidebar context = { "model": model, From a1bd8fc877719cd0ab3a36ec76d6366a93952372 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:01:44 +0100 Subject: [PATCH 15/44] Update controller.py --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 59f7e9a..6d0e7f2 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -355,7 +355,7 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): def edit_relation(self, id, data=None, errors=None): try: - c.link = str("/dataset/relationship/new_relationship/" + id) + link = str("/dataset/relationship/new_relationship/" + id) """context = { "model": model, "session": model.Session, From f0ec446603baa9014bfb1e34bfe583581786120b Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:10:56 +0100 Subject: [PATCH 16/44] Update controller.py --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 6d0e7f2..3f70c70 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -119,7 +119,7 @@ def new_relation(self, id): def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ FIXME: This is a temporary action to allow styling of the forms. """ - linkResource = str("/dataset/new_resource/" + id) + #linkResource = str("/dataset/new_resource/" + id) if request.method == "POST" and not data: save_action = request.params.get("save") From 30f9adc612570acdc287685e024a95a20d6f2a07 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:15:19 +0100 Subject: [PATCH 17/44] Update controller.py --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 3f70c70..6d0e7f2 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -119,7 +119,7 @@ def new_relation(self, id): def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ FIXME: This is a temporary action to allow styling of the forms. """ - #linkResource = str("/dataset/new_resource/" + id) + linkResource = str("/dataset/new_resource/" + id) if request.method == "POST" and not data: save_action = request.params.get("save") From 84d229b9b5df0fb35ceea860db7040ed50db3e5a Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:17:10 +0100 Subject: [PATCH 18/44] {% set action = form_action or h.url_for(linkResource) %} --- ckanext/relation/templates/package/snippets/resource_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/templates/package/snippets/resource_form.html b/ckanext/relation/templates/package/snippets/resource_form.html index 196d9a2..db4aad2 100644 --- a/ckanext/relation/templates/package/snippets/resource_form.html +++ b/ckanext/relation/templates/package/snippets/resource_form.html @@ -2,7 +2,7 @@ {% set data = data or {} %} {% set errors = errors or {} %} -{% set action = form_action or h.url_for(linkResource) %} + {% block stages %} From b35f01fff5d2b238e70621a313a668db207e0445 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:33:00 +0100 Subject: [PATCH 19/44] Update plugin.py --- ckanext/relation/plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index 9345d44..d9317b9 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -1,8 +1,8 @@ import os import sys import ckan as ckan -import ckan.plugins as p -from ckan.common import c, request +import ckan.plugins as plugins +from ckan.common import config, request import ckan.model as model import logging from ckan.lib.plugins import DefaultTranslation From 469dbf5ddbed75d32021183b732691b0c9d79a30 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:34:09 +0100 Subject: [PATCH 20/44] Update plugin.py --- ckanext/relation/plugin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index d9317b9..97ba319 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -79,7 +79,7 @@ def get_child_package(id): try: access = p.toolkit.check_access( "package_show", - context={"user": c.user}, + context={"user": user}, data_dict={"id": rel["object"]}, ) child = p.toolkit.get_action("package_show")( @@ -148,7 +148,7 @@ def get_depend_package(id): try: access = p.toolkit.check_access( "package_show", - context={"user": c.user}, + context={"user": user}, data_dict={"id": rel["object"]}, ) dep = p.toolkit.get_action("package_show")( @@ -199,7 +199,7 @@ def get_links_package(id): try: access = p.toolkit.check_access( "package_show", - context={"user": c.user}, + context={"user": user}, data_dict={"id": rel["object"]}, ) link = p.toolkit.get_action("package_show")( From 905d31b68d97f8403d89c2c920fc440cd150bb18 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:35:49 +0100 Subject: [PATCH 21/44] Update plugin.py --- ckanext/relation/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index 97ba319..1af02e8 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -1,7 +1,7 @@ import os import sys import ckan as ckan -import ckan.plugins as plugins +import ckan.plugins as p from ckan.common import config, request import ckan.model as model import logging From e9eaffa5b97878f8a417766bb8ee9ff2943b5b07 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:35:59 +0100 Subject: [PATCH 22/44] Update controller.py --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 6d0e7f2..55cbb6a 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -1,5 +1,5 @@ import logging -import ckan.plugins as plugins +import ckan.plugins as p from ckan.lib.base import BaseController import ckan.lib.helpers as h from ckan.common import OrderedDict, _, json, request, common, g, response, config From 03817f469b0c1e3eaf3a15f51890a6164d3bcb7f Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:40:14 +0100 Subject: [PATCH 23/44] Update resource_form.html --- ckanext/relation/templates/package/snippets/resource_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/templates/package/snippets/resource_form.html b/ckanext/relation/templates/package/snippets/resource_form.html index db4aad2..196d9a2 100644 --- a/ckanext/relation/templates/package/snippets/resource_form.html +++ b/ckanext/relation/templates/package/snippets/resource_form.html @@ -2,7 +2,7 @@ {% set data = data or {} %} {% set errors = errors or {} %} - +{% set action = form_action or h.url_for(linkResource) %} {% block stages %} From 549b113b7e9315c993a62f6827698928cc3c4abd Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:54:21 +0100 Subject: [PATCH 24/44] Update controller.py --- ckanext/relation/controller.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 55cbb6a..1de6173 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -58,8 +58,8 @@ def new_relation(self, id): context = { "model": model, "session": model.Session, - "user": c.user or c.author, - "auth_user_obj": c.userobj, + "user": c.user or author, + "auth_user_obj": userobj, } # Remove button in the edit page @@ -146,8 +146,8 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): context = { "model": model, "session": model.Session, - "user": c.user or c.author, - "auth_user_obj": c.userobj, + "user": user or author, + "auth_user_obj": userobj, } # see if we have any data that we are trying to save @@ -264,8 +264,8 @@ def new_resource_ext(self, id, data=None, errors=None, error_summary=None): context = { "model": model, "session": model.Session, - "user": c.user or c.author, - "auth_user_obj": c.userobj, + "user": user or author, + "auth_user_obj": userobj, } try: pkg_dict = get_action("package_show")(context, {"id": id}) @@ -367,9 +367,9 @@ def edit_relation(self, id, data=None, errors=None): c.pkg_dict = get_action("package_show")({ "model": model, "session": model.Session, - "user": c.user or c.author, + "user": user or author, "for_view": True, - "auth_user_obj": c.userobj, + "auth_user_obj": userobj, "use_cache": False, }, {"id": id}) # c.pkg = context["package"] @@ -384,15 +384,15 @@ def relation(self, id): context = { "model": model, "session": model.Session, - "user": c.user or c.author, + "user": user or author, "for_view": True, - "auth_user_obj": c.userobj, + "auth_user_obj": userobj, "use_cache": False, } data_dict = {"id": id} try: c.pkg_dict = get_action("package_show")(context, data_dict) - dataset_type = c.pkg_dict["type"] or "dataset" + dataset_type = pkg_dict["type"] or "dataset" except NotFound: abort(404, _("Dataset not found")) except NotAuthorized: From e6c0a6f6fc9ae357b7dec5ac0f3d5b789cb4eaef Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 2 Feb 2023 10:09:02 +0100 Subject: [PATCH 25/44] Update controller.py --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index 1de6173..fa9c409 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -119,7 +119,7 @@ def new_relation(self, id): def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ FIXME: This is a temporary action to allow styling of the forms. """ - linkResource = str("/dataset/new_resource/" + id) + g.linkResource = str("/dataset/new_resource/" + id) if request.method == "POST" and not data: save_action = request.params.get("save") From cbb0b685020be2c58c535c927567fe5931673fdc Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Thu, 2 Feb 2023 10:09:19 +0100 Subject: [PATCH 26/44] Update resource_form.html --- ckanext/relation/templates/package/snippets/resource_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/templates/package/snippets/resource_form.html b/ckanext/relation/templates/package/snippets/resource_form.html index 196d9a2..b9439b0 100644 --- a/ckanext/relation/templates/package/snippets/resource_form.html +++ b/ckanext/relation/templates/package/snippets/resource_form.html @@ -2,7 +2,7 @@ {% set data = data or {} %} {% set errors = errors or {} %} -{% set action = form_action or h.url_for(linkResource) %} +{% set action = form_action or h.url_for(g.linkResource) %} {% block stages %} From 038337359b0a26ea07db7633fb458f571c86df37 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 3 Feb 2023 14:45:59 +0100 Subject: [PATCH 27/44] Update controller.py --- ckanext/relation/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/controller.py b/ckanext/relation/controller.py index fa9c409..01f800f 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/controller.py @@ -119,7 +119,7 @@ def new_relation(self, id): def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ FIXME: This is a temporary action to allow styling of the forms. """ - g.linkResource = str("/dataset/new_resource/" + id) + c.linkResource = str("/dataset/new_resource/" + id) if request.method == "POST" and not data: save_action = request.params.get("save") From 6e656e7d559f323d1150dc36f46d9f9e79617c19 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Fri, 3 Feb 2023 14:46:16 +0100 Subject: [PATCH 28/44] Update resource_form.html --- ckanext/relation/templates/package/snippets/resource_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/relation/templates/package/snippets/resource_form.html b/ckanext/relation/templates/package/snippets/resource_form.html index b9439b0..60849b3 100644 --- a/ckanext/relation/templates/package/snippets/resource_form.html +++ b/ckanext/relation/templates/package/snippets/resource_form.html @@ -2,7 +2,7 @@ {% set data = data or {} %} {% set errors = errors or {} %} -{% set action = form_action or h.url_for(g.linkResource) %} +{% set action = form_action or h.url_for(c.linkResource) %} {% block stages %} From 71717abe2cbe1e9f11fac7ca154b1febc02602b0 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Mon, 6 Feb 2023 14:00:43 +0100 Subject: [PATCH 29/44] blueprint.py --- ckanext/relation/{controller.py => blueprint.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename ckanext/relation/{controller.py => blueprint.py} (99%) diff --git a/ckanext/relation/controller.py b/ckanext/relation/blueprint.py similarity index 99% rename from ckanext/relation/controller.py rename to ckanext/relation/blueprint.py index 01f800f..4cae8ef 100644 --- a/ckanext/relation/controller.py +++ b/ckanext/relation/blueprint.py @@ -2,7 +2,7 @@ import ckan.plugins as p from ckan.lib.base import BaseController import ckan.lib.helpers as h -from ckan.common import OrderedDict, _, json, request, common, g, response, config +from ckan.common import OrderedDict, _, json, request, common, c, g, response, config from urllib.parse import urlencode import cgi from paste.deploy.converters import asbool From 44261b89c5d3ce138b0f6c8f688c5106c5159ca9 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Mon, 6 Feb 2023 14:52:18 +0100 Subject: [PATCH 30/44] Update blueprint.py --- ckanext/relation/blueprint.py | 43 ++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/ckanext/relation/blueprint.py b/ckanext/relation/blueprint.py index 4cae8ef..4dce004 100644 --- a/ckanext/relation/blueprint.py +++ b/ckanext/relation/blueprint.py @@ -1,9 +1,11 @@ import logging import ckan.plugins as p -from ckan.lib.base import BaseController + +from flask import Blueprint import ckan.lib.helpers as h -from ckan.common import OrderedDict, _, json, request, common, c, g, response, config +from ckan.common import OrderedDict, _, json, request, common, g, response, config from urllib.parse import urlencode +from ckan.plugins.toolkit import get_action, request, h import cgi from paste.deploy.converters import asbool import ckan.logic as logic @@ -13,6 +15,7 @@ import ckan.lib.render import ckan.lib.navl.dictization_functions as dict_fns +datastore = Blueprint(u'datastore', __name__) log = logging.getLogger(__name__) @@ -32,33 +35,31 @@ flatten_to_string_key = logic.flatten_to_string_key lookup_package_plugin = ckan.lib.plugins.lookup_package_plugin - -class RelationController(BaseController): - def _resource_form(self, package_type): - # backwards compatibility with plugins not inheriting from - # DefaultDatasetPlugin and not implmenting resource_form - plugin = lookup_package_plugin(package_type) - if hasattr(plugin, "resource_form"): - result = plugin.resource_form() - if result is not None: - return result +def _resource_form(self, package_type): +# backwards compatibility with plugins not inheriting from +# DefaultDatasetPlugin and not implmenting resource_form + plugin = lookup_package_plugin(package_type) + if hasattr(plugin, "resource_form"): + result = plugin.resource_form() + if result is not None: + return result return lookup_package_plugin().resource_form() - def finalrel(self, id, data=None, errors=None): - if request.method == "POST": - pass +def finalrel(self, id, data=None, errors=None): + if request.method == "POST": + pass link = str("/dataset/relationship/edit/" + id) return render("package/new_data_relation.html", extra_vars={"package_id": id}) - def new_relation(self, id): - link = str("/dataset/relationship/edit/" + id) +def new_relation(self, id): + link = str("/dataset/relationship/edit/" + id) if request.method == "POST": save_action = request.params.get("save") print("new data dictionary !!!!!!!!!!!!!!!!") context = { "model": model, "session": model.Session, - "user": c.user or author, + "user": g.user or author, "auth_user_obj": userobj, } @@ -119,7 +120,7 @@ def new_relation(self, id): def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ FIXME: This is a temporary action to allow styling of the forms. """ - c.linkResource = str("/dataset/new_resource/" + id) + g.linkResource = str("/dataset/new_resource/" + id) if request.method == "POST" and not data: save_action = request.params.get("save") @@ -364,7 +365,7 @@ def edit_relation(self, id, data=None, errors=None): "auth_user_obj": c.userobj, "use_cache": False, }""" - c.pkg_dict = get_action("package_show")({ + g.pkg_dict = get_action("package_show")({ "model": model, "session": model.Session, "user": user or author, @@ -391,7 +392,7 @@ def relation(self, id): } data_dict = {"id": id} try: - c.pkg_dict = get_action("package_show")(context, data_dict) + g.pkg_dict = get_action("package_show")(context, data_dict) dataset_type = pkg_dict["type"] or "dataset" except NotFound: abort(404, _("Dataset not found")) From 163e9874fd36b5b6f0e17d500d66f28fa6c4069f Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Mon, 6 Feb 2023 14:52:34 +0100 Subject: [PATCH 31/44] Update plugin.py --- ckanext/relation/plugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index 1af02e8..8624d1b 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -3,6 +3,7 @@ import ckan as ckan import ckan.plugins as p from ckan.common import config, request +from ckan.plugins.toolkit import get_action, request, h import ckan.model as model import logging from ckan.lib.plugins import DefaultTranslation @@ -220,7 +221,7 @@ class RelationPlugin(p.SingletonPlugin): p.implements(p.ITranslation) p.implements(p.IConfigurable) p.implements(p.ITemplateHelpers) - p.implements(p.IBlueprint, inherit=True) + p.implements(p.IBlueprint) package_link = "/dataset/" From c5fbec813ab76dfd5aa2fbbc45687d8e7a873d7d Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Mon, 6 Feb 2023 14:59:20 +0100 Subject: [PATCH 32/44] Update plugin.py --- ckanext/relation/plugin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index 8624d1b..3ad7962 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -1,6 +1,7 @@ import os import sys import ckan as ckan +from flask import Blueprint import ckan.plugins as p from ckan.common import config, request from ckan.plugins.toolkit import get_action, request, h From cd48c94c763891d1d227adca788c5dddea886e44 Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Mon, 6 Feb 2023 16:31:51 +0100 Subject: [PATCH 33/44] Update and rename blueprint.py to controller.py --- .../relation/{blueprint.py => controller.py} | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) rename ckanext/relation/{blueprint.py => controller.py} (94%) diff --git a/ckanext/relation/blueprint.py b/ckanext/relation/controller.py similarity index 94% rename from ckanext/relation/blueprint.py rename to ckanext/relation/controller.py index 4dce004..4cae8ef 100644 --- a/ckanext/relation/blueprint.py +++ b/ckanext/relation/controller.py @@ -1,11 +1,9 @@ import logging import ckan.plugins as p - -from flask import Blueprint +from ckan.lib.base import BaseController import ckan.lib.helpers as h -from ckan.common import OrderedDict, _, json, request, common, g, response, config +from ckan.common import OrderedDict, _, json, request, common, c, g, response, config from urllib.parse import urlencode -from ckan.plugins.toolkit import get_action, request, h import cgi from paste.deploy.converters import asbool import ckan.logic as logic @@ -15,7 +13,6 @@ import ckan.lib.render import ckan.lib.navl.dictization_functions as dict_fns -datastore = Blueprint(u'datastore', __name__) log = logging.getLogger(__name__) @@ -35,31 +32,33 @@ flatten_to_string_key = logic.flatten_to_string_key lookup_package_plugin = ckan.lib.plugins.lookup_package_plugin -def _resource_form(self, package_type): -# backwards compatibility with plugins not inheriting from -# DefaultDatasetPlugin and not implmenting resource_form - plugin = lookup_package_plugin(package_type) - if hasattr(plugin, "resource_form"): - result = plugin.resource_form() - if result is not None: - return result + +class RelationController(BaseController): + def _resource_form(self, package_type): + # backwards compatibility with plugins not inheriting from + # DefaultDatasetPlugin and not implmenting resource_form + plugin = lookup_package_plugin(package_type) + if hasattr(plugin, "resource_form"): + result = plugin.resource_form() + if result is not None: + return result return lookup_package_plugin().resource_form() -def finalrel(self, id, data=None, errors=None): - if request.method == "POST": - pass + def finalrel(self, id, data=None, errors=None): + if request.method == "POST": + pass link = str("/dataset/relationship/edit/" + id) return render("package/new_data_relation.html", extra_vars={"package_id": id}) -def new_relation(self, id): - link = str("/dataset/relationship/edit/" + id) + def new_relation(self, id): + link = str("/dataset/relationship/edit/" + id) if request.method == "POST": save_action = request.params.get("save") print("new data dictionary !!!!!!!!!!!!!!!!") context = { "model": model, "session": model.Session, - "user": g.user or author, + "user": c.user or author, "auth_user_obj": userobj, } @@ -120,7 +119,7 @@ def new_relation(self, id): def new_resource_ext(self, id, data=None, errors=None, error_summary=None): """ FIXME: This is a temporary action to allow styling of the forms. """ - g.linkResource = str("/dataset/new_resource/" + id) + c.linkResource = str("/dataset/new_resource/" + id) if request.method == "POST" and not data: save_action = request.params.get("save") @@ -365,7 +364,7 @@ def edit_relation(self, id, data=None, errors=None): "auth_user_obj": c.userobj, "use_cache": False, }""" - g.pkg_dict = get_action("package_show")({ + c.pkg_dict = get_action("package_show")({ "model": model, "session": model.Session, "user": user or author, @@ -392,7 +391,7 @@ def relation(self, id): } data_dict = {"id": id} try: - g.pkg_dict = get_action("package_show")(context, data_dict) + c.pkg_dict = get_action("package_show")(context, data_dict) dataset_type = pkg_dict["type"] or "dataset" except NotFound: abort(404, _("Dataset not found")) From 526545074fcd1136d402170162b722dba82b561e Mon Sep 17 00:00:00 2001 From: Marija <93824048+MarijaKnezevic@users.noreply.github.com> Date: Mon, 6 Feb 2023 16:32:02 +0100 Subject: [PATCH 34/44] Update plugin.py --- ckanext/relation/plugin.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index 3ad7962..1af02e8 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -1,10 +1,8 @@ import os import sys import ckan as ckan -from flask import Blueprint import ckan.plugins as p from ckan.common import config, request -from ckan.plugins.toolkit import get_action, request, h import ckan.model as model import logging from ckan.lib.plugins import DefaultTranslation @@ -222,7 +220,7 @@ class RelationPlugin(p.SingletonPlugin): p.implements(p.ITranslation) p.implements(p.IConfigurable) p.implements(p.ITemplateHelpers) - p.implements(p.IBlueprint) + p.implements(p.IBlueprint, inherit=True) package_link = "/dataset/" From e588f70595dfa3c530e5bbd65670ea72a1d4ad1a Mon Sep 17 00:00:00 2001 From: cirun Date: Tue, 7 Feb 2023 19:19:52 +0100 Subject: [PATCH 35/44] add .gitignore --- .gitignore | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9876d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +.ropeproject +node_modules +bower_components + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +sdist/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Sphinx documentation +docs/_build/ \ No newline at end of file From 492532020cca23cb56798037c9e51374a38e914f Mon Sep 17 00:00:00 2001 From: cirun Date: Sun, 12 Feb 2023 17:53:52 +0100 Subject: [PATCH 36/44] add support for CKAN 2.9 --- ckanext/relation/flask_plugin.py | 11 + ckanext/relation/helpers.py | 200 ++++++++++ ckanext/relation/plugin.py | 345 ++--------------- ckanext/relation/pylons_plugin.py | 106 +++++ .../css}/group_label.css | 0 .../{fanstatic => resources/css}/style.css | 0 .../js/cytoscape.min.js | 0 ckanext/relation/resources/resource.config | 8 + ckanext/relation/resources/webassets.yml | 9 + .../relation/templates/package/edit_base.html | 5 +- .../templates/package/edit_data_relation.html | 2 +- .../relation/templates/package/read_base.html | 6 +- .../templates/package/snippets/marker.html | 12 +- .../package/snippets/relation_list.html | 10 +- .../package/snippets/resource_form.html | 6 +- .../snippets/relation_css_asset.html | 1 + .../snippets/relation_css_resource.html | 1 + .../templates/snippets/relation_js_asset.html | 1 + .../snippets/relation_js_resource.html | 1 + ckanext/relation/views.py | 364 ++++++++++++++++++ 20 files changed, 746 insertions(+), 342 deletions(-) create mode 100644 ckanext/relation/flask_plugin.py create mode 100644 ckanext/relation/helpers.py create mode 100644 ckanext/relation/pylons_plugin.py rename ckanext/relation/{fanstatic => resources/css}/group_label.css (100%) rename ckanext/relation/{fanstatic => resources/css}/style.css (100%) rename ckanext/relation/{fanstatic => resources}/js/cytoscape.min.js (100%) create mode 100644 ckanext/relation/resources/resource.config create mode 100644 ckanext/relation/resources/webassets.yml create mode 100644 ckanext/relation/templates/snippets/relation_css_asset.html create mode 100644 ckanext/relation/templates/snippets/relation_css_resource.html create mode 100644 ckanext/relation/templates/snippets/relation_js_asset.html create mode 100644 ckanext/relation/templates/snippets/relation_js_resource.html create mode 100644 ckanext/relation/views.py diff --git a/ckanext/relation/flask_plugin.py b/ckanext/relation/flask_plugin.py new file mode 100644 index 0000000..d6cd772 --- /dev/null +++ b/ckanext/relation/flask_plugin.py @@ -0,0 +1,11 @@ +import ckan.plugins as p +import ckanext.relation.views as views + + +class MixinPlugin(p.SingletonPlugin): + p.implements(p.IBlueprint) + + # IBlueprint + + def get_blueprint(self): + return views.get_blueprints() diff --git a/ckanext/relation/helpers.py b/ckanext/relation/helpers.py new file mode 100644 index 0000000..5c033fc --- /dev/null +++ b/ckanext/relation/helpers.py @@ -0,0 +1,200 @@ +import ckan.plugins.toolkit as tk + + +def outgoing_relationship(id): + """Return true if the dataset has any relationship, false otherwise.""" + type = ["depends_on", "child_of" ,"links_to"] + for i in type: + try: + relationship = tk.get_action("package_relationships_list")(data_dict={"id": id, "rel": i}) + rel = bool(relationship) + break + except: + rel = 0 + return rel + + +def has_relationship(id): + """Return true if the dataset has any relationship, false otherwise.""" + relationship = tk.get_action("package_relationships_list")( + data_dict={"id": id} + ) + if relationship: + rel = bool(relationship) + else: + rel = 0 + return rel + + +def rel_id_list(rel_list): + """Return a list of already added datasets with relationships""" + + rel_ids = [] + for i in range(0, len(rel_list)): + rel_id = rel_list[i]["id"] + rel_ids.append(rel_id) + return rel_ids + + +def package_all(q): + """Return a list of all datasets searched for with value 'q'.""" + + query = (q.dict_of_lists())["q"][0] + datasets = tk.get_action("package_search")( + {}, data_dict={"q": query, "include_private": True} + ) + + result = datasets["results"] + results = [] + for res in result: + results.append(res) + return results + + +def get_child_package(id): + """ Returns a list of packages that are the subjects of child_of relationships.""" + + relationships = [] + try: + relationships = tk.get_action("package_relationships_list")( + data_dict={"id": id, "rel": "parent_of"} + ) + except Exception as e: + return {} + + children = [] + if relationships: + for rel in relationships: + try: + access = tk.check_access( + "package_show", + context={"user": tk.c.user}, + data_dict={"id": rel["object"]}, + ) + child = tk.get_action("package_show")( + data_dict={"id": rel["object"]} + ) + children.append(child) + except: + pass + return children + + +def get_parent_package(id): + """ Returns a list of packages that are the subjects of parent_of relationships.""" + + relationships = [] + try: + relationships = tk.get_action("package_relationships_list")( + data_dict={"id": id, "rel": "child_of"} + ) + except Exception as e: + return {} + + parents = [] + if relationships: + for rel in relationships: + parent = tk.get_action("package_show")(data_dict={"id": rel["object"]}) + parents.append(parent) + return parents + + +def get_dependency_package(id): + """ Returns a list of packages that are the subjects of dependency_of relationships.""" + + relationships = [] + try: + relationships = tk.get_action("package_relationships_list")( + data_dict={"id": id, "rel": "depends_on"} + ) + except Exception as e: + return {} + + dependencies = [] + if relationships: + for rel in relationships: + dependency = tk.get_action("package_show")( + data_dict={"id": rel["object"]} + ) + dependencies.append(dependency) + return dependencies + + +def get_depend_package(id): + """ Returns a list of packages that are the subjects of depends_on relationships.""" + + relationships = [] + try: + relationships = tk.get_action("package_relationships_list")( + data_dict={"id": id, "rel": "dependency_of"} + ) + except Exception as e: + return {} + + depend = [] + if relationships: + for rel in relationships: + try: + access = tk.check_access( + "package_show", + context={"user": tk.c.user}, + data_dict={"id": rel["object"]}, + ) + dep = tk.get_action("package_show")( + data_dict={"id": rel["object"]} + ) + depend.append(dep) + except: + pass + return depend + + +def get_linked_package(id): + """ Returns a list of packages that are the subjects of linked_from relationships.""" + + relationships = [] + try: + relationships = tk.get_action("package_relationships_list")( + data_dict={"id": id, "rel": "links_to"} + ) + except Exception as e: + return {} + + linked = [] + if relationships: + for rel in relationships: + lnk = tk.get_action("package_show")( + data_dict={"id": rel["object"]} + ) + linked.append(lnk) + + return linked + + +def get_links_package(id): + """ Returns a list of packages that are the subjects of links_to relationships.""" + + relationships = [] + try: + relationships = tk.get_action("package_relationships_list")( + data_dict={"id": id, "rel": "linked_from"} + ) + except Exception as e: + return {} + + links = [] + if relationships: + for rel in relationships: + try: + tk.check_access( + "package_show", + context={"user": tk.c.user}, + data_dict={"id": rel["object"]}, + ) + link = tk.get_action("package_show")( + data_dict={"id": rel["object"]} + ) + links.append(link) + except: + pass + return links diff --git a/ckanext/relation/plugin.py b/ckanext/relation/plugin.py index 1af02e8..fc7842e 100644 --- a/ckanext/relation/plugin.py +++ b/ckanext/relation/plugin.py @@ -1,226 +1,31 @@ import os -import sys -import ckan as ckan -import ckan.plugins as p -from ckan.common import config, request -import ckan.model as model import logging -from ckan.lib.plugins import DefaultTranslation - -log = logging.getLogger(__name__) - -HERE = os.path.abspath(os.path.dirname(__file__)) -I18N_DIR = os.path.join(HERE, "i18n") - -def outgoing_relationship(id): - """Return true if the dataset has any relationship, false otherwise.""" - type = ["depends_on", "child_of" ,"links_to"] - for i in type: - try: - relationship = p.toolkit.get_action("package_relationships_list")(data_dict={"id": id, "rel": i}) - rel = bool(relationship) - break - except: - rel = 0 - return rel - -def has_relationship(id): - """Return true if the dataset has any relationship, false otherwise.""" - - relationship = p.toolkit.get_action("package_relationships_list")( - data_dict={"id": id} - ) - if relationship: - rel = bool(relationship) - else: - rel = 0 - return rel - - -def rel_id_list(rel_list): - """Return a list of already added datasets with relationships""" - - rel_ids = [] - for i in range(0, len(rel_list)): - rel_id = rel_list[i]["id"] - rel_ids.append(rel_id) - return rel_ids - - -def package_all(q): - """Return a list of all datasets searched for with value 'q'.""" - - query = (q.dict_of_lists())["q"][0] - datasets = p.toolkit.get_action("package_search")( - {}, data_dict={"q": query, "include_private": True} - ) - - result = datasets["results"] - results = [] - for res in result: - results.append(res) - return results - - -def get_child_package(id): - """ Returns a list of packages that are the subjects of child_of relationships.""" - - relationships = [] - try: - relationships = p.toolkit.get_action("package_relationships_list")( - data_dict={"id": id, "rel": "parent_of"} - ) - except Exception as e: - return {} - - children = [] - if relationships: - for rel in relationships: - try: - access = p.toolkit.check_access( - "package_show", - context={"user": user}, - data_dict={"id": rel["object"]}, - ) - child = p.toolkit.get_action("package_show")( - data_dict={"id": rel["object"]} - ) - children.append(child) - except: - pass - return children - - -def get_parent_package(id): - """ Returns a list of packages that are the subjects of parent_of relationships.""" - - relationships = [] - try: - relationships = p.toolkit.get_action("package_relationships_list")( - data_dict={"id": id, "rel": "child_of"} - ) - except Exception as e: - return {} - - parents = [] - if relationships: - for rel in relationships: - parent = p.toolkit.get_action("package_show")(data_dict={"id": rel["object"]}) - parents.append(parent) - return parents - - -def get_dependency_package(id): - """ Returns a list of packages that are the subjects of dependency_of relationships.""" - - relationships = [] - try: - relationships = p.toolkit.get_action("package_relationships_list")( - data_dict={"id": id, "rel": "depends_on"} - ) - except Exception as e: - return {} - - dependencies = [] - if relationships: - for rel in relationships: - dependency = p.toolkit.get_action("package_show")( - data_dict={"id": rel["object"]} - ) - dependencies.append(dependency) - return dependencies - -def get_depend_package(id): - """ Returns a list of packages that are the subjects of depends_on relationships.""" - - relationships = [] - try: - relationships = p.toolkit.get_action("package_relationships_list")( - data_dict={"id": id, "rel": "dependency_of"} - ) - except Exception as e: - return {} - - depend = [] - if relationships: - for rel in relationships: - try: - access = p.toolkit.check_access( - "package_show", - context={"user": user}, - data_dict={"id": rel["object"]}, - ) - dep = p.toolkit.get_action("package_show")( - data_dict={"id": rel["object"]} - ) - depend.append(dep) - except: - pass - return depend - - -def get_linked_package(id): - """ Returns a list of packages that are the subjects of linked_from relationships.""" - - relationships = [] - try: - relationships = p.toolkit.get_action("package_relationships_list")( - data_dict={"id": id, "rel": "links_to"} - ) - except Exception as e: - return {} - - linked = [] - if relationships: - for rel in relationships: - lnk = p.toolkit.get_action("package_show")( - data_dict={"id": rel["object"]} - ) - linked.append(lnk) +import ckan.plugins as p +from ckan.common import config, request - return linked +import ckanext.relation.helpers as h +if p.toolkit.check_ckan_version(min_version='2.9.0'): + from ckanext.relation.flask_plugin import MixinPlugin +else: + from ckanext.relation.pylons_plugin import MixinPlugin -def get_links_package(id): - """ Returns a list of packages that are the subjects of links_to relationships.""" - relationships = [] - try: - relationships = p.toolkit.get_action("package_relationships_list")( - data_dict={"id": id, "rel": "linked_from"} - ) - except Exception as e: - return {} +log = logging.getLogger(__name__) - links = [] - if relationships: - for rel in relationships: - try: - access = p.toolkit.check_access( - "package_show", - context={"user": user}, - data_dict={"id": rel["object"]}, - ) - link = p.toolkit.get_action("package_show")( - data_dict={"id": rel["object"]} - ) - links.append(link) - except: - pass - return links +HERE = os.path.abspath(os.path.dirname(__file__)) +I18N_DIR = os.path.join(HERE, "i18n") -class RelationPlugin(p.SingletonPlugin): +class RelationPlugin(MixinPlugin, p.SingletonPlugin): """ Plugin for creating, deleting and viewing relationships """ - p.implements(p.IConfigurer) p.implements(p.ITranslation) p.implements(p.IConfigurable) p.implements(p.ITemplateHelpers) - p.implements(p.IBlueprint, inherit=True) package_link = "/dataset/" @@ -231,12 +36,12 @@ def configure(self, config): if site_url is not None: RelationPlugin.package_link = site_url + "/dataset/" - def update_config(self, config): + def update_config(self, _config): # add our templates - p.toolkit.add_template_directory(config, "templates") - p.toolkit.add_public_directory(config, "public") - p.toolkit.add_resource("fanstatic", "relation") + p.toolkit.add_template_directory(_config, "templates") + p.toolkit.add_public_directory(_config, "public") + p.toolkit.add_resource("resources", "relation") def get_helpers(self): """Register the functions above as template helper functions. @@ -244,119 +49,19 @@ def get_helpers(self): # Template helper function names should begin with the name of the # extension they belong to, to avoid clashing with functions from # other extensions. - print("something1") return { - "rel_get_child_package": get_child_package, - "rel_get_parent_package": get_parent_package, - "rel_get_dependency_package": get_dependency_package, - "rel_get_depend_package": get_depend_package, - "rel_get_links_package": get_links_package, - "rel_get_linked_package": get_linked_package, - "package_all": package_all, - "rel_id_list": rel_id_list, - "has_relationship": has_relationship, - "outgoing_relationship": outgoing_relationship, + "rel_get_child_package": h.get_child_package, + "rel_get_parent_package": h.get_parent_package, + "rel_get_dependency_package": h.get_dependency_package, + "rel_get_depend_package": h.get_depend_package, + "rel_get_links_package": h.get_links_package, + "rel_get_linked_package": h.get_linked_package, + "package_all": h.package_all, + "rel_id_list": h.rel_id_list, + "has_relationship": h.has_relationship, + "outgoing_relationship": h.outgoing_relationship, } - def before_map(self, map): - - map.connect( - "temp", - "/demp/demo", - controller="ckanext.relation.controller:RelationController", - action="index", - ) - - map.connect( - "data_relation_add", # name of path route - "/dataset/relationship/add/{id}", # url to map path to - controller="ckanext.relation.controller:RelationController", # controller - action="finalrel", # controller action (method) - ) - - map.connect( - "/dataset/new_resource/{id}", - controller="ckanext.relation.controller:RelationController", - action="new_resource_ext", - ) - - # map.connect('delete_ext', '/dataset/edit/{id}',controller='ckanext.relation.controller:RelationController', action='delete_ext') - # print('here8') - - map.connect( - "dataset_edit_relation", - "/dataset/relationship/edit/{id}", - controller="ckanext.relation.controller:RelationController", - action="edit_relation", - ckan_icon="edit", - ) - - map.connect( - "data dict button", - "/dataset/relationship/new_relationship/{id}", - controller="ckanext.relation.controller:RelationController", - action="new_relation", - ) - - map.connect( - "relation", - "/dataset/relationship/{id}", - controller="ckanext.relation.controller:RelationController", - action="relation", - ckan_icon="link", - ) - return map - - def after_map(self, map): - - map.connect( - "temp", - "/demp/demo", - controller="ckanext.relation.controller:RelationController", - action="index", - ) - - map.connect( - "data_relation_add", - "/dataset/relationship/add/{id}", - controller="ckanext.relation.controller:RelationController", - action="finalrel", - ) - - map.connect( - "/dataset/new_resource/{id}", - controller="ckanext.relation.controller:RelationController", - action="new_resource_ext", - ) - - # map.connect('delete_ext','/dataset/edit/{id}',controller='ckanext.relation.controller:RelationController', action='delete_ext') - # print('here8') - - map.connect( - "dataset_edit_relation", - "/dataset/relationship/edit/{id}", - controller="ckanext.relation.controller:RelationController", - action="edit_relation", - ckan_icon="edit", - ) - - map.connect( - "data dict button", - "/dataset/relationship/new_relationship/{id}", - controller="ckanext.relation.controller:RelationController", - action="new_relation", - ) - - map.connect( - "relation", - "/dataset/relationship/{id}", - controller="ckanext.relation.controller:RelationController", - action="relation", - ckan_icon="link", - ) - - return map - # def i18n_directory(self): # '''Change the directory of the *.mo translation files # The default implementation assumes the plugin is diff --git a/ckanext/relation/pylons_plugin.py b/ckanext/relation/pylons_plugin.py new file mode 100644 index 0000000..b8cac80 --- /dev/null +++ b/ckanext/relation/pylons_plugin.py @@ -0,0 +1,106 @@ +import ckan.plugins as p + + +class MixinPlugin(p.SingletonPlugin): + p.implements(p.IRoutes, inherit=True) + + # IRoutes + + def before_map(self, map): + + map.connect( + "temp", + "/demp/demo", + controller="ckanext.relation.controller:RelationController", + action="index", + ) + + map.connect( + "data_relation_add", # name of path route + "/dataset/relationship/add/{id}", # url to map path to + controller="ckanext.relation.controller:RelationController", # controller + action="finalrel", # controller action (method) + ) + + map.connect( + "/dataset/new_resource/{id}", + controller="ckanext.relation.controller:RelationController", + action="new_resource_ext", + ) + + # map.connect('delete_ext', '/dataset/edit/{id}',controller='ckanext.relation.controller:RelationController', action='delete_ext') + # print('here8') + + map.connect( + "dataset_edit_relation", + "/dataset/relationship/edit/{id}", + controller="ckanext.relation.controller:RelationController", + action="edit_relation", + ckan_icon="edit", + ) + + map.connect( + "data dict button", + "/dataset/relationship/new_relationship/{id}", + controller="ckanext.relation.controller:RelationController", + action="new_relation", + ) + + map.connect( + "relation", + "/dataset/relationship/{id}", + controller="ckanext.relation.controller:RelationController", + action="relation", + ckan_icon="link", + ) + return map + + def after_map(self, map): + + map.connect( + "temp", + "/demp/demo", + controller="ckanext.relation.controller:RelationController", + action="index", + ) + + map.connect( + "data_relation_add", + "/dataset/relationship/add/{id}", + controller="ckanext.relation.controller:RelationController", + action="finalrel", + ) + + map.connect( + "/dataset/new_resource/{id}", + controller="ckanext.relation.controller:RelationController", + action="new_resource_ext", + ) + + # map.connect('delete_ext','/dataset/edit/{id}',controller='ckanext.relation.controller:RelationController', action='delete_ext') + # print('here8') + + map.connect( + "dataset_edit_relation", + "/dataset/relationship/edit/{id}", + controller="ckanext.relation.controller:RelationController", + action="edit_relation", + ckan_icon="edit", + ) + + map.connect( + "data dict button", + "/dataset/relationship/new_relationship/{id}", + controller="ckanext.relation.controller:RelationController", + action="new_relation", + ) + + map.connect( + "relation", + "/dataset/relationship/{id}", + controller="ckanext.relation.controller:RelationController", + action="relation", + ckan_icon="link", + ) + + return map diff --git a/ckanext/relation/fanstatic/group_label.css b/ckanext/relation/resources/css/group_label.css similarity index 100% rename from ckanext/relation/fanstatic/group_label.css rename to ckanext/relation/resources/css/group_label.css diff --git a/ckanext/relation/fanstatic/style.css b/ckanext/relation/resources/css/style.css similarity index 100% rename from ckanext/relation/fanstatic/style.css rename to ckanext/relation/resources/css/style.css diff --git a/ckanext/relation/fanstatic/js/cytoscape.min.js b/ckanext/relation/resources/js/cytoscape.min.js similarity index 100% rename from ckanext/relation/fanstatic/js/cytoscape.min.js rename to ckanext/relation/resources/js/cytoscape.min.js diff --git a/ckanext/relation/resources/resource.config b/ckanext/relation/resources/resource.config new file mode 100644 index 0000000..7f430a5 --- /dev/null +++ b/ckanext/relation/resources/resource.config @@ -0,0 +1,8 @@ +[groups] + +main_js = + js/cytoscape.min.js + +main_css = + css/group_label.css + css/style.css \ No newline at end of file diff --git a/ckanext/relation/resources/webassets.yml b/ckanext/relation/resources/webassets.yml new file mode 100644 index 0000000..1999ce8 --- /dev/null +++ b/ckanext/relation/resources/webassets.yml @@ -0,0 +1,9 @@ +main_css: + filters: cssrewrite + contents: + - css/group_label.css + - css/style.css + +main_js: + contents: + - js/cytoscape.min.js diff --git a/ckanext/relation/templates/package/edit_base.html b/ckanext/relation/templates/package/edit_base.html index 4c02e58..f3df6ec 100644 --- a/ckanext/relation/templates/package/edit_base.html +++ b/ckanext/relation/templates/package/edit_base.html @@ -1,7 +1,6 @@ {% ckan_extends %} - +{# {% set pkg = c.pkg %} #} {% block content_primary_nav %} {{ super() }} - {{ h.build_nav_icon('dataset_edit_relation', _('Edit Relationship'), id=pkg.name) }} + {{ h.build_nav_icon('relation.edit_relation', _('Edit Relationship'), id=pkg.name) }} {% endblock %} - diff --git a/ckanext/relation/templates/package/edit_data_relation.html b/ckanext/relation/templates/package/edit_data_relation.html index b9647ba..88bb26b 100644 --- a/ckanext/relation/templates/package/edit_data_relation.html +++ b/ckanext/relation/templates/package/edit_data_relation.html @@ -1,4 +1,4 @@ -{%extends "package/edit_base.html" %} +{% extends "package/edit_base.html" %} {% set logged_in = true if c.userobj else false %} {% set res = c.resource %} diff --git a/ckanext/relation/templates/package/read_base.html b/ckanext/relation/templates/package/read_base.html index e9e7283..2051aa9 100644 --- a/ckanext/relation/templates/package/read_base.html +++ b/ckanext/relation/templates/package/read_base.html @@ -2,13 +2,9 @@ {% block subtitle %}{{ h.dataset_display_name(pkg) }} - {{ super() }}{% endblock %} - - {% block content_primary_nav %} {{ h.build_nav_icon('dataset_read', _('Dataset'), id=pkg.name) }} {{ h.build_nav_icon('dataset_groups', _('Groups'), id=pkg.name) }} - {{ h.build_nav_icon('relation', _('Relationships'), id=pkg.name) }} + {{ h.build_nav_icon('relation.relations', _('Relationships'), id=pkg.name) }} {{ h.build_nav_icon('dataset_activity', _('Activity Stream'), id=pkg.name) }} {% endblock %} - - diff --git a/ckanext/relation/templates/package/snippets/marker.html b/ckanext/relation/templates/package/snippets/marker.html index 24e97fa..edc608b 100644 --- a/ckanext/relation/templates/package/snippets/marker.html +++ b/ckanext/relation/templates/package/snippets/marker.html @@ -1,13 +1,12 @@ {% set package = package %} -{% set count = h.get_allowable_children_groups_g('main-categories') %} -{% set main_group = h.get_selected_group(package.groups, 'main-categories') %} +{# {% set count = h.get_allowable_children_groups_g('main-categories') %} +{% set main_group = h.get_selected_group(package.groups, 'main-categories') %} #} -{% resource 'relation/group_label.css' %} +{% set type = 'asset' if h.ckan_version().split('.')|map('int')|list >= [2, 9, 0] else 'resource' %} +{% include 'snippets/relation_css_' ~ type ~ '.html' %} {% set group_color = namespace(color='') %} - -
    {% for main in main_group %} {% if main.name == count[0].name %} @@ -45,8 +44,7 @@
- {% macro col()%} {% set color = group_color.color %} {{color}} -{% endmacro %} \ No newline at end of file +{% endmacro %} diff --git a/ckanext/relation/templates/package/snippets/relation_list.html b/ckanext/relation/templates/package/snippets/relation_list.html index 5abb4db..e599192 100644 --- a/ckanext/relation/templates/package/snippets/relation_list.html +++ b/ckanext/relation/templates/package/snippets/relation_list.html @@ -1,14 +1,18 @@ {% extends "package/read_base.html" %} -{% resource 'relation/group_label.css' %} +{% set type = 'asset' if h.ckan_version().split('.')|map('int')|list >= [2, 9, 0] else 'resource' %} +{% include 'snippets/relation_css_' ~ type ~ '.html' %} {% block header %} {{ super() }} -{% resource 'relation/style.css' %} + +{# {% set type = 'asset' if h.ckan_version().split('.')|map('int')|list >= [2, 9, 0] else 'resource' %} +{% include 'snippets/relation_js_' ~ type ~ '.html' %} #} + @@ -16,8 +20,8 @@ {% block primary_content_inner %} -{% set pkg = c.pkg_dict %} +{% set pkg = c.pkg_dict or pkg_dict %} {% set graph_nodes = {} %}

diff --git a/ckanext/relation/templates/package/snippets/resource_form.html b/ckanext/relation/templates/package/snippets/resource_form.html index 60849b3..edab182 100644 --- a/ckanext/relation/templates/package/snippets/resource_form.html +++ b/ckanext/relation/templates/package/snippets/resource_form.html @@ -2,7 +2,7 @@ {% set data = data or {} %} {% set errors = errors or {} %} -{% set action = form_action or h.url_for(c.linkResource) %} +{% set action = form_action %} {% block stages %} @@ -63,10 +63,10 @@ {% block buttons %} {% block delete_button %} {% if data.id %} - {% if h.check_access('resource_delete', {'id': data.id}) %} + {#{% if h.check_access('resource_delete', {'id': data.id}) %} {% set locale = h.dump_json({'content': _('Are you sure you want to delete this resource?')}) %} {% block delete_button_text %}{{ _('Delete') }}{% endblock %} - {% endif %} + {% endif %} #} {% endif %} {% endblock %} {% if stage %} diff --git a/ckanext/relation/templates/snippets/relation_css_asset.html b/ckanext/relation/templates/snippets/relation_css_asset.html new file mode 100644 index 0000000..4e1e0fb --- /dev/null +++ b/ckanext/relation/templates/snippets/relation_css_asset.html @@ -0,0 +1 @@ +{% asset 'relation/main_css' %} diff --git a/ckanext/relation/templates/snippets/relation_css_resource.html b/ckanext/relation/templates/snippets/relation_css_resource.html new file mode 100644 index 0000000..7925861 --- /dev/null +++ b/ckanext/relation/templates/snippets/relation_css_resource.html @@ -0,0 +1 @@ +{% resource 'relation/main_css' %} diff --git a/ckanext/relation/templates/snippets/relation_js_asset.html b/ckanext/relation/templates/snippets/relation_js_asset.html new file mode 100644 index 0000000..3127287 --- /dev/null +++ b/ckanext/relation/templates/snippets/relation_js_asset.html @@ -0,0 +1 @@ +{% asset 'relation/main_js' %} diff --git a/ckanext/relation/templates/snippets/relation_js_resource.html b/ckanext/relation/templates/snippets/relation_js_resource.html new file mode 100644 index 0000000..4535fd8 --- /dev/null +++ b/ckanext/relation/templates/snippets/relation_js_resource.html @@ -0,0 +1 @@ +{% resource 'relation/main_js' %} diff --git a/ckanext/relation/views.py b/ckanext/relation/views.py new file mode 100644 index 0000000..c7bfe60 --- /dev/null +++ b/ckanext/relation/views.py @@ -0,0 +1,364 @@ +import cgi + +from flask import Blueprint +from flask.views import MethodView + +import ckan.model as model +import ckan.logic as logic +import ckan.plugins.toolkit as tk +import ckan.lib.helpers as h +import ckan.lib.navl.dictization_functions as dict_fns +import ckan.lib.base as base + +from ckan.lib.plugins import lookup_package_plugin + +relation = Blueprint('relation', __name__) + +clean_dict = logic.clean_dict +tuplize_dict = logic.tuplize_dict +parse_params = logic.parse_params +NotFound = tk.ObjectNotFound +NotAuthorized = tk.NotAuthorized +ValidationError = tk.ValidationError + + +class RelationAdd(MethodView): + def get(self): + pass + + def post(self): + pass + + +def relations(id): + context = { + "model": model, + "session": model.Session, + "user": tk.c.user, + "for_view": True, + "auth_user_obj": tk.c.userobj, + "use_cache": False, + } + data_dict = {"id": id} + + try: + tk.c.pkg_dict = pkg_dict = tk.get_action("package_show")(context, data_dict) + dataset_type = pkg_dict["type"] or "dataset" + except NotFound: + tk.abort(404, tk._("Dataset not found")) + except NotAuthorized: + tk.abort(401, tk._("Unauthorized to read dataset %s") % id) + return tk.render("package/snippets/relation_list.html", { + "dataset_type": dataset_type, + "pkg_dict": pkg_dict} + ) + + +def new_relation(id): + tk.c.link = url = str("/dataset/relationship/edit/" + id) + breakpoint() + if tk.request.method == "POST": + save_action = tk.request.params.get("save") + print("new data dictionary !!!!!!!!!!!!!!!!") + context = { + "model": model, + "session": model.Session, + "user": tk.c.user, + "auth_user_obj": tk.c.userobj, + } + + # Remove button in the edit page + removed_rel = None + type_rem = None + for param in tk.request.POST: + if param.startswith("relation_remove"): + removed_rel = param.split(".")[-1] + type_rem = param.split(".")[-2] + break + if removed_rel: + data_dict = {"subject": id, "object": removed_rel, "type": type_rem} + + try: + tk.get_action("package_relationship_delete")(context, data_dict) + except NotFound: + tk.abort(404, tk._("Relationship not found")) + try: + tk.check_access("package_relationship_create", context, data_dict) + except NotAuthorized: + tk.abort( + 401, tk._("Unauthorized to create a relationship for this package") + ) + return h.redirect_to(url) + + # add button in the edit page + add_rel = None + type_add = None + for param in tk.request.POST: + if param.startswith("relation_add"): + add_rel = param.split(".")[-1] + type_add = param.split(".")[-2] + break + + if add_rel: + # context={'ignore_auth': True} + data_dict = {"subject": id, "object": add_rel, "type": type_add} + + try: + tk.get_action("package_relationship_create")(context, data_dict) + except NotFound: + tk.abort(404, tk._("Relationship cannot be created")) + + # try: + # check_access('package_relationship_create', context, {'object': add_rel, 'subject': id})#{"package_id": pkg_dict["id"]}) + # print('it is done') + # except NotAuthorized: + # abort(401, _('Unauthorized to create a relationship for this package')) + return h.redirect_to(url) + + if save_action == "go-metadata": + # XXX race condition if another user edits/deletes + h.redirect_to('package.read', id=id) + + return h.redirect_to(url) + + +def _resource_form(package_type): + # backwards compatibility with plugins not inheriting from + # DefaultDatasetPlugin and not implmenting resource_form + plugin = lookup_package_plugin(package_type) + if hasattr(plugin, "resource_form"): + result = plugin.resource_form() + if result is not None: + return result + return lookup_package_plugin().resource_form() + + +class CreateResource(MethodView): + def post(self, id, data=None, errors=None, error_summary=None): + """ FIXME: This is a temporary action to allow styling of the + forms. """ + tk.c.linkResource = str("/dataset/new_resource/" + id) + breakpoint() + save_action = tk.request.params.get("save") + # if save_action == 'go-datadict': + # redirect(h.url_for(controller='package', action='addDictionary')) + data = data or clean_dict( + dict_fns.unflatten(tuplize_dict(parse_params(tk.request.form))) + ) + # we don't want to include save as it is part of the form + del data["save"] + # if 'id' in data.keys()and save_action=="go-dataset-final": + # print("Id found","and the path is: ",request.path) + resource_id = data["id"] + # redirect(h.url_for(controller='package',action='read',id=id)) + # request.path.split("/")[2])) + # else: + # print("In else part",id," and the request path is: ", request.path) + # if save_action == 'go-dataset-final': + # redirect(h.url_for(controller='package',action='new_resource',id=id)) + # request.path.split("/")[2])) + + del data["id"] + + context = { + "model": model, + "session": model.Session, + "user": tk.c.user, + "auth_user_obj": tk.c.userobj, + } + + # see if we have any data that we are trying to save + data_provided = False + for key, value in data.items(): + if ( + value or isinstance(value, cgi.FieldStorage) + ) and key != "resource_type": + data_provided = True + break + + # if save_action == 'go-datadict': + # + # print('save action was go-datadict in the exntenstion NEEWWWW!!!!!!!!!!!') + # h.redirect_to("/dataset/relationship/add/" + id) + + if not data_provided and save_action != "go-dataset-complete": + if save_action == "go-datadict": + data_dict = tk.get_action("package_show")(context, {"id": id}) + tk.get_action("package_update")( + dict(context, allow_state_change=True), + dict(data_dict, state="active"), + ) + # h.flash_notice(_('Dataset has been deleted.')) + print( + "save action was go-datadict in the exntenstion NEEWWWW!!!!!!!!!!!" + ) + h.redirect_to("/dataset/relationship/edit/" + id) + + if save_action == "go-dataset": + # go to final stage of adddataset + h.redirect_to(h.url_for('package.edit', id=id)) + # see if we have added any resources + try: + data_dict = tk.get_action("package_show")(context, {"id": id}) + except NotAuthorized: + tk.abort(401, _("Unauthorized to update dataset")) + except NotFound: + tk.abort(404, _("The dataset {id} could not be found.").format(id=id)) + + require_resources = tk.asbool( + tk.config.get("ckan.dataset.create_on_ui_requires_resources", "True") + ) + if require_resources and not len(data_dict["resources"]): + # no data so keep on page + msg = _("You must add at least one data resource") + # On new templates do not use flash message + + if tk.asbool(tk.config.get("ckan.legacy_templates")): + h.flash_error(msg) + h.redirect_to( + controller="package", action="new_resource", id=id + ) + else: + errors = {} + error_summary = {_("Error"): msg} + return self.get(id, data, errors, error_summary) + + # XXX race condition if another user edits/deletes + data_dict = tk.get_action("package_show")(context, {"id": id}) + tk.get_action("package_update")( + dict(context, allow_state_change=True), + dict(data_dict, state="active"), + ) + h.redirect_to(h.url_for('package.read', id=id)) + + data["package_id"] = id + try: + if resource_id: + data["id"] = resource_id + tk.get_action("resource_update")(context, data) + else: + tk.get_action("resource_create")(context, data) + except ValidationError as e: + errors = e.error_dict + error_summary = e.error_summary + return self.get(id, data, errors, error_summary) + except NotAuthorized: + tk.abort(401, tk._("Unauthorized to create a resource")) + except NotFound: + tk.abort(404, tk._("The dataset {id} could not be found.").format(id=id)) + if save_action == "go-metadata": + # XXX race condition if another user edits/deletes + data_dict = tk.get_action("package_show")(context, {"id": id}) + tk.get_action("package_update")( + dict(context, allow_state_change=True), + dict(data_dict, state="active"), + ) + h.flash_notice(_("Dataset has been deleted.")) + h.redirect_to('package.read', id=id) + + elif save_action == "go-datadict": + data_dict = tk.get_action("package_show")(context, {"id": id}) + tk.get_action("package_update")( + dict(context, allow_state_change=True), + dict(data_dict, state="active"), + ) + # h.flash_notice(_('Dataset has been deleted.')) + print( + "save action was go-datadict in the exntenstion NEEWWWW!!!!!!!!!!!" + ) + h.redirect_to("/dataset/relationship/edit/" + id) + # redirect(h.url_for(controller='package', action='finaldict', id=id)) + elif save_action == "go-dataset": + # go to first stage of add dataset + h.redirect_to('package.edit', id=id) + elif save_action == "go-dataset-complete": + # go to first stage of add dataset + h.redirect_to('package.edit', id=id) + else: + # add more resources + h.redirect_to('package.new_resource', id=id) + + def get(self, id, data=None, errors=None, error_summary=None): + # get resources for sidebar + context = { + "model": model, + "session": model.Session, + "user": tk.c.user, + "auth_user_obj": tk.c.userobj, + } + breakpoint() + try: + pkg_dict = tk.get_action("package_show")(context, {"id": id}) + except NotFound: + tk.abort(404, tk._("The dataset {id} could not be found.").format(id=id)) + try: + tk.check_access("resource_create", context, {"package_id": pkg_dict["id"]}) + except NotAuthorized: + tk.abort(401, tk._("Unauthorized to create a resource for this package")) + + package_type = pkg_dict["type"] or "dataset" + + errors = errors or {} + error_summary = error_summary or {} + vars = { + "data": data, + "errors": errors, + "error_summary": error_summary, + "action": "new", + "resource_form_snippet": _resource_form(package_type), + "dataset_type": package_type, + } + vars["pkg_name"] = id + # required for nav menu + vars["pkg_dict"] = pkg_dict + template = "package/new_resource_not_draft.html" + if pkg_dict["state"].startswith("draft"): + vars["stage"] = ["complete", "active"] + template = "package/new_resource.html" + return tk.render(template, extra_vars=vars) + + +def finalrel(self, id, data=None, errors=None): + if tk.request.method == "POST": + pass + link = str("/dataset/relationship/edit/" + id) + return tk.render("package/new_data_relation.html", extra_vars={"package_id": id}) + + +def edit_relation(id, data=None, errors=None): + breakpoint() + try: + link = str("/dataset/relationship/new_relationship/" + id) + """context = { + "model": model, + "session": model.Session, + "user": c.user or c.author, + "for_view": True, + "auth_user_obj": c.userobj, + "use_cache": False, + }""" + pkg_dict = tk.get_action("package_show")({ + "model": model, + "session": model.Session, + "user": tk.c.user, + "for_view": True, + "auth_user_obj": tk.c.userobj, + "use_cache": False, + }, {"id": id}) + tk.c.pkg_dict = tk.c.pkg = pkg_dict + except NotFound: + tk.abort(404, tk._("Dataset not found")) + except NotAuthorized: + tk.abort(401, tk._("Unauthorized to read dataset %s") % id) + + return tk.render("package/edit_data_relation.html", extra_vars={"package_id": id}) + + +relation.add_url_rule("/dataset/relationship/", view_func=relations), +relation.add_url_rule("/dataset/relationship/new_relationship/", view_func=new_relation, methods=("GET", "POST")), +relation.add_url_rule("/dataset/new_resource/", view_func=CreateResource.as_view(str(u'new_res'))), +relation.add_url_rule("/dataset/relationship/add/", view_func=finalrel, methods=("GET", "POST")), +relation.add_url_rule("/dataset/relationship/edit/", view_func=edit_relation, methods=("GET", "POST")), + +def get_blueprints(): + return [relation] From 8f6c6712b127d9e32d5e06038adec4d65c6f7e84 Mon Sep 17 00:00:00 2001 From: cirun Date: Mon, 13 Feb 2023 10:47:58 +0100 Subject: [PATCH 37/44] add CKAN-2.9 support --- ckanext/relation/helpers.py | 2 +- .../relation/templates/package/edit_base.html | 3 +- ckanext/relation/templates/package/read.html | 2 +- .../snippets/data_relation_fields.html | 9 +-- .../snippets/edit_data_relation_fields.html | 4 +- .../templates/package/snippets/edit_form.html | 2 + .../package/snippets/relation_list.html | 6 +- .../package/snippets/resource_form.html | 3 +- ckanext/relation/views.py | 67 ++++++++++++------- 9 files changed, 62 insertions(+), 36 deletions(-) diff --git a/ckanext/relation/helpers.py b/ckanext/relation/helpers.py index 5c033fc..e08f23e 100644 --- a/ckanext/relation/helpers.py +++ b/ckanext/relation/helpers.py @@ -39,7 +39,7 @@ def rel_id_list(rel_list): def package_all(q): """Return a list of all datasets searched for with value 'q'.""" - query = (q.dict_of_lists())["q"][0] + query = q.get('q') datasets = tk.get_action("package_search")( {}, data_dict={"q": query, "include_private": True} ) diff --git a/ckanext/relation/templates/package/edit_base.html b/ckanext/relation/templates/package/edit_base.html index f3df6ec..7de134e 100644 --- a/ckanext/relation/templates/package/edit_base.html +++ b/ckanext/relation/templates/package/edit_base.html @@ -1,5 +1,6 @@ {% ckan_extends %} -{# {% set pkg = c.pkg %} #} + +{% set pkg = c.pkg %} {% block content_primary_nav %} {{ super() }} {{ h.build_nav_icon('relation.edit_relation', _('Edit Relationship'), id=pkg.name) }} diff --git a/ckanext/relation/templates/package/read.html b/ckanext/relation/templates/package/read.html index 479539c..d05dd41 100644 --- a/ckanext/relation/templates/package/read.html +++ b/ckanext/relation/templates/package/read.html @@ -19,7 +19,7 @@ {{ super() }} {% if h.has_relationship(pkg.id) %} - {% set url = app_globals.site_url + "/dataset/relationship/" + pkg.name %} + {% set url = h.url_for("relation.relations", id=pkg.name) %}