change api date filter to datetime filter

This commit is contained in:
jcgoette 2022-04-15 21:44:25 -04:00 committed by Christopher Charbonneau Wells
parent 9d916e19f1
commit 611fb85314
3 changed files with 193 additions and 183 deletions

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from django_filters import rest_framework as filters
from core import models
from django_filters import rest_framework as filters
class CharInFilter(filters.BaseInFilter, filters.CharFilter):
@ -26,12 +25,12 @@ class TagsFieldFilter(filters.FilterSet):
class TimeFieldFilter(ChildFieldFilter):
date = filters.DateFilter(field_name="time__date", label="Date")
date_max = filters.DateFilter(
field_name="time__date", label="Max. Date", lookup_expr="lte"
date = filters.IsoDateTimeFilter(field_name="time", label="DateTime")
date_max = filters.IsoDateTimeFilter(
field_name="time", label="Max. DateTime", lookup_expr="lte"
)
date_min = filters.DateFilter(
field_name="time__date", label="Min. Date", lookup_expr="gte"
date_min = filters.IsoDateTimeFilter(
field_name="time", label="Min. DateTime", lookup_expr="gte"
)
class Meta:
@ -40,19 +39,19 @@ class TimeFieldFilter(ChildFieldFilter):
class StartEndFieldFilter(ChildFieldFilter):
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 = filters.IsoDateTimeFilter(field_name="end", label="End DateTime")
end_max = filters.IsoDateTimeFilter(
field_name="end", label="Max. End DateTime", lookup_expr="lte"
)
end_min = filters.DateFilter(
field_name="end__date", label="Min. End Date", lookup_expr="gte"
end_min = filters.IsoDateTimeFilter(
field_name="end", label="Min. End DateTime", 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 = filters.IsoDateTimeFilter(field_name="start", label="Start DateTime")
start_max = filters.IsoDateTimeFilter(
field_name="start", lookup_expr="lte", label="Max. End DateTime"
)
start_min = filters.DateFilter(
field_name="start__date", lookup_expr="gte", label="Min. Start Date"
start_min = filters.IsoDateTimeFilter(
field_name="start", lookup_expr="gte", label="Min. Start DateTime"
)
class Meta:

View File

@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APITestCase
from babybuddy.models import User
from core import models
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APITestCase
class TestBase:
@ -258,6 +256,19 @@ class FeedingAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
},
)
# check backwards compatibility
def test_get_with_date_filter(self):
response = self.client.get(self.endpoint, {"start_min": "2017-11-18"})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 3)
def test_get_with_iso_filter(self):
response = self.client.get(
self.endpoint, {"start_min": "2017-11-18T11:30:00-05:00"}
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 2)
def test_post(self):
data = {
"child": 1,

File diff suppressed because it is too large Load Diff