Skip to content

Commit

Permalink
Merge pull request #1771 from dandi/fix-contact-person-parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandenburgh authored Dec 1, 2023
2 parents bcde511 + e895814 commit 85d8b36
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dandiapi/api/models/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def citation(cls, metadata):
version = metadata['version']
# If we can't find any contributors, use this citation format
citation = f'{name} ({year}). (Version {version}) [Data set]. DANDI archive. {url}'
if 'contributor' in metadata and metadata['contributor']:
if 'contributor' in metadata and isinstance(metadata['contributor'], list):
cl = '; '.join(
[
val['name']
Expand Down
16 changes: 16 additions & 0 deletions dandiapi/api/tests/test_dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,3 +1010,19 @@ def test_dandiset_rest_search_identifier(api_client, draft_version):

assert results[0]['draft_version']['version'] == draft_version.version
assert results[0]['draft_version']['name'] == draft_version.name


@pytest.mark.django_db()
@pytest.mark.parametrize(
'contributors',
[None, 'string', 1, [], {}],
)
def test_dandiset_contact_person_malformed_contributors(api_client, draft_version, contributors):
draft_version.metadata['contributor'] = contributors
draft_version.save()

results = api_client.get(
f'/api/dandisets/{draft_version.dandiset.identifier}/',
)

assert results.data['draft_version']['dandiset']['contact_person'] == ''
4 changes: 2 additions & 2 deletions dandiapi/api/views/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from dandiapi.search.models import AssetSearch


def extract_contact_person(version):
def extract_contact_person(version: Version) -> str:
"""Extract a version's contact person from its metadata."""
# TODO: move this logic into dandischema since it is schema-dependant
contributors = version.metadata.get('contributor')
if contributors is not None:
if contributors is not None and isinstance(contributors, list):
for contributor in contributors:
name = contributor.get('name')
role_names = contributor.get('roleName')
Expand Down

0 comments on commit 85d8b36

Please sign in to comment.