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..5a7cdc3 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 @@ -37,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): @@ -47,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): @@ -58,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 @@ -84,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 @@ -99,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): """ @@ -130,7 +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, + '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 @@