Skip to content

Commit

Permalink
Merge pull request #508 from dandi/doi-config-check
Browse files Browse the repository at this point in the history
DOI config check
  • Loading branch information
dchiquito authored Sep 7, 2021
2 parents 2744de6 + b837b72 commit cb2f504
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions dandiapi/api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ class PublishConfig(AppConfig):
verbose_name = 'DANDI: Publish'

def ready(self):
import dandiapi.api.checks # noqa: F401
import dandiapi.api.signals # noqa: F401
23 changes: 23 additions & 0 deletions dandiapi/api/checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.conf import settings
from django.core.checks import Error, register

from dandiapi.api.doi import DANDI_DOI_SETTINGS


@register()
def check_doi_settings(app_configs, **kwargs):
any_doi_setting = any([setting is not None for setting, _ in DANDI_DOI_SETTINGS])
if not any_doi_setting:
# If no DOI settings are defined, DOIs will not be created on publish.
return []
errors = []
for setting, name in DANDI_DOI_SETTINGS:
if setting is None:
errors.append(
Error(
f'Setting {name} is not specified, but other DOI settings are.',
hint=f'Define {name} as an environment variable.',
obj=settings,
)
)
return errors
22 changes: 13 additions & 9 deletions dandiapi/api/doi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

from dandiapi.api.models import Version

# All of the required DOI configuration settings
DANDI_DOI_SETTINGS = [
(settings.DANDI_DOI_API_URL, 'DANDI_DOI_API_URL'),
(settings.DANDI_DOI_API_URL, 'DANDI_DOI_API_USER'),
(settings.DANDI_DOI_API_PASSWORD, 'DANDI_DOI_API_PASSWORD'),
(settings.DANDI_DOI_API_PREFIX, 'DANDI_DOI_API_PREFIX'),
]


def doi_configured() -> bool:
return any([setting is not None for setting, _ in DANDI_DOI_SETTINGS])


def _generate_doi_data(version: Version):
if settings.DANDI_ALLOW_LOCALHOST_URLS:
Expand All @@ -16,6 +28,7 @@ def _generate_doi_data(version: Version):
from dandischema.datacite import to_datacite

publish = settings.DANDI_DOI_PUBLISH
# Use the DANDI test datacite instance as a placeholder if PREFIX isn't set
prefix = settings.DANDI_DOI_API_PREFIX or '10.80507'
dandiset_id = version.dandiset.identifier
version_id = version.version
Expand All @@ -25,15 +38,6 @@ def _generate_doi_data(version: Version):
return (doi, to_datacite(metadata, publish=publish))


def doi_configured() -> bool:
return (
settings.DANDI_DOI_API_URL is not None
or settings.DANDI_DOI_API_USER is not None
or settings.DANDI_DOI_API_PASSWORD is not None
or settings.DANDI_DOI_API_PREFIX is not None
)


def create_doi(version: Version) -> str:
doi, request_body = _generate_doi_data(version)
# If DOI isn't configured, skip the API call
Expand Down

0 comments on commit cb2f504

Please sign in to comment.