Skip to content

Commit 2cafb50

Browse files
authored
Merge pull request #1414 from dandi/gh-1413
Add arguments for API query parameters when fetching all Dandisets; support creating embargoed Dandisets
2 parents c99385d + 1ed5a55 commit 2cafb50

File tree

3 files changed

+114
-9
lines changed

3 files changed

+114
-9
lines changed

dandi/dandiapi.py

+70-6
Original file line numberDiff line numberDiff line change
@@ -554,19 +554,83 @@ def get_dandiset(
554554
return d.for_version(version_id)
555555
return d
556556

557-
def get_dandisets(self) -> Iterator[RemoteDandiset]:
557+
def get_dandisets(
558+
self,
559+
*,
560+
draft: bool | None = None,
561+
embargoed: bool | None = None,
562+
empty: bool | None = None,
563+
mine: bool | None = None,
564+
order: str | None = None,
565+
search: str | None = None,
566+
) -> Iterator[RemoteDandiset]:
558567
"""
559568
Returns a generator of all Dandisets on the server. For each Dandiset,
560569
the `RemoteDandiset`'s version is set to the most recent published
561570
version if there is one, otherwise to the draft version.
562-
"""
563-
for data in self.paginate("/dandisets/"):
571+
572+
.. versionchanged:: 0.61.0
573+
574+
``draft``, ``embargoed``, ``empty``, ``mine``, ``order``, and
575+
``search`` parameters added
576+
577+
:param draft:
578+
If true, Dandisets that have only draft versions (i.e., that
579+
haven't yet been published) will be included in the results
580+
(default true)
581+
582+
:param embargoed:
583+
If true, embargoed Dandisets will be included in the results
584+
(default false)
585+
586+
:param empty:
587+
If true, empty Dandisets will be included in the results (default
588+
true)
589+
590+
:param mine:
591+
If true, only Dandisets owned by the authenticated user will be
592+
retrieved (default false)
593+
594+
:param order:
595+
The field to sort the results by. The accepted field names are
596+
``"id"``, ``"name"``, ``"modified"``, and ``"size"``. Prepend a
597+
hyphen to the field name to reverse the sort order.
598+
599+
:param search:
600+
A search string to filter the returned Dandisets by. The string is
601+
searched for in the metadata of Dandiset versions.
602+
"""
603+
for data in self.paginate(
604+
"/dandisets/",
605+
params={
606+
"draft": draft,
607+
"embargoed": embargoed,
608+
"empty": empty,
609+
"ordering": order,
610+
"search": search,
611+
"user": "me" if mine else None,
612+
},
613+
):
564614
yield RemoteDandiset.from_data(self, data)
565615

566-
def create_dandiset(self, name: str, metadata: dict[str, Any]) -> RemoteDandiset:
567-
"""Creates a Dandiset with the given name & metadata"""
616+
def create_dandiset(
617+
self, name: str, metadata: dict[str, Any], *, embargo: bool = False
618+
) -> RemoteDandiset:
619+
"""
620+
Creates a Dandiset with the given name & metadata. If ``embargo`` is
621+
`True`, the resulting Dandiset will be embargoed.
622+
623+
.. versionchanged:: 0.61.0
624+
625+
``embargo`` argument added
626+
"""
568627
return RemoteDandiset.from_data(
569-
self, self.post("/dandisets/", json={"name": name, "metadata": metadata})
628+
self,
629+
self.post(
630+
"/dandisets/",
631+
json={"name": name, "metadata": metadata},
632+
params={"embargo": "true" if embargo else "false"},
633+
),
570634
)
571635

572636
def check_schema_version(self, schema_version: str | None = None) -> None:

dandi/tests/fixtures.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ class SampleDandisetFactory:
520520
local_dandi_api: DandiAPI
521521
tmp_path_factory: pytest.TempPathFactory
522522

523-
def mkdandiset(self, name: str) -> SampleDandiset:
523+
def mkdandiset(self, name: str, embargo: bool = False) -> SampleDandiset:
524524
d = self.local_dandi_api.client.create_dandiset(
525525
name,
526526
# Minimal metadata needed to create a publishable Dandiset:
@@ -539,6 +539,7 @@ def mkdandiset(self, name: str) -> SampleDandiset:
539539
}
540540
],
541541
},
542+
embargo=embargo,
542543
)
543544
dspath = self.tmp_path_factory.mktemp("dandiset")
544545
(dspath / dandiset_metadata_file).write_text(f"identifier: '{d.identifier}'\n")

dandi/tests/test_dandiapi.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import requests
1717
import responses
1818

19-
from .fixtures import DandiAPI, SampleDandiset
19+
from .fixtures import DandiAPI, SampleDandiset, SampleDandisetFactory
2020
from .skip import mark
2121
from .. import dandiapi
2222
from ..consts import (
@@ -329,7 +329,47 @@ def test_check_schema_version_mismatch() -> None:
329329

330330
def test_get_dandisets(text_dandiset: SampleDandiset) -> None:
331331
dandisets = list(text_dandiset.client.get_dandisets())
332-
assert sum(1 for d in dandisets if d.identifier == text_dandiset.dandiset_id) == 1
332+
assert text_dandiset.dandiset_id in [d.identifier for d in dandisets]
333+
334+
335+
def test_get_embargoed_dandisets(
336+
sample_dandiset_factory: SampleDandisetFactory,
337+
) -> None:
338+
ds = sample_dandiset_factory.mkdandiset("Embargoed Dandiset", embargo=True)
339+
dandisets = list(ds.client.get_dandisets(embargoed=True))
340+
assert ds.dandiset_id in [d.identifier for d in dandisets]
341+
dandisets = list(ds.client.get_dandisets(embargoed=False))
342+
assert ds.dandiset_id not in [d.identifier for d in dandisets]
343+
dandisets = list(ds.client.get_dandisets(embargoed=None))
344+
assert ds.dandiset_id not in [d.identifier for d in dandisets]
345+
346+
347+
def test_get_draft_dandisets(new_dandiset: SampleDandiset) -> None:
348+
dandisets = list(new_dandiset.client.get_dandisets(draft=True))
349+
assert new_dandiset.dandiset_id in [d.identifier for d in dandisets]
350+
dandisets = list(new_dandiset.client.get_dandisets(draft=False))
351+
assert new_dandiset.dandiset_id not in [d.identifier for d in dandisets]
352+
dandisets = list(new_dandiset.client.get_dandisets(draft=None))
353+
assert new_dandiset.dandiset_id in [d.identifier for d in dandisets]
354+
355+
356+
def test_get_empty_dandisets(new_dandiset: SampleDandiset) -> None:
357+
dandisets = list(new_dandiset.client.get_dandisets(empty=True))
358+
assert new_dandiset.dandiset_id in [d.identifier for d in dandisets]
359+
dandisets = list(new_dandiset.client.get_dandisets(empty=False))
360+
assert new_dandiset.dandiset_id not in [d.identifier for d in dandisets]
361+
dandisets = list(new_dandiset.client.get_dandisets(empty=None))
362+
assert new_dandiset.dandiset_id in [d.identifier for d in dandisets]
363+
364+
365+
def test_search_get_dandisets(
366+
sample_dandiset_factory: SampleDandisetFactory,
367+
) -> None:
368+
ds = sample_dandiset_factory.mkdandiset("Unicorn Dandiset")
369+
dandisets = list(ds.client.get_dandisets(search="Unicorn"))
370+
assert ds.dandiset_id in [d.identifier for d in dandisets]
371+
dandisets = list(ds.client.get_dandisets(search="Dragon"))
372+
assert ds.dandiset_id not in [d.identifier for d in dandisets]
333373

334374

335375
def test_get_dandiset_lazy(

0 commit comments

Comments
 (0)