Add date filters to all relevant models

This commit is contained in:
Christopher C. Wells 2021-10-31 06:34:45 -07:00 committed by Christopher Charbonneau Wells
parent 23f5f76db4
commit 25471b2dc6
2 changed files with 47 additions and 10 deletions

View File

@ -3,7 +3,7 @@ from django_filters import rest_framework as filters
from core import models
class DateFromTimeFieldFilter(filters.FilterSet):
class TimeFieldFilter(filters.FilterSet):
date = filters.DateFilter(field_name='date', label='Date')
date_min = filters.DateFilter(field_name='time__date', label='Min. Date',
lookup_expr='gte')
@ -11,19 +11,56 @@ class DateFromTimeFieldFilter(filters.FilterSet):
lookup_expr='lte')
class DiaperChangeFilter(DateFromTimeFieldFilter):
class StartEndFieldFilter(filters.FilterSet):
end = filters.DateFilter(field_name='end__date', label='End Date')
end_min = filters.DateFilter(field_name='end__date', label='Min. End Date',
lookup_expr='gte')
end_max = filters.DateFilter(field_name='end__date', label='Max. End Date',
lookup_expr='lte')
start = filters.DateFilter(field_name='start__date', label='Start Date')
start_min = filters.DateFilter(field_name='start__date', lookup_expr='gte',
label='Min. Start Date',)
start_end = filters.DateFilter(field_name='start__date', lookup_expr='lte',
label='Max. End Date')
class DiaperChangeFilter(TimeFieldFilter):
class Meta:
model = models.DiaperChange
fields = ['child', 'wet', 'solid', 'color', 'amount']
class NoteFilter(DateFromTimeFieldFilter):
class FeedingFilter(StartEndFieldFilter):
class Meta:
model = models.Feeding
fields = ['child', 'type', 'method']
class NoteFilter(TimeFieldFilter):
class Meta:
model = models.Note
fields = ['child']
class TemperatureFilter(DateFromTimeFieldFilter):
class SleepFilter(StartEndFieldFilter):
class Meta:
model = models.Sleep
fields = ['child']
class TemperatureFilter(TimeFieldFilter):
class Meta:
model = models.Temperature
fields = ['child']
class TimerFilter(StartEndFieldFilter):
class Meta:
model = models.Timer
fields = ['child', 'active', 'user']
class TummyTimeFilter(StartEndFieldFilter):
class Meta:
model = models.TummyTime
fields = ['child']

View File

@ -11,7 +11,7 @@ class ChildViewSet(viewsets.ModelViewSet):
queryset = models.Child.objects.all()
serializer_class = serializers.ChildSerializer
lookup_field = 'slug'
filterset_fields = ('first_name', 'last_name', 'slug')
filterset_fields = ('first_name', 'last_name', 'slug', 'birth_date')
class DiaperChangeViewSet(viewsets.ModelViewSet):
@ -23,7 +23,7 @@ class DiaperChangeViewSet(viewsets.ModelViewSet):
class FeedingViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
queryset = models.Feeding.objects.all()
serializer_class = serializers.FeedingSerializer
filterset_fields = ('child', 'type', 'method')
filterset_class = filters.FeedingFilter
class NoteViewSet(viewsets.ModelViewSet):
@ -35,7 +35,7 @@ class NoteViewSet(viewsets.ModelViewSet):
class SleepViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
queryset = models.Sleep.objects.all()
serializer_class = serializers.SleepSerializer
filterset_fields = ('child',)
filterset_class = filters.SleepFilter
class TemperatureViewSet(viewsets.ModelViewSet):
@ -47,16 +47,16 @@ class TemperatureViewSet(viewsets.ModelViewSet):
class TimerViewSet(viewsets.ModelViewSet):
queryset = models.Timer.objects.all()
serializer_class = serializers.TimerSerializer
filterset_fields = ('child', 'active', 'user')
filterset_class = filters.TimerFilter
class TummyTimeViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
queryset = models.TummyTime.objects.all()
serializer_class = serializers.TummyTimeSerializer
filterset_fields = ('child',)
filterset_class = filters.TummyTimeFilter
class WeightViewSet(viewsets.ModelViewSet):
queryset = models.Weight.objects.all()
serializer_class = serializers.WeightSerializer
filterset_fields = ('child',)
filterset_fields = ('child', 'date')