Skip to content

Commit

Permalink
Add parking status filter to internal API
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas777 committed Dec 22, 2016
1 parent a78bf90 commit 67f1e07
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
21 changes: 21 additions & 0 deletions parkings/api/internal/parking.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import django_filters
from django.utils import timezone
from rest_framework import permissions, serializers, viewsets

from parkings.models import Parking
Expand All @@ -11,7 +13,26 @@ class Meta:
fields = '__all__'


class InternalAPIParkingFilter(django_filters.rest_framework.FilterSet):
status = django_filters.CharFilter(method='filter_status')

class Meta:
model = Parking
fields = ('status',)

def filter_status(self, queryset, name, value):
now = timezone.now()

if value == Parking.VALID:
return queryset.filter(time_start__lte=now, time_end__gte=now)
elif value == Parking.NOT_VALID:
return queryset.exclude(time_start__lte=now, time_end__gte=now)

return queryset


class InternalAPIParkingViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Parking.objects.all()
serializer_class = InternalAPIParkingSerializer
permission_classes = (permissions.IsAdminUser,)
filter_class = InternalAPIParkingFilter
10 changes: 9 additions & 1 deletion parkings/tests/api/internal/test_parking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from parkings.models import Parking

from ..utils import ALL_METHODS, check_list_endpoint_base_fields, check_method_status_codes, get
from ..utils import ALL_METHODS, check_list_endpoint_base_fields, check_method_status_codes, get, get_ids_from_results

list_url = reverse('internal:v1:parking-list')

Expand Down Expand Up @@ -74,3 +74,11 @@ def test_is_valid_field(staff_api_client, past_parking, current_parking, future_

parking_data = get(staff_api_client, get_detail_url(future_parking))
assert parking_data['status'] == Parking.NOT_VALID


def test_is_valid_filter(staff_api_client, past_parking, current_parking, future_parking):
results = get(staff_api_client, list_url + '?status=valid')['results']
assert get_ids_from_results(results) == {current_parking.id}

results = get(staff_api_client, list_url + '?status=not_valid')['results']
assert get_ids_from_results(results) == {past_parking.id, future_parking.id}
6 changes: 6 additions & 0 deletions parkings/tests/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import uuid

from rest_framework.authtoken.models import Token

Expand Down Expand Up @@ -70,3 +71,8 @@ def check_required_fields(api_client, url, expected_required_fields, detail_endp
required_fields.add(field)

assert required_fields == expected_required_fields


def get_ids_from_results(results, as_set=True):
id_list = [uuid.UUID(result['id']) for result in results]
return set(id_list) if as_set else id_list
1 change: 1 addition & 0 deletions parkkihubi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
'ALLOWED_VERSIONS': ('v1',),
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticatedOrReadOnly',),
'DEFAULT_AUTHENTICATION_CLASSES': ('parkings.authentication.ApiKeyAuthentication',),
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'PAGE_SIZE': 20,
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
}
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ psycopg2
# Django REST Framework
djangorestframework
djangorestframework-gis
django-filter

# Misc
pytz
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
contextlib2==0.5.3 # via raven
django-environ==0.4.1
django-filter==1.0.1
django==1.10.3
djangorestframework-gis==0.11
djangorestframework==3.5.3
Expand Down

0 comments on commit 67f1e07

Please sign in to comment.