Skip to content

Commit

Permalink
Merge pull request dandi#2125 from dandi/nested-version-serializer
Browse files Browse the repository at this point in the history
Prevent duplicate serialization of dandiset in detail view
  • Loading branch information
jjnesbitt authored Jan 8, 2025
2 parents 991f342 + 1b8c3cf commit c1d4288
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
30 changes: 1 addition & 29 deletions dandiapi/api/tests/test_dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,6 @@ def test_dandiset_rest_create(api_client, user):
'asset_count': 0,
'active_uploads': 0,
'size': 0,
'dandiset': {
'identifier': DANDISET_ID_RE,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Doe, John',
'embargo_status': 'OPEN',
},
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
Expand Down Expand Up @@ -459,13 +452,6 @@ def test_dandiset_rest_create_with_identifier(api_client, admin_user):
'asset_count': 0,
'active_uploads': 0,
'size': 0,
'dandiset': {
'identifier': identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Doe, John',
'embargo_status': 'OPEN',
},
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
Expand Down Expand Up @@ -567,13 +553,6 @@ def test_dandiset_rest_create_with_contributor(api_client, admin_user):
'asset_count': 0,
'active_uploads': 0,
'size': 0,
'dandiset': {
'identifier': identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Jane Doe',
'embargo_status': 'OPEN',
},
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
Expand Down Expand Up @@ -658,13 +637,6 @@ def test_dandiset_rest_create_embargoed(api_client, user):
'asset_count': 0,
'active_uploads': 0,
'size': 0,
'dandiset': {
'identifier': DANDISET_ID_RE,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Doe, John',
'embargo_status': 'EMBARGOED',
},
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
Expand Down Expand Up @@ -1142,7 +1114,7 @@ def test_dandiset_contact_person_malformed_contributors(api_client, draft_versio
f'/api/dandisets/{draft_version.dandiset.identifier}/',
)

assert results.data['draft_version']['dandiset']['contact_person'] == ''
assert results.data['contact_person'] == ''


@pytest.mark.django_db
Expand Down
10 changes: 8 additions & 2 deletions dandiapi/api/views/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ class Meta:
dandiset = DandisetSerializer()
# name = serializers.SlugRelatedField(read_only=True, slug_field='name')

def __init__(self, *args, child_context=False, **kwargs):
if child_context:
del self.fields['dandiset']

super().__init__(*args, **kwargs)


class DandisetVersionSerializer(serializers.ModelSerializer):
"""The version serializer nested within the Dandiset Serializer."""
Expand Down Expand Up @@ -174,8 +180,8 @@ class DandisetDetailSerializer(DandisetSerializer):
class Meta(DandisetSerializer.Meta):
fields = [*DandisetSerializer.Meta.fields, 'most_recent_published_version', 'draft_version']

most_recent_published_version = VersionSerializer(read_only=True)
draft_version = VersionSerializer(read_only=True)
most_recent_published_version = VersionSerializer(read_only=True, child_context=True)
draft_version = VersionSerializer(read_only=True, child_context=True)


class DandisetQueryParameterSerializer(serializers.Serializer):
Expand Down

0 comments on commit c1d4288

Please sign in to comment.