Replace custom API filters implementation with django-filter.

This commit is contained in:
Christopher Charbonneau Wells 2017-11-02 06:05:12 -04:00
parent 237796a644
commit d3333d68b4
5 changed files with 13 additions and 81 deletions

View File

View File

@ -1,35 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
from django.utils import timezone
from rest_framework.views import APIView
from rest_framework.test import APIRequestFactory
from api.utils import filter_by_params
from core.models import Child
class UtilsTestCase(TestCase):
def test_filter_by_params(self):
factory = APIRequestFactory()
Child.objects.create(
first_name='First',
last_name='Child',
birth_date=timezone.localdate())
Child.objects.create(
first_name='Second',
last_name='Child',
birth_date=timezone.localdate())
request = factory.get('/children/')
request = APIView().initialize_request(request)
response = filter_by_params(request, Child, [])
self.assertTrue(response, Child.objects.all())
request = factory.get('/children/', {'first_name': 'First'})
request = APIView().initialize_request(request)
response = filter_by_params(request, Child, ['first_name'])
self.assertTrue(response, Child.objects.filter(first_name='First'))

View File

@ -1,15 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
def filter_by_params(request, model, available_params):
"""Filters all instances of a model based on request parameters.
"""
queryset = model.objects.all()
for param in available_params:
value = request.query_params.get(param, None)
if value is not None:
queryset = queryset.filter(**{param: value})
return queryset

View File

@ -3,7 +3,6 @@ from __future__ import unicode_literals
from rest_framework import viewsets
from api.utils import filter_by_params
from core.models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
TummyTime)
@ -16,61 +15,40 @@ class ChildViewSet(viewsets.ModelViewSet):
queryset = Child.objects.all()
serializer_class = ChildSerializer
lookup_field = 'slug'
def get_queryset(self):
params = ['first_name', 'last_name', 'slug']
return filter_by_params(self.request, Child, params)
filter_fields = ('first_name', 'last_name', 'slug')
class DiaperChangeViewSet(viewsets.ModelViewSet):
queryset = DiaperChange.objects.all()
serializer_class = DiaperChangeSerializer
def get_queryset(self):
params = ['child__slug', 'wet', 'solid', 'color']
return filter_by_params(self.request, DiaperChange, params)
filter_fields = ('child', 'wet', 'solid', 'color')
class FeedingViewSet(viewsets.ModelViewSet):
queryset = Feeding.objects.all()
serializer_class = FeedingSerializer
def get_queryset(self):
params = ['child__slug', 'type', 'method', 'amount']
return filter_by_params(self.request, Feeding, params)
filter_fields = ('child', 'type', 'method')
class NoteViewSet(viewsets.ModelViewSet):
queryset = Note.objects.all()
serializer_class = NoteSerializer
def get_queryset(self):
params = ['child__slug']
return filter_by_params(self.request, Note, params)
filter_fields = ('child',)
class SleepViewSet(viewsets.ModelViewSet):
queryset = Sleep.objects.all()
serializer_class = SleepSerializer
def get_queryset(self):
params = ['child__slug']
return filter_by_params(self.request, Sleep, params)
filter_fields = ('child',)
class TimerViewSet(viewsets.ModelViewSet):
queryset = Timer.objects.all()
serializer_class = TimerSerializer
def get_queryset(self):
params = ['name', 'active', 'user']
return filter_by_params(self.request, Timer, params)
filter_fields = ('active', 'user')
class TummyTimeViewSet(viewsets.ModelViewSet):
queryset = TummyTime.objects.all()
serializer_class = TummyTimeSerializer
def get_queryset(self):
params = ['child__slug']
return filter_by_params(self.request, TummyTime, params)
filter_fields = ('child',)

View File

@ -25,6 +25,7 @@ INSTALLED_APPS = [
'dashboard',
'reports',
'django_filters',
'rest_framework',
'widget_tweaks',
@ -130,12 +131,15 @@ WHITENOISE_ROOT = os.path.join(BASE_DIR, 'static', 'root')
# http://www.django-rest-framework.org/#
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
],
'DEFAULT_PERMISSION_CLASSES': [
'api.permissions.BabyBuddyDjangoModelPermissions'
],
'DEFAULT_RENDERER_CLASSES': (
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
),
],
'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 100