ENH: Expose species (#32)

This commit is contained in:
Matthew Ryan Dillon 2017-11-19 17:15:50 -07:00 committed by GitHub
parent 6c5ddbec1e
commit 92a349c496
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 2 deletions

View file

@ -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()),

View file

@ -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']

View file

@ -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):

View file

@ -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')

14
ccdb/species/viewsets.py Normal file
View file

@ -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