mybuddy/api/filters.py

108 lines
3.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from core import models
from django_filters import rest_framework as filters
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):
date = filters.IsoDateTimeFilter(field_name="time", label="DateTime")
date_max = filters.IsoDateTimeFilter(
field_name="time", label="Max. DateTime", lookup_expr="lte"
2022-02-10 00:00:30 +00:00
)
date_min = filters.IsoDateTimeFilter(
field_name="time", label="Min. DateTime", lookup_expr="gte"
2022-02-10 00:00:30 +00:00
)
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):
end = filters.IsoDateTimeFilter(field_name="end", label="End DateTime")
end_max = filters.IsoDateTimeFilter(
field_name="end", label="Max. End DateTime", lookup_expr="lte"
2022-02-10 00:00:30 +00:00
)
end_min = filters.IsoDateTimeFilter(
field_name="end", label="Min. End DateTime", lookup_expr="gte"
2022-02-10 00:00:30 +00:00
)
start = filters.IsoDateTimeFilter(field_name="start", label="Start DateTime")
start_max = filters.IsoDateTimeFilter(
field_name="start", lookup_expr="lte", label="Max. End DateTime"
2022-02-10 00:00:30 +00:00
)
start_min = filters.IsoDateTimeFilter(
field_name="start", lookup_expr="gte", label="Min. Start DateTime"
2022-02-10 00:00:30 +00:00
)
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"]
)
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
2023-07-02 16:18:14 +00:00
class PumpingFilter(StartEndFieldFilter):
class Meta(StartEndFieldFilter.Meta):
2022-05-27 22:39:25 +00:00
model = models.Pumping
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
fields = sorted(StartEndFieldFilter.Meta.fields + ["user"])
2022-04-04 02:23:35 +00:00
class TummyTimeFilter(StartEndFieldFilter, TagsFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.TummyTime