mirror of https://github.com/snachodog/mybuddy.git
Add a queryset param filter utility function.
This commit is contained in:
parent
2452826586
commit
a234799418
51
api/views.py
51
api/views.py
|
@ -4,6 +4,7 @@ from __future__ import unicode_literals
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|
||||||
from core.models import Baby, DiaperChange, Feeding, Sleep, TummyTime
|
from core.models import Baby, DiaperChange, Feeding, Sleep, TummyTime
|
||||||
|
from core.utils import filter_by_params
|
||||||
from .serializers import (BabySerializer, DiaperChangeSerializer,
|
from .serializers import (BabySerializer, DiaperChangeSerializer,
|
||||||
FeedingSerializer, SleepSerializer,
|
FeedingSerializer, SleepSerializer,
|
||||||
TummyTimeSerializer,)
|
TummyTimeSerializer,)
|
||||||
|
@ -14,14 +15,8 @@ class BabyViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = BabySerializer
|
serializer_class = BabySerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
queryset = Baby.objects.all()
|
params = ['first_name', 'last_name']
|
||||||
|
return filter_by_params(self.request, Baby, params)
|
||||||
for param in ['first_name', 'last_name']:
|
|
||||||
value = self.request.query_params.get(param, None)
|
|
||||||
if value is not None:
|
|
||||||
queryset = queryset.filter(**{param: value})
|
|
||||||
|
|
||||||
return queryset
|
|
||||||
|
|
||||||
|
|
||||||
class DiaperChangeViewSet(viewsets.ModelViewSet):
|
class DiaperChangeViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -29,14 +24,8 @@ class DiaperChangeViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = DiaperChangeSerializer
|
serializer_class = DiaperChangeSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
queryset = DiaperChange.objects.all()
|
params = ['baby__last_name', 'wet', 'solid', 'color']
|
||||||
|
return filter_by_params(self.request, DiaperChange, params)
|
||||||
for param in ['baby__last_name', 'wet', 'solid', 'color']:
|
|
||||||
value = self.request.query_params.get(param, None)
|
|
||||||
if value is not None:
|
|
||||||
queryset = queryset.filter(**{param: value})
|
|
||||||
|
|
||||||
return queryset
|
|
||||||
|
|
||||||
|
|
||||||
class FeedingViewSet(viewsets.ModelViewSet):
|
class FeedingViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -44,14 +33,8 @@ class FeedingViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = FeedingSerializer
|
serializer_class = FeedingSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
queryset = Feeding.objects.all()
|
params = ['baby__last_name', 'type', 'method']
|
||||||
|
return filter_by_params(self.request, Feeding, params)
|
||||||
for param in ['baby__last_name', 'type', 'method']:
|
|
||||||
value = self.request.query_params.get(param, None)
|
|
||||||
if value is not None:
|
|
||||||
queryset = queryset.filter(**{param: value})
|
|
||||||
|
|
||||||
return queryset
|
|
||||||
|
|
||||||
|
|
||||||
class SleepViewSet(viewsets.ModelViewSet):
|
class SleepViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -59,14 +42,8 @@ class SleepViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = SleepSerializer
|
serializer_class = SleepSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
queryset = Sleep.objects.all()
|
params = ['baby__last_name']
|
||||||
|
return filter_by_params(self.request, Sleep, params)
|
||||||
for param in ['baby__last_name']:
|
|
||||||
value = self.request.query_params.get(param, None)
|
|
||||||
if value is not None:
|
|
||||||
queryset = queryset.filter(**{param: value})
|
|
||||||
|
|
||||||
return queryset
|
|
||||||
|
|
||||||
|
|
||||||
class TummyTimeViewSet(viewsets.ModelViewSet):
|
class TummyTimeViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -74,11 +51,5 @@ class TummyTimeViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = TummyTimeSerializer
|
serializer_class = TummyTimeSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
queryset = TummyTime.objects.all()
|
params = ['baby__last_name']
|
||||||
|
return filter_by_params(self.request, TummyTime, params)
|
||||||
for param in ['baby__last_name']:
|
|
||||||
value = self.request.query_params.get(param, None)
|
|
||||||
if value is not None:
|
|
||||||
queryset = queryset.filter(**{param: value})
|
|
||||||
|
|
||||||
return queryset
|
|
||||||
|
|
|
@ -21,3 +21,14 @@ def duration_string(start, end):
|
||||||
'' if duration is '' else ', ', s, 's' if s > 1 else '')
|
'' if duration is '' else ', ', s, 's' if s > 1 else '')
|
||||||
|
|
||||||
return duration
|
return duration
|
||||||
|
|
||||||
|
|
||||||
|
def filter_by_params(request, model, available_params):
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue