Skip to content

Commit

Permalink
Support datetime lookups on the timestamp filter
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Dec 17, 2020
1 parent 63d725b commit e4ff070
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
35 changes: 35 additions & 0 deletions onadata/apps/messaging/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,49 @@
Messaging viewset filters module.
"""
from __future__ import unicode_literals
from actstream.models import Action

from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from rest_framework import exceptions, filters
from django_filters import rest_framework as rest_filters

from onadata.apps.messaging.utils import TargetDoesNotExist, get_target


DATETIME_LOOKUPS = [
"exact",
"gt",
"lt",
"gte",
"lte",
"year",
"year__gt",
"year__lt",
"year__gte",
"year__lte",
"month",
"month__gt",
"month__lt",
"month__gte",
"month__lte",
"day",
"day__gt",
"day__lt",
"day__gte",
"day__lte",
]


class ActionFilterSet(rest_filters.FilterSet):
class Meta:
model = Action
fields = {
"verb": ["exact"],
"timestamp": DATETIME_LOOKUPS
}


class TargetTypeFilterBackend(filters.BaseFilterBackend):
"""
A filter backend that filters by target type.
Expand Down
19 changes: 19 additions & 0 deletions onadata/apps/messaging/tests/test_messaging_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ def test_messaging_timestamp_filter(self):
self.assertEqual(
response.data[0].get('id'), message_one['id'])

# Test able to filter using gt & gte lookups
request = self.factory.get(
f'/messaging?timestamp__gt={message_one_timestamp}&'
f'target_type=user&target_id={target_id}')
Expand All @@ -283,6 +284,15 @@ def test_messaging_timestamp_filter(self):
self.assertEqual(
response.data[0].get('id'), message_two['id'])

request = self.factory.get(
f'/messaging?timestamp__gte={message_one_timestamp}&'
f'target_type=user&target_id={target_id}')
force_authenticate(request, user=user)
response = view(request=request)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 2)

# Test able to filter using lt & lte lookups
message_two_timestamp = message_two['timestamp']
request = self.factory.get(
f'/messaging?timestamp__lt={message_two_timestamp}&'
Expand All @@ -293,3 +303,12 @@ def test_messaging_timestamp_filter(self):
self.assertEqual(len(response.data), 1)
self.assertEqual(
response.data[0].get('id'), message_one['id'])

message_two_timestamp = message_two['timestamp']
request = self.factory.get(
f'/messaging?timestamp__lte={message_two_timestamp}&'
f'target_type=user&target_id={target_id}')
force_authenticate(request, user=user)
response = view(request=request)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 2)
8 changes: 4 additions & 4 deletions onadata/apps/messaging/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from rest_framework.permissions import IsAuthenticated

from onadata.apps.messaging.constants import MESSAGE_VERBS
from onadata.apps.messaging.filters import (TargetIDFilterBackend,
TargetTypeFilterBackend,
UserFilterBackend)
from onadata.apps.messaging.filters import (
ActionFilterSet, TargetIDFilterBackend, TargetTypeFilterBackend,
UserFilterBackend)
from onadata.apps.messaging.permissions import TargetObjectPermissions
from onadata.apps.messaging.serializers import MessageSerializer

Expand All @@ -30,4 +30,4 @@ class MessagingViewSet(mixins.CreateModelMixin, mixins.ListModelMixin,
permission_classes = [IsAuthenticated, TargetObjectPermissions]
filter_backends = (TargetTypeFilterBackend, TargetIDFilterBackend,
UserFilterBackend, DjangoFilterBackend)
filter_fields = ['verb', 'timestamp']
filterset_class = ActionFilterSet

0 comments on commit e4ff070

Please sign in to comment.