Skip to content

Commit

Permalink
Merge pull request #927 from uktrade/bug/tss-2204/barrier-downloads-f…
Browse files Browse the repository at this point in the history
…ilterset-params

tss-2204: Barrier downloads filter params in body
  • Loading branch information
abarolo authored Oct 30, 2024
2 parents 94c77f5 + d65b48a commit 78f498f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 15 additions & 1 deletion api/barrier_downloads/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
from api.barriers.models import Barrier, BarrierFilterSet


class BarrierDownloadFilterBackend(DjangoFilterBackend):
"""
We want to override DjangoFilterBackend to read the filters
from request.data instead of request.query_params
"""

def get_filterset_kwargs(self, request, queryset, view):
return {
"data": request.data,
"queryset": queryset,
"request": request,
}


class BarrierDownloadsView(generics.ListCreateAPIView):
queryset = (
Barrier.barriers.annotate(
Expand All @@ -41,7 +55,7 @@ class BarrierDownloadsView(generics.ListCreateAPIView):
)
serializer_class = BarrierDownloadSerializer
filterset_class = BarrierFilterSet
filter_backends = (DjangoFilterBackend,)
filter_backends = (BarrierDownloadFilterBackend,)
pagination_class = PageNumberPagination

def post(self, request, *args, **kwargs):
Expand Down
10 changes: 7 additions & 3 deletions tests/barrier_downloads/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ def test_barrier_download_post_endpoint_no_results(self):

def test_barrier_download_post_endpoint_no_results_filter(self):
barrier = BarrierFactory()
url = f'{reverse("barrier-downloads")}?search_term_text=wrong-{barrier.title}'
url = f'{reverse("barrier-downloads")}'

response = self.api_client.post(url)
response = self.api_client.post(
url,
data={"search_term_text": f"wrong-{barrier.title}"},
format="json",
)

assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.content == b'{"error": "No barriers matching filterset"}'
Expand Down Expand Up @@ -60,7 +64,7 @@ def test_barrier_download_post_endpoint_success(
@patch("api.barrier_downloads.tasks.generate_barrier_download_file")
def test_barrier_download_post_endpoint_success_with_filter(self, mock_generate):
barrier = BarrierFactory()
url = f'{reverse("barrier-downloads")}?text={barrier.title}'
url = f'{reverse("barrier-downloads")}'

response = self.api_client.post(
url, data={"filters": {"text": barrier.title}}, format="json"
Expand Down

0 comments on commit 78f498f

Please sign in to comment.