Ensure API `OPTIONS` captures class-based filters

This commit is contained in:
Christopher C. Wells 2021-11-19 11:14:24 -05:00
parent 54a24423c0
commit 507e0c383d
2 changed files with 51 additions and 23 deletions

View File

@ -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']

View File

@ -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