Skip to content

Commit 9c36d76

Browse files
committed
Use function to supply default license value
1 parent 017f5e8 commit 9c36d76

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

dandiapi/api/management/commands/create_dev_dandiset.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from uuid import uuid4
44

5-
from dandischema.conf import get_instance_config
65
from django.conf import settings
76
from django.contrib.auth.models import User
87
from django.core.files.uploadedfile import SimpleUploadedFile
@@ -11,7 +10,11 @@
1110
from dandiapi.api.models import AssetBlob
1211
from dandiapi.api.services.asset import add_asset_to_version
1312
from dandiapi.api.services.dandiset import create_open_dandiset
14-
from dandiapi.api.services.metadata import validate_asset_metadata, validate_version_metadata
13+
from dandiapi.api.services.metadata import (
14+
get_default_license,
15+
validate_asset_metadata,
16+
validate_version_metadata,
17+
)
1518
from dandiapi.api.services.permissions.dandiset import add_dandiset_owner
1619
from dandiapi.api.tasks import calculate_sha256
1720

@@ -31,12 +34,9 @@
3134
def create_dev_dandiset(*, name: str, email: str, num_extra_owners: int):
3235
owner = User.objects.get(email=email)
3336

34-
# The licenses field is a set, sort values to ensure consistent behavior
35-
licenses = sorted(x.value for x in get_instance_config().licenses)
36-
3737
version_metadata = {
3838
'description': 'An informative description',
39-
'license': licenses[0],
39+
'license': get_default_license(),
4040
}
4141
dandiset, draft_version = create_open_dandiset(
4242
user=owner, version_name=name, version_metadata=version_metadata

dandiapi/api/services/metadata/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
logger = get_task_logger(__name__)
2626

2727

28+
def get_default_license() -> str:
29+
return sorted(x.value for x in get_instance_config().licenses)[0]
30+
31+
2832
def _encode_pydantic_error(error) -> dict[str, str]:
2933
return {'field': error['loc'][0], 'message': error['msg']}
3034

dandiapi/api/tests/factories.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
UserMetadata,
2121
Version,
2222
)
23+
from dandiapi.api.services.metadata import get_default_license
2324
from dandiapi.api.services.permissions.dandiset import add_dandiset_owner
2425

2526

@@ -129,7 +130,7 @@ def metadata(self) -> dict:
129130
'schemaKey': 'Person',
130131
}
131132
],
132-
'license': ['spdx:CC0-1.0'],
133+
'license': [get_default_license()],
133134
}
134135
# Remove faked data that might conflict with the schema types
135136
for key in ['about']:

dandiapi/api/tests/test_dandiset.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from django.utils import timezone
1010
import pytest
1111

12+
from dandiapi.api.services.metadata import get_default_license
13+
1214
if TYPE_CHECKING:
1315
from rest_framework.test import APIClient
1416

@@ -725,7 +727,11 @@ def test_dandiset_rest_create_embargoed(api_client, user):
725727
@pytest.mark.django_db
726728
def test_dandiset_rest_create_embargoed_with_award_info(authenticated_api_client: APIClient):
727729
name = 'Test Embargoed Dandiset'
728-
metadata = {'name': name, 'description': 'Test embargoed dandiset', 'license': ['spdx:CC0-1.0']}
730+
metadata = {
731+
'name': name,
732+
'description': 'Test embargoed dandiset',
733+
'license': [get_default_license()],
734+
}
729735

730736
# Create embargoed dandiset with funding and award info
731737
embargo_end_date = (timezone.now().date() + datetime.timedelta(days=365)).isoformat()
@@ -771,7 +777,11 @@ def test_dandiset_rest_create_embargoed_with_award_info(authenticated_api_client
771777
def test_dandiset_rest_create_embargoed_no_funding_info(authenticated_api_client: APIClient):
772778
"""Test creating embargoed dandiset with no funding source or award number (should succeed)."""
773779
name = 'Test Embargoed Dandiset - No Funding'
774-
metadata = {'name': name, 'description': 'Test embargoed dandiset', 'license': ['spdx:CC0-1.0']}
780+
metadata = {
781+
'name': name,
782+
'description': 'Test embargoed dandiset',
783+
'license': [get_default_license()],
784+
}
775785

776786
# Create embargoed dandiset without funding info
777787
query_params = {'embargo': 'true'}
@@ -814,7 +824,11 @@ def test_dandiset_rest_create_embargoed_no_funding_info(authenticated_api_client
814824
def test_dandiset_rest_create_embargoed_funding_no_award(authenticated_api_client: APIClient):
815825
"""Test creating embargoed dandiset with funding source but no award number (should fail)."""
816826
name = 'Test Embargoed Dandiset - Funding Only'
817-
metadata = {'name': name, 'description': 'Test embargoed dandiset', 'license': ['spdx:CC0-1.0']}
827+
metadata = {
828+
'name': name,
829+
'description': 'Test embargoed dandiset',
830+
'license': [get_default_license()],
831+
}
818832

819833
# Create embargoed dandiset with funding source but no award number
820834
query_params = {
@@ -832,7 +846,11 @@ def test_dandiset_rest_create_embargoed_funding_no_award(authenticated_api_clien
832846
def test_dandiset_rest_create_embargoed_award_no_funding(authenticated_api_client: APIClient):
833847
"""Test creating embargoed dandiset with award number but no funding source (should fail)."""
834848
name = 'Test Embargoed Dandiset - Award Only'
835-
metadata = {'name': name, 'description': 'Test embargoed dandiset', 'license': ['spdx:CC0-1.0']}
849+
metadata = {
850+
'name': name,
851+
'description': 'Test embargoed dandiset',
852+
'license': [get_default_license()],
853+
}
836854

837855
# Create embargoed dandiset with award number but no funding source
838856
query_params = {

0 commit comments

Comments
 (0)