Skip to content

Commit

Permalink
Merge pull request #86 from girder/filter-dataset-by-name
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeezley authored Jun 3, 2021
2 parents 5ff3214 + 0e46a39 commit e3f052c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions shapeworks_cloud/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
from . import models


class DatasetFilter(FilterSet):
class Meta:
model = models.Dataset
fields = ['name']


class SubjectFilter(FilterSet):
dataset = ModelChoiceFilter(queryset=models.Dataset.objects.all())

Expand Down
1 change: 1 addition & 0 deletions shapeworks_cloud/core/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def get_serializer_class(self) -> Type[BaseSerializer]:
class DatasetViewSet(BaseViewSet):
queryset = models.Dataset.objects.all().order_by('name')
serializer_class = serializers.DatasetSerializer
filterset_class = filters.DatasetFilter


class SubjectViewSet(BaseViewSet):
Expand Down
8 changes: 8 additions & 0 deletions swcc/swcc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ def subjects(self) -> Iterator[Subject]:
def add_subject(self, name: str) -> Subject:
return Subject(name=name, dataset=self).create()

@classmethod
def from_name(cls, name: str) -> Optional[Dataset]:
results = cls.list(name=name)
try:
return next(results)
except StopIteration:
return None


class Subject(ApiModel):
_endpoint = 'subjects'
Expand Down
14 changes: 14 additions & 0 deletions swcc/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ def test_model_crud(session, factory):

obj.delete()
assert obj.id not in {o.id for o in model.list()}


def test_get_dataset_by_name(session):
factories.DatasetFactory(name='dataset1').create()
factories.DatasetFactory(name='dataset2').create()

dataset = models.Dataset.from_name('dataset1')
assert dataset and dataset.name == 'dataset1'

dataset = models.Dataset.from_name('dataset2')
assert dataset and dataset.name == 'dataset2'

dataset = models.Dataset.from_name('dataset3')
assert dataset is None

0 comments on commit e3f052c

Please sign in to comment.