Skip to content

Commit

Permalink
Retain fields query parameter when redirecting from find_view in API (#…
Browse files Browse the repository at this point in the history
…1683)

Co-authored-by: James Biggs <[email protected]>
  • Loading branch information
ahosgood and jamesbiggs authored Jul 23, 2024
1 parent 53f22d5 commit 7d556d6
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion etna/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.http import Http404
from django.shortcuts import redirect
from django.utils.crypto import constant_time_compare

from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.api.v2.utils import BadRequestError
from wagtail.api.v2.utils import BadRequestError, get_object_detail_url
from wagtail.api.v2.views import PagesAPIViewSet
from wagtail.contrib.redirects.models import Redirect
from wagtail.images.api.v2.views import ImagesAPIViewSet
Expand Down Expand Up @@ -77,6 +78,32 @@ def detail_view(self, request, pk):
}
return Response(data, status=status.HTTP_403_FORBIDDEN)

def find_view(self, request):
queryset = self.get_queryset()

try:
obj = self.find_object(queryset, request)
if obj is None:
raise self.model.DoesNotExist

except self.model.DoesNotExist:
raise Http404("not found")

url = get_object_detail_url(
self.request.wagtailapi_router, request, self.model, obj.pk
)
if url is None:
raise Exception(
"Cannot generate URL to detail view. Is '{}' installed in the API router?".format(
self.__class__.__name__
)
)

if "fields" in request.GET:
url = url + "fields=" + request.GET["fields"]

return redirect(url)

def get_base_queryset(self):
"""
Copy of https://github.com/wagtail/wagtail/blob/f5552c40442b0ed6a0316ee899c7f28a0b1ed4e5/wagtail/api/v2/views.py#L491
Expand Down

0 comments on commit 7d556d6

Please sign in to comment.