mirror of https://github.com/snachodog/mybuddy.git
Replace custom API filters implementation with django-filter.
This commit is contained in:
parent
237796a644
commit
d3333d68b4
|
@ -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'))
|
|
15
api/utils.py
15
api/utils.py
|
@ -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
|
|
36
api/views.py
36
api/views.py
|
@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|
||||||
from api.utils import filter_by_params
|
|
||||||
from core.models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
|
from core.models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
|
||||||
TummyTime)
|
TummyTime)
|
||||||
|
|
||||||
|
@ -16,61 +15,40 @@ class ChildViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Child.objects.all()
|
queryset = Child.objects.all()
|
||||||
serializer_class = ChildSerializer
|
serializer_class = ChildSerializer
|
||||||
lookup_field = 'slug'
|
lookup_field = 'slug'
|
||||||
|
filter_fields = ('first_name', 'last_name', 'slug')
|
||||||
def get_queryset(self):
|
|
||||||
params = ['first_name', 'last_name', 'slug']
|
|
||||||
return filter_by_params(self.request, Child, params)
|
|
||||||
|
|
||||||
|
|
||||||
class DiaperChangeViewSet(viewsets.ModelViewSet):
|
class DiaperChangeViewSet(viewsets.ModelViewSet):
|
||||||
queryset = DiaperChange.objects.all()
|
queryset = DiaperChange.objects.all()
|
||||||
serializer_class = DiaperChangeSerializer
|
serializer_class = DiaperChangeSerializer
|
||||||
|
filter_fields = ('child', 'wet', 'solid', 'color')
|
||||||
def get_queryset(self):
|
|
||||||
params = ['child__slug', 'wet', 'solid', 'color']
|
|
||||||
return filter_by_params(self.request, DiaperChange, params)
|
|
||||||
|
|
||||||
|
|
||||||
class FeedingViewSet(viewsets.ModelViewSet):
|
class FeedingViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Feeding.objects.all()
|
queryset = Feeding.objects.all()
|
||||||
serializer_class = FeedingSerializer
|
serializer_class = FeedingSerializer
|
||||||
|
filter_fields = ('child', 'type', 'method')
|
||||||
def get_queryset(self):
|
|
||||||
params = ['child__slug', 'type', 'method', 'amount']
|
|
||||||
return filter_by_params(self.request, Feeding, params)
|
|
||||||
|
|
||||||
|
|
||||||
class NoteViewSet(viewsets.ModelViewSet):
|
class NoteViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Note.objects.all()
|
queryset = Note.objects.all()
|
||||||
serializer_class = NoteSerializer
|
serializer_class = NoteSerializer
|
||||||
|
filter_fields = ('child',)
|
||||||
def get_queryset(self):
|
|
||||||
params = ['child__slug']
|
|
||||||
return filter_by_params(self.request, Note, params)
|
|
||||||
|
|
||||||
|
|
||||||
class SleepViewSet(viewsets.ModelViewSet):
|
class SleepViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Sleep.objects.all()
|
queryset = Sleep.objects.all()
|
||||||
serializer_class = SleepSerializer
|
serializer_class = SleepSerializer
|
||||||
|
filter_fields = ('child',)
|
||||||
def get_queryset(self):
|
|
||||||
params = ['child__slug']
|
|
||||||
return filter_by_params(self.request, Sleep, params)
|
|
||||||
|
|
||||||
|
|
||||||
class TimerViewSet(viewsets.ModelViewSet):
|
class TimerViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Timer.objects.all()
|
queryset = Timer.objects.all()
|
||||||
serializer_class = TimerSerializer
|
serializer_class = TimerSerializer
|
||||||
|
filter_fields = ('active', 'user')
|
||||||
def get_queryset(self):
|
|
||||||
params = ['name', 'active', 'user']
|
|
||||||
return filter_by_params(self.request, Timer, params)
|
|
||||||
|
|
||||||
|
|
||||||
class TummyTimeViewSet(viewsets.ModelViewSet):
|
class TummyTimeViewSet(viewsets.ModelViewSet):
|
||||||
queryset = TummyTime.objects.all()
|
queryset = TummyTime.objects.all()
|
||||||
serializer_class = TummyTimeSerializer
|
serializer_class = TummyTimeSerializer
|
||||||
|
filter_fields = ('child',)
|
||||||
def get_queryset(self):
|
|
||||||
params = ['child__slug']
|
|
||||||
return filter_by_params(self.request, TummyTime, params)
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ INSTALLED_APPS = [
|
||||||
'dashboard',
|
'dashboard',
|
||||||
'reports',
|
'reports',
|
||||||
|
|
||||||
|
'django_filters',
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'widget_tweaks',
|
'widget_tweaks',
|
||||||
|
|
||||||
|
@ -130,12 +131,15 @@ WHITENOISE_ROOT = os.path.join(BASE_DIR, 'static', 'root')
|
||||||
# http://www.django-rest-framework.org/#
|
# http://www.django-rest-framework.org/#
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
|
'DEFAULT_FILTER_BACKENDS': [
|
||||||
|
'django_filters.rest_framework.DjangoFilterBackend',
|
||||||
|
],
|
||||||
'DEFAULT_PERMISSION_CLASSES': [
|
'DEFAULT_PERMISSION_CLASSES': [
|
||||||
'api.permissions.BabyBuddyDjangoModelPermissions'
|
'api.permissions.BabyBuddyDjangoModelPermissions'
|
||||||
],
|
],
|
||||||
'DEFAULT_RENDERER_CLASSES': (
|
'DEFAULT_RENDERER_CLASSES': [
|
||||||
'rest_framework.renderers.JSONRenderer',
|
'rest_framework.renderers.JSONRenderer',
|
||||||
),
|
],
|
||||||
'DEFAULT_PAGINATION_CLASS':
|
'DEFAULT_PAGINATION_CLASS':
|
||||||
'rest_framework.pagination.LimitOffsetPagination',
|
'rest_framework.pagination.LimitOffsetPagination',
|
||||||
'PAGE_SIZE': 100
|
'PAGE_SIZE': 100
|
||||||
|
|
Loading…
Reference in New Issue