Skip to content

Commit

Permalink
merge: PR #90 from dev
Browse files Browse the repository at this point in the history
Weekly release 2023-12-04
  • Loading branch information
alycejenni authored Dec 4, 2023
2 parents 0506762 + 0648777 commit 69b9d3f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
4 changes: 3 additions & 1 deletion ckanext/doi/lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions ckanext/doi/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()))
48 changes: 27 additions & 21 deletions ckanext/doi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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):
"""
Expand All @@ -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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h3>{{ _('Cite this as') }}</h3>
</div>
{% if not res %}
<div>
{% 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' %}
<a class="btn btn-mini" title="Download BibTeX" href="https://{{ datacite_url }}/dois/application/x-bibtex/{{ pkg_dict['doi'] }}">
<small><i class="fa fa-file"></i>BibTeX</small>
</a>
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies = [
"jsonschema==3.0.0",
"xmltodict==0.12.0",
"datacite==1.1.2",
"ckantools>=0.3.0"
"ckantools>=0.4.1"
]

[project.optional-dependencies]
Expand Down

0 comments on commit 69b9d3f

Please sign in to comment.