Add date filter to models with `time` field

Fixes #328
This commit is contained in:
Christopher C. Wells 2021-10-31 06:13:38 -07:00 committed by Christopher Charbonneau Wells
parent c215ad9d0f
commit 23f5f76db4
2 changed files with 33 additions and 4 deletions

29
api/filters.py Normal file
View File

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
from django_filters import rest_framework as filters
from core import models
class DateFromTimeFieldFilter(filters.FilterSet):
date = filters.DateFilter(field_name='date', label='Date')
date_min = filters.DateFilter(field_name='time__date', label='Min. Date',
lookup_expr='gte')
date_max = filters.DateFilter(field_name='time__date', label='Max. Date',
lookup_expr='lte')
class DiaperChangeFilter(DateFromTimeFieldFilter):
class Meta:
model = models.DiaperChange
fields = ['child', 'wet', 'solid', 'color', 'amount']
class NoteFilter(DateFromTimeFieldFilter):
class Meta:
model = models.Note
fields = ['child']
class TemperatureFilter(DateFromTimeFieldFilter):
class Meta:
model = models.Temperature
fields = ['child']

View File

@ -3,7 +3,7 @@ from rest_framework import viewsets
from core import models from core import models
from . import serializers from . import serializers, filters
from .mixins import TimerFieldSupportMixin from .mixins import TimerFieldSupportMixin
@ -17,7 +17,7 @@ class ChildViewSet(viewsets.ModelViewSet):
class DiaperChangeViewSet(viewsets.ModelViewSet): class DiaperChangeViewSet(viewsets.ModelViewSet):
queryset = models.DiaperChange.objects.all() queryset = models.DiaperChange.objects.all()
serializer_class = serializers.DiaperChangeSerializer serializer_class = serializers.DiaperChangeSerializer
filterset_fields = ('child', 'wet', 'solid', 'color', 'amount') filterset_class = filters.DiaperChangeFilter
class FeedingViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet): class FeedingViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
@ -29,7 +29,7 @@ class FeedingViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
class NoteViewSet(viewsets.ModelViewSet): class NoteViewSet(viewsets.ModelViewSet):
queryset = models.Note.objects.all() queryset = models.Note.objects.all()
serializer_class = serializers.NoteSerializer serializer_class = serializers.NoteSerializer
filterset_fields = ('child',) filterset_class = filters.NoteFilter
class SleepViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet): class SleepViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
@ -41,7 +41,7 @@ class SleepViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
class TemperatureViewSet(viewsets.ModelViewSet): class TemperatureViewSet(viewsets.ModelViewSet):
queryset = models.Temperature.objects.all() queryset = models.Temperature.objects.all()
serializer_class = serializers.TemperatureSerializer serializer_class = serializers.TemperatureSerializer
filterset_fields = ('child',) filterset_class = filters.TemperatureFilter
class TimerViewSet(viewsets.ModelViewSet): class TimerViewSet(viewsets.ModelViewSet):