diff --git a/ccdb/api/urls.py b/ccdb/api/urls.py index f841f7a..866fc40 100644 --- a/ccdb/api/urls.py +++ b/ccdb/api/urls.py @@ -7,6 +7,7 @@ from ..utils import viewsets as utils_viewsets from ..collections_ccdb import viewsets as collections_viewsets from ..projects import viewsets as projects_viewsets from ..locations import viewsets as locations_viewsets +from ..species import viewsets as species_viewsets router = routers.DefaultRouter(trailing_slash=False) @@ -28,6 +29,10 @@ router.register(r'projects', projects_viewsets.ProjectViewSet) router.register(r'regions', locations_viewsets.RegionViewSet) router.register(r'sites', locations_viewsets.SiteViewSet) router.register(r'study-locations', locations_viewsets.StudyLocationViewSet) +# Species +router.register(r'species', species_viewsets.SpeciesViewSet) +router.register(r'collection-species', + species_viewsets.CollectionSpeciesViewSet) urlpatterns = [ url(r'^auth/login/', api_v.Login.as_view()), diff --git a/ccdb/collections_ccdb/filters.py b/ccdb/collections_ccdb/filters.py index fba756b..d934b25 100644 --- a/ccdb/collections_ccdb/filters.py +++ b/ccdb/collections_ccdb/filters.py @@ -4,6 +4,7 @@ from django_filters import rest_framework as filters from .models import Collection, CollectionMethod, ADFGPermit from ccdb.projects.models import Project from ccdb.locations.models import Region, Site, StudyLocation +from ccdb.species.models import Species class CollectionFilter(filters.FilterSet): @@ -43,9 +44,15 @@ class CollectionFilter(filters.FilterSet): queryset=ADFGPermit.objects.all(), ) + species = ModelMultipleChoiceFilter( + name='collection_species__species__id', + to_field_name='id', + queryset=Species.objects.all(), + ) + class Meta: model = Collection fields = ['project', 'region', 'site', 'study_location', 'collection_method', 'number_of_traps', 'collection_start_date', 'collection_end_date', - 'adfg_permit'] + 'adfg_permit', 'species'] diff --git a/ccdb/collections_ccdb/serializers.py b/ccdb/collections_ccdb/serializers.py index 7f243c0..95b16c8 100644 --- a/ccdb/collections_ccdb/serializers.py +++ b/ccdb/collections_ccdb/serializers.py @@ -16,6 +16,8 @@ class CollectionSerializer(serializers.ModelSerializer): 'collection_type': 'ccdb.collections_ccdb.serializers.CollectionTypeSerializer', 'flaw': 'ccdb.collections_ccdb.serializers.FlawSerializer', + 'collection_species': + 'ccdb.species.serializers.CollectionSpeciesSerializer', } class Meta: @@ -25,7 +27,8 @@ class CollectionSerializer(serializers.ModelSerializer): 'collection_start_date', 'collection_start_time', 'collection_end_date', 'collection_end_time', 'storage_location', 'specimen_state', 'process_type', - 'reagent', 'adfg_permit', 'flaw', 'display_name') + 'reagent', 'adfg_permit', 'flaw', 'display_name', + 'collection_species') class ADFGPermitSerializer(serializers.ModelSerializer): diff --git a/ccdb/species/serializers.py b/ccdb/species/serializers.py new file mode 100644 index 0000000..2e1c874 --- /dev/null +++ b/ccdb/species/serializers.py @@ -0,0 +1,21 @@ +from rest_framework_json_api import serializers + +from .models import Species, CollectionSpecies + + +class SpeciesSerializer(serializers.ModelSerializer): + class Meta: + model = Species + fields = ('common_name', 'genus', 'species', 'parasite', 'sort_order') + + +class CollectionSpeciesSerializer(serializers.ModelSerializer): + included_serializers = { + 'collection': 'ccdb.collections_ccdb.serializers.CollectionSerializer', + 'species': 'ccdb.species.serializers.SpeciesSerializer', + } + + class Meta: + model = CollectionSpecies + fields = ('id', 'sex', 'count', 'count_estimated', 'collection', + 'species') diff --git a/ccdb/species/viewsets.py b/ccdb/species/viewsets.py new file mode 100644 index 0000000..35d4097 --- /dev/null +++ b/ccdb/species/viewsets.py @@ -0,0 +1,14 @@ +from rest_framework import viewsets + +from .models import Species, CollectionSpecies +from .serializers import SpeciesSerializer, CollectionSpeciesSerializer + + +class SpeciesViewSet(viewsets.ModelViewSet): + queryset = Species.objects.all() + serializer_class = SpeciesSerializer + + +class CollectionSpeciesViewSet(viewsets.ModelViewSet): + queryset = CollectionSpecies.objects.all() + serializer_class = CollectionSpeciesSerializer