mybuddy/api/filters.py

109 lines
3.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from django_filters import rest_framework as filters
2022-04-04 02:23:35 +00:00
from core import models
2022-04-04 02:23:35 +00:00
class CharInFilter(filters.BaseInFilter, filters.CharFilter):
pass
class ChildFieldFilter(filters.FilterSet):
class Meta:
abstract = True
2022-02-10 00:00:30 +00:00
fields = ["child"]
2022-04-04 02:23:35 +00:00
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):
2022-02-10 00:00:30 +00:00
date = filters.DateFilter(field_name="time__date", label="Date")
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
2022-02-10 00:00:30 +00:00
fields = sorted(ChildFieldFilter.Meta.fields + ["date", "date_max", "date_min"])
class StartEndFieldFilter(ChildFieldFilter):
2022-02-10 00:00:30 +00:00
end = filters.DateFilter(field_name="end__date", label="End Date")
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_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
2022-02-10 00:00:30 +00:00
fields = sorted(
ChildFieldFilter.Meta.fields
+ ["end", "end_max", "end_min", "start", "start_max", "start_min"]
)
class BreastpumpFilter(TimeFieldFilter):
class Meta(TimeFieldFilter.Meta):
model = models.Breastpump
2022-04-04 02:23:35 +00:00
class DiaperChangeFilter(TimeFieldFilter, TagsFieldFilter):
class Meta(TimeFieldFilter.Meta):
model = models.DiaperChange
2022-02-10 00:00:30 +00:00
fields = sorted(
TimeFieldFilter.Meta.fields + ["wet", "solid", "color", "amount"]
)
2022-04-04 02:23:35 +00:00
class FeedingFilter(StartEndFieldFilter, TagsFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.Feeding
2022-02-10 00:00:30 +00:00
fields = sorted(StartEndFieldFilter.Meta.fields + ["type", "method"])
2022-04-04 02:23:35 +00:00
class NoteFilter(TimeFieldFilter, TagsFieldFilter):
class Meta(TimeFieldFilter.Meta):
model = models.Note
2022-04-04 02:23:35 +00:00
class SleepFilter(StartEndFieldFilter, TagsFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.Sleep
2022-04-04 02:23:35 +00:00
class TemperatureFilter(TimeFieldFilter, TagsFieldFilter):
class Meta(TimeFieldFilter.Meta):
model = models.Temperature
class TimerFilter(StartEndFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.Timer
2022-02-10 00:00:30 +00:00
fields = sorted(StartEndFieldFilter.Meta.fields + ["active", "user"])
2022-04-04 02:23:35 +00:00
class TummyTimeFilter(StartEndFieldFilter, TagsFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.TummyTime