diff --git a/api/filters.py b/api/filters.py index 739dc084..d831740d 100644 --- a/api/filters.py +++ b/api/filters.py @@ -3,64 +3,90 @@ from django_filters import rest_framework as filters from core import models -class TimeFieldFilter(filters.FilterSet): +class ChildFieldFilter(filters.FilterSet): + class Meta: + abstract = True + fields = ['child'] + + +class TimeFieldFilter(ChildFieldFilter): date = filters.DateFilter(field_name='time__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') + date_min = filters.DateFilter(field_name='time__date', label='Min. Date', + lookup_expr='gte') + + class Meta: + abstract = True + fields = sorted(ChildFieldFilter.Meta.fields + [ + 'date', + 'date_max', + 'date_min' + ]) -class StartEndFieldFilter(filters.FilterSet): +class StartEndFieldFilter(ChildFieldFilter): 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') + end_min = filters.DateFilter(field_name='end__date', label='Min. End Date', + lookup_expr='gte') 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', + start_max = filters.DateFilter(field_name='start__date', lookup_expr='lte', label='Max. End Date') + start_min = filters.DateFilter(field_name='start__date', lookup_expr='gte', + label='Min. Start Date') + + class Meta: + abstract = True + fields = sorted(ChildFieldFilter.Meta.fields + [ + 'end', + 'end_max', + 'end_min', + 'start', + 'start_max', + 'start_min' + ]) class DiaperChangeFilter(TimeFieldFilter): - class Meta: + class Meta(TimeFieldFilter.Meta): model = models.DiaperChange - fields = ['child', 'wet', 'solid', 'color', 'amount'] + fields = sorted(TimeFieldFilter.Meta.fields + [ + 'wet', + 'solid', + 'color', + 'amount' + ]) class FeedingFilter(StartEndFieldFilter): - class Meta: + class Meta(StartEndFieldFilter.Meta): model = models.Feeding - fields = ['child', 'type', 'method'] + fields = sorted(StartEndFieldFilter.Meta.fields + ['type', 'method']) class NoteFilter(TimeFieldFilter): - class Meta: + class Meta(TimeFieldFilter.Meta): model = models.Note - fields = ['child'] class SleepFilter(StartEndFieldFilter): - class Meta: + class Meta(StartEndFieldFilter.Meta): model = models.Sleep - fields = ['child'] class TemperatureFilter(TimeFieldFilter): - class Meta: + class Meta(TimeFieldFilter.Meta): model = models.Temperature - fields = ['child'] class TimerFilter(StartEndFieldFilter): - class Meta: + class Meta(StartEndFieldFilter.Meta): model = models.Timer - fields = ['child', 'active', 'user'] + fields = sorted(StartEndFieldFilter.Meta.fields + ['active', 'user']) class TummyTimeFilter(StartEndFieldFilter): - class Meta: + class Meta(StartEndFieldFilter.Meta): model = models.TummyTime - fields = ['child'] diff --git a/api/metadata.py b/api/metadata.py index 760b08da..d4403762 100644 --- a/api/metadata.py +++ b/api/metadata.py @@ -11,4 +11,6 @@ class APIMetadata(metadata.SimpleMetadata): data.pop('description') if hasattr(view, 'filterset_fields'): data.update({'filters': view.filterset_fields}) + elif hasattr(view, 'filterset_class'): + data.update({'filters': view.filterset_class.Meta.fields}) return data