From b0e478761e305a4cd526ef236f7cb8d7cf7a5213 Mon Sep 17 00:00:00 2001 From: Ginger Butcher Date: Mon, 4 Dec 2023 13:20:27 +0000 Subject: [PATCH 1/3] fix: add helper to correctly determine test mode status --- ckanext/doi/lib/api.py | 4 +++- ckanext/doi/lib/helpers.py | 10 ++++++++++ ckanext/doi/plugin.py | 8 +++++++- .../theme/templates/doi/snippets/package_citation.html | 2 +- pyproject.toml | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ckanext/doi/lib/api.py b/ckanext/doi/lib/api.py index c452b00..07bb586 100644 --- a/ckanext/doi/lib/api.py +++ b/ckanext/doi/lib/api.py @@ -16,6 +16,8 @@ from datacite.errors import DataCiteError, DataCiteNotFoundError from datetime import datetime as dt +from ckanext.doi.lib.helpers import doi_test_mode + log = logging.getLogger(__name__) DEPRECATED_TEST_PREFIX = '10.5072' @@ -49,7 +51,7 @@ def test_mode(self): :return: test mode enabled as boolean (true=enabled) """ if self._test_mode is None: - self._test_mode = asbool(toolkit.config.get('ckanext.doi.test_mode', True)) + self._test_mode = doi_test_mode() return self._test_mode @classmethod diff --git a/ckanext/doi/lib/helpers.py b/ckanext/doi/lib/helpers.py index 22bdaec..df68e7e 100644 --- a/ckanext/doi/lib/helpers.py +++ b/ckanext/doi/lib/helpers.py @@ -8,6 +8,7 @@ import dateutil.parser as parser from ckan.plugins import toolkit +from ckantools.config import get_debug, get_setting def package_get_year(pkg_dict): @@ -56,3 +57,12 @@ def date_or_none(date_object_or_string): return parser.parse(date_object_or_string) else: return None + + +def doi_test_mode(): + """ + Determines whether we're running in test mode. + + :return: bool + """ + return toolkit.asbool(get_setting('ckanext.doi.test_mode', default=get_debug())) diff --git a/ckanext/doi/plugin.py b/ckanext/doi/plugin.py index c129832..98a35d6 100644 --- a/ckanext/doi/plugin.py +++ b/ckanext/doi/plugin.py @@ -11,7 +11,12 @@ from ckanext.doi import cli from ckanext.doi.lib.api import DataciteClient -from ckanext.doi.lib.helpers import get_site_title, get_site_url, package_get_year +from ckanext.doi.lib.helpers import ( + get_site_title, + get_site_url, + package_get_year, + doi_test_mode, +) from ckanext.doi.lib.metadata import build_metadata_dict, build_xml_dict from ckanext.doi.model.crud import DOIQuery @@ -133,4 +138,5 @@ def get_helpers(self): "package_get_year": package_get_year, "now": datetime.now, "get_site_title": get_site_title, + "doi_test_mode": doi_test_mode, } diff --git a/ckanext/doi/theme/templates/doi/snippets/package_citation.html b/ckanext/doi/theme/templates/doi/snippets/package_citation.html index 4626f47..6ecadd0 100644 --- a/ckanext/doi/theme/templates/doi/snippets/package_citation.html +++ b/ckanext/doi/theme/templates/doi/snippets/package_citation.html @@ -46,7 +46,7 @@

{{ _('Cite this as') }}

{% if not res %}
- {% set datacite_url = 'api.test.datacite.org' if config.get('ckanext.doi.test_mode', config.get('DEBUG', True)) else 'api.datacite.org' %} + {% set datacite_url = 'api.test.datacite.org' if h.doi_test_mode() else 'api.datacite.org' %} BibTeX diff --git a/pyproject.toml b/pyproject.toml index 74769c2..65eb887 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ dependencies = [ "jsonschema==3.0.0", "xmltodict==0.12.0", "datacite==1.1.2", - "ckantools>=0.3.0" + "ckantools>=0.4.0" ] [project.optional-dependencies] From a758ceca1fc8d18fd5e2c8edb377d9e9cf383076 Mon Sep 17 00:00:00 2001 From: Ginger Butcher Date: Mon, 4 Dec 2023 13:24:07 +0000 Subject: [PATCH 2/3] style: use single quotes --- ckanext/doi/plugin.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/ckanext/doi/plugin.py b/ckanext/doi/plugin.py index 98a35d6..5a7cdc3 100644 --- a/ckanext/doi/plugin.py +++ b/ckanext/doi/plugin.py @@ -42,7 +42,7 @@ def update_config(self, config): """ Adds templates. """ - toolkit.add_template_directory(config, "theme/templates") + toolkit.add_template_directory(config, 'theme/templates') ## IPackageController def after_dataset_create(self, context, pkg_dict): @@ -52,7 +52,7 @@ def after_dataset_create(self, context, pkg_dict): NB: This is called after creation of a dataset, before resources have been added, so state = draft. """ - DOIQuery.read_package(pkg_dict["id"], create_if_none=True) + DOIQuery.read_package(pkg_dict['id'], create_if_none=True) ## IPackageController def after_dataset_update(self, context, pkg_dict): @@ -63,17 +63,17 @@ def after_dataset_update(self, context, pkg_dict): network. """ # Is this active and public? If so we need to make sure we have an active DOI - if pkg_dict.get("state", "active") == "active" and not pkg_dict.get( - "private", False + if pkg_dict.get('state', 'active') == 'active' and not pkg_dict.get( + 'private', False ): - package_id = pkg_dict["id"] + package_id = pkg_dict['id'] # remove user-defined update schemas first (if needed) - context.pop("schema", None) + context.pop('schema', None) # Load the package_show version of the dict - pkg_show_dict = toolkit.get_action("package_show")( - context, {"id": package_id} + pkg_show_dict = toolkit.get_action('package_show')( + context, {'id': package_id} ) # Load or create the local DOI (package may not have a DOI if extension was loaded @@ -89,13 +89,13 @@ def after_dataset_update(self, context, pkg_dict): # metadata gets created before minting client.set_metadata(doi.identifier, xml_dict) client.mint_doi(doi.identifier, package_id) - toolkit.h.flash_success("DataCite DOI created") + toolkit.h.flash_success('DataCite DOI created') else: same = client.check_for_update(doi.identifier, xml_dict) if not same: # Not the same, so we want to update the metadata client.set_metadata(doi.identifier, xml_dict) - toolkit.h.flash_success("DataCite DOI metadata updated") + toolkit.h.flash_success('DataCite DOI metadata updated') return pkg_dict @@ -104,15 +104,15 @@ def after_dataset_show(self, context, pkg_dict): """ Add the DOI details to the pkg_dict so it can be displayed. """ - doi = DOIQuery.read_package(pkg_dict["id"]) + doi = DOIQuery.read_package(pkg_dict['id']) if doi: - pkg_dict["doi"] = doi.identifier - pkg_dict["doi_status"] = True if doi.published else False - pkg_dict["domain"] = get_site_url().replace("http://", "") - pkg_dict["doi_date_published"] = ( - datetime.strftime(doi.published, "%Y-%m-%d") if doi.published else None + pkg_dict['doi'] = doi.identifier + pkg_dict['doi_status'] = True if doi.published else False + pkg_dict['domain'] = get_site_url().replace('http://', '') + pkg_dict['doi_date_published'] = ( + datetime.strftime(doi.published, '%Y-%m-%d') if doi.published else None ) - pkg_dict["doi_publisher"] = toolkit.config.get("ckanext.doi.publisher") + pkg_dict['doi_publisher'] = toolkit.config.get('ckanext.doi.publisher') def after_create(self, *args, **kwargs): """ @@ -135,8 +135,8 @@ def after_show(self, *args, **kwargs): # ITemplateHelpers def get_helpers(self): return { - "package_get_year": package_get_year, - "now": datetime.now, - "get_site_title": get_site_title, - "doi_test_mode": doi_test_mode, + 'package_get_year': package_get_year, + 'now': datetime.now, + 'get_site_title': get_site_title, + 'doi_test_mode': doi_test_mode, } From 28e49e4ac23c4df4c22db9b2b83a992018d46acc Mon Sep 17 00:00:00 2001 From: Alice Butcher Date: Mon, 4 Dec 2023 13:58:00 +0000 Subject: [PATCH 3/3] fix: update ckantools to patch bug with get_setting --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 65eb887..f135366 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ dependencies = [ "jsonschema==3.0.0", "xmltodict==0.12.0", "datacite==1.1.2", - "ckantools>=0.4.0" + "ckantools>=0.4.1" ] [project.optional-dependencies]