Skip to content

Commit

Permalink
Merge pull request #82 from 6aika/jurisdiction-filter
Browse files Browse the repository at this point in the history
Add jurisdiction_id filtering to service list
  • Loading branch information
akx authored May 17, 2017
2 parents f934a99 + 83d7f82 commit fbe3ba3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions issues/api/views/services.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
from __future__ import absolute_import

from django.utils import translation
from rest_framework.filters import BaseFilterBackend
from rest_framework.generics import ListAPIView

from issues.api.serializers import ServiceSerializer
from issues.models import Service


class ServiceFilter(BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
jurisdiction_id = request.query_params.get('jurisdiction_id')
if jurisdiction_id:
queryset = queryset.filter(jurisdictions__identifier=jurisdiction_id)
return queryset


class ServiceList(ListAPIView):
item_tag_name = 'service'
root_tag_name = 'services'
serializer_class = ServiceSerializer
queryset = Service.objects.all()
filter_backends = (
ServiceFilter,
)

def dispatch(self, request, *args, **kwargs):
locale = (request.GET.get("locale") or translation.get_language())
Expand Down
23 changes: 23 additions & 0 deletions issues/tests/test_service_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from django.core.urlresolvers import reverse_lazy
from django.utils.crypto import get_random_string

from issues.models import Jurisdiction
from issues.models.services import Service
from issues.tests.utils import get_data_from_response

Expand All @@ -23,3 +26,23 @@ def test_service_list_i18n(mf_api_client, random_service):
)
assert len(data) == 1
assert data[0]['service_name'].endswith(lang) # As we set above


@pytest.mark.django_db
def test_service_list_jurisdiction_filter(mf_api_client):
tku = Jurisdiction.objects.create(identifier='fi.turku')
hel = Jurisdiction.objects.create(identifier='fi.hel')
tku_service = Service.objects.create(service_code=get_random_string(), service_name='Aurajokipalvelu')
hel_service = Service.objects.create(service_code=get_random_string(), service_name='Suomenlinnapalvelu')
tku_service.jurisdictions.add(tku)
hel_service.jurisdictions.add(hel)
for query, expected in (
({}, [tku_service, hel_service]),
({'jurisdiction_id': tku.identifier}, [tku_service]),
({'jurisdiction_id': hel.identifier}, [hel_service]),
):
data = get_data_from_response(
mf_api_client.get(SERVICE_LIST_ENDPOINT, query)
)
assert len(data) == len(expected)
assert set(s['service_name'] for s in data) == set(s.service_name for s in expected)

0 comments on commit fbe3ba3

Please sign in to comment.