Add tags filter to supported models

This commit is contained in:
Christopher C. Wells 2022-04-03 19:23:35 -07:00 committed by Christopher Charbonneau Wells
parent fd66d3f369
commit a779901851
1 changed files with 22 additions and 6 deletions

View File

@ -1,14 +1,30 @@
# -*- coding: utf-8 -*-
from django_filters import rest_framework as filters
from core import models
class CharInFilter(filters.BaseInFilter, filters.CharFilter):
pass
class ChildFieldFilter(filters.FilterSet):
class Meta:
abstract = True
fields = ["child"]
class TagsFieldFilter(filters.FilterSet):
tags = CharInFilter(
field_name="tags__name",
label="tag",
help_text="A list of tag names, comma separated",
)
class Meta:
abstract = True
class TimeFieldFilter(ChildFieldFilter):
date = filters.DateFilter(field_name="time__date", label="Date")
date_max = filters.DateFilter(
@ -47,7 +63,7 @@ class StartEndFieldFilter(ChildFieldFilter):
)
class DiaperChangeFilter(TimeFieldFilter):
class DiaperChangeFilter(TimeFieldFilter, TagsFieldFilter):
class Meta(TimeFieldFilter.Meta):
model = models.DiaperChange
fields = sorted(
@ -55,23 +71,23 @@ class DiaperChangeFilter(TimeFieldFilter):
)
class FeedingFilter(StartEndFieldFilter):
class FeedingFilter(StartEndFieldFilter, TagsFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.Feeding
fields = sorted(StartEndFieldFilter.Meta.fields + ["type", "method"])
class NoteFilter(TimeFieldFilter):
class NoteFilter(TimeFieldFilter, TagsFieldFilter):
class Meta(TimeFieldFilter.Meta):
model = models.Note
class SleepFilter(StartEndFieldFilter):
class SleepFilter(StartEndFieldFilter, TagsFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.Sleep
class TemperatureFilter(TimeFieldFilter):
class TemperatureFilter(TimeFieldFilter, TagsFieldFilter):
class Meta(TimeFieldFilter.Meta):
model = models.Temperature
@ -82,6 +98,6 @@ class TimerFilter(StartEndFieldFilter):
fields = sorted(StartEndFieldFilter.Meta.fields + ["active", "user"])
class TummyTimeFilter(StartEndFieldFilter):
class TummyTimeFilter(StartEndFieldFilter, TagsFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.TummyTime