Permalink
Browse files

add date filter to history API

  • Loading branch information...
tiltec committed Jul 3, 2017
1 parent 8ef99b3 commit 562919221c95be8ccc6fcb335dc22b79f769b254
Showing with 55 additions and 14 deletions.
  1. +2 −14 foodsaving/history/api.py
  2. +39 −0 foodsaving/history/filters.py
  3. +14 −0 foodsaving/history/tests/test_api.py
@@ -1,25 +1,13 @@
from django_filters.rest_framework import filters
from django_filters.rest_framework import FilterSet
from rest_framework import viewsets
from rest_framework.filters import DjangoFilterBackend
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.permissions import IsAuthenticated

from foodsaving.history.models import History, HistoryTypus
from foodsaving.history.filters import HistoryFilter
from foodsaving.history.models import History
from foodsaving.history.serializers import HistorySerializer


def filter_history_typus(qs, field, value):
return qs.filter(**{field: getattr(HistoryTypus, value)})


class HistoryFilter(FilterSet):
class Meta:
model = History
fields = ('group', 'store', 'users', 'typus')
typus = filters.ChoiceFilter(choices=HistoryTypus.items(), method=filter_history_typus)


class HistoryPagination(LimitOffsetPagination):
default_limit = 50
max_limit = 1000
@@ -0,0 +1,39 @@
import django_filters
from django import forms
from django.utils.dateparse import parse_datetime
from django.utils.encoding import force_str
from django_filters.fields import RangeField
from django_filters.rest_framework import filters, FilterSet

from foodsaving.history.models import HistoryTypus, History


class ISODateTimeField(forms.DateTimeField):
def strptime(self, value, format):
return parse_datetime(force_str(value))


class DateTimeRangeField(RangeField):
def __init__(self, *args, **kwargs):
fields = (
ISODateTimeField(),
ISODateTimeField())
super(DateTimeRangeField, self).__init__(fields, *args, **kwargs)


class DateTimeFromToRangeFilter(django_filters.RangeFilter):
field_class = DateTimeRangeField


def filter_history_typus(qs, field, value):
return qs.filter(**{field: getattr(HistoryTypus, value)})


class HistoryFilter(FilterSet):
typus = filters.ChoiceFilter(choices=HistoryTypus.items(), method=filter_history_typus)
date = DateTimeFromToRangeFilter(name='date')

class Meta:
model = History
fields = ('group', 'store', 'users', 'typus', 'date')

@@ -258,3 +258,17 @@ def test_filter_pickup_done(self):
self.assertEqual(response.data[0]['typus'], 'PICKUP_MISSED')
response = self.get_results(history_url, {'typus': 'GROUP_JOIN'}) # unrelated event should give no result
self.assertEqual(len(response.data), 0)


class TestHistoryAPIDateFiltering(PaginatedResponseTestCase):
@classmethod
def setUpTestData(cls):
cls.member = UserFactory()

def test_filter_by_date(self):
self.client.force_login(self.member)
self.client.post('/api/groups/', {'name': 'xyzabc', 'timezone': 'Europe/Berlin'})
response = self.get_results(history_url, data={'date_1': timezone.now()})
self.assertEqual(len(response.data), 1)
response = self.get_results(history_url, data={'date_0': timezone.now()})
self.assertEqual(len(response.data), 0)

0 comments on commit 5629192

Please sign in to comment.