ENH: Collection filterings (#30)

Fixes #29
This commit is contained in:
Matthew Ryan Dillon 2017-11-10 11:16:17 -07:00 committed by GitHub
parent 4a37d35694
commit b8e7bb9bcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 102 additions and 5 deletions

13
ccdb/api/middleware.py Normal file
View file

@ -0,0 +1,13 @@
class DeBracketifyMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
cleaned = request.GET.copy()
for key in cleaned:
if key.endswith('[]'):
val = cleaned.pop(key)
cleaned_key = key.replace('[]', '')
cleaned.setlist(cleaned_key, val)
request.GET = cleaned
return self.get_response(request)

View file

@ -24,6 +24,8 @@ router.register(r'collection-flaws',
# Projects # Projects
router.register(r'projects', projects_viewsets.ProjectViewSet) router.register(r'projects', projects_viewsets.ProjectViewSet)
# Locations # Locations
router.register(r'regions', locations_viewsets.RegionViewSet)
router.register(r'sites', locations_viewsets.SiteViewSet)
router.register(r'study-locations', locations_viewsets.StudyLocationViewSet) router.register(r'study-locations', locations_viewsets.StudyLocationViewSet)
urlpatterns = [ urlpatterns = [

View file

@ -0,0 +1,44 @@
from django_filters.filters import ModelMultipleChoiceFilter
from django_filters import rest_framework as filters
from .models import Collection, CollectionMethod
from ccdb.projects.models import Project
from ccdb.locations.models import Region, Site, StudyLocation
class CollectionFilter(filters.FilterSet):
project = ModelMultipleChoiceFilter(
name='project__id',
to_field_name='id',
queryset=Project.objects.all(),
)
region = ModelMultipleChoiceFilter(
name='study_location__site__region__id',
to_field_name='id',
queryset=Region.objects.all(),
)
site = ModelMultipleChoiceFilter(
name='study_location__site__id',
to_field_name='id',
queryset=Site.objects.all(),
)
study_location = ModelMultipleChoiceFilter(
name='study_location__id',
to_field_name='id',
queryset=StudyLocation.objects.all(),
)
collection_method = ModelMultipleChoiceFilter(
name='collection_method__id',
to_field_name='id',
queryset=CollectionMethod.objects.all(),
)
class Meta:
model = Collection
fields = ['project', 'region', 'site', 'study_location',
'collection_method', 'number_of_traps',
'collection_start_date', 'collection_end_date']

View file

@ -6,6 +6,7 @@ from .models import Collection, CollectionMethod, CollectionType, Flaw
class CollectionSerializer(serializers.ModelSerializer): class CollectionSerializer(serializers.ModelSerializer):
included_serializers = { included_serializers = {
'project': 'ccdb.projects.serializers.ProjectSerializer', 'project': 'ccdb.projects.serializers.ProjectSerializer',
'site': 'ccdb.locations.serializers.SiteSerializer',
'study_location': 'ccdb.locations.serializers.StudyLocationSerializer', 'study_location': 'ccdb.locations.serializers.StudyLocationSerializer',
'collection_method': 'collection_method':
'ccdb.collections_ccdb.serializers.CollectionMethodSerializer', 'ccdb.collections_ccdb.serializers.CollectionMethodSerializer',

View file

@ -1,5 +1,7 @@
from rest_framework import viewsets from rest_framework import viewsets
from django_filters import rest_framework as filters
from .filters import CollectionFilter
from .models import Collection, CollectionMethod, CollectionType, Flaw from .models import Collection, CollectionMethod, CollectionType, Flaw
from .serializers import (CollectionSerializer, CollectionMethodSerializer, from .serializers import (CollectionSerializer, CollectionMethodSerializer,
CollectionTypeSerializer, FlawSerializer) CollectionTypeSerializer, FlawSerializer)
@ -8,6 +10,8 @@ from .serializers import (CollectionSerializer, CollectionMethodSerializer,
class CollectionViewSet(viewsets.ModelViewSet): class CollectionViewSet(viewsets.ModelViewSet):
queryset = Collection.objects.all() queryset = Collection.objects.all()
serializer_class = CollectionSerializer serializer_class = CollectionSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_class = CollectionFilter
class CollectionMethodViewSet(viewsets.ModelViewSet): class CollectionMethodViewSet(viewsets.ModelViewSet):

View file

@ -1,10 +1,30 @@
from rest_framework import serializers from rest_framework import serializers
from .models import StudyLocation from .models import Region, Site, StudyLocation
class RegionSerializer(serializers.ModelSerializer):
class Meta:
model = Region
fields = ('name', 'code', 'sort_order')
class SiteSerializer(serializers.ModelSerializer):
included_serializers = {
'region': 'ccdb.locations.serializers.RegionSerializer',
}
class Meta:
model = Site
fields = ('name', 'code', 'description', 'sort_order', 'region')
class StudyLocationSerializer(serializers.ModelSerializer): class StudyLocationSerializer(serializers.ModelSerializer):
included_serializers = {
'site': 'ccdb.locations.serializers.SiteSerializer',
}
class Meta: class Meta:
model = StudyLocation model = StudyLocation
fields = ('name', 'code', 'study_location_type', 'treatment_type', fields = ('name', 'code', 'study_location_type', 'treatment_type',
'collecting_location', 'description', 'sort_order') 'collecting_location', 'description', 'sort_order', 'site')

View file

@ -1,7 +1,18 @@
from rest_framework import viewsets from rest_framework import viewsets
from .models import StudyLocation from .models import Region, Site, StudyLocation
from .serializers import StudyLocationSerializer from .serializers import (
RegionSerializer, SiteSerializer, StudyLocationSerializer)
class RegionViewSet(viewsets.ModelViewSet):
queryset = Region.objects.all()
serializer_class = RegionSerializer
class SiteViewSet(viewsets.ModelViewSet):
queryset = Site.objects.all()
serializer_class = SiteSerializer
class StudyLocationViewSet(viewsets.ModelViewSet): class StudyLocationViewSet(viewsets.ModelViewSet):

View file

@ -19,6 +19,7 @@ THIRD_PARTY_APPS = (
'rest_framework.authtoken', 'rest_framework.authtoken',
'corsheaders', 'corsheaders',
'djoser', 'djoser',
'django_filters',
) )
LOCAL_APPS = ( LOCAL_APPS = (
'ccdb.utils', 'ccdb.utils',
@ -43,6 +44,7 @@ MIDDLEWARE = (
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'ccdb.api.middleware.DeBracketifyMiddleware',
) )
DEBUG = env.bool("DJANGO_DEBUG", False) DEBUG = env.bool("DJANGO_DEBUG", False)

View file

@ -19,6 +19,6 @@ requests==2.11.0
# REST # REST
djangorestframework==3.6.4 djangorestframework==3.6.4
django-cors-headers==1.3.1 django-cors-headers==1.3.1
django-filter==1.0.4 django-filter==1.1
djoser==0.7.0 djoser==0.7.0
djangorestframework-jsonapi==2.1.1 djangorestframework-jsonapi==2.1.1