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
router.register(r'projects', projects_viewsets.ProjectViewSet)
# Locations
router.register(r'regions', locations_viewsets.RegionViewSet)
router.register(r'sites', locations_viewsets.SiteViewSet)
router.register(r'study-locations', locations_viewsets.StudyLocationViewSet)
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):
included_serializers = {
'project': 'ccdb.projects.serializers.ProjectSerializer',
'site': 'ccdb.locations.serializers.SiteSerializer',
'study_location': 'ccdb.locations.serializers.StudyLocationSerializer',
'collection_method':
'ccdb.collections_ccdb.serializers.CollectionMethodSerializer',

View file

@ -1,5 +1,7 @@
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 .serializers import (CollectionSerializer, CollectionMethodSerializer,
CollectionTypeSerializer, FlawSerializer)
@ -8,6 +10,8 @@ from .serializers import (CollectionSerializer, CollectionMethodSerializer,
class CollectionViewSet(viewsets.ModelViewSet):
queryset = Collection.objects.all()
serializer_class = CollectionSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_class = CollectionFilter
class CollectionMethodViewSet(viewsets.ModelViewSet):

View file

@ -1,10 +1,30 @@
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):
included_serializers = {
'site': 'ccdb.locations.serializers.SiteSerializer',
}
class Meta:
model = StudyLocation
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 .models import StudyLocation
from .serializers import StudyLocationSerializer
from .models import Region, Site, StudyLocation
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):

View file

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

View file

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