Skip to content

Commit

Permalink
extract redundant redirection logic into handle_enketo_redirect function
Browse files Browse the repository at this point in the history
  • Loading branch information
RuthShryock committed Jul 30, 2024
1 parent 907a37b commit 5fa367a
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions kpi/views/v2/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,16 @@ def destroy(self, request, pk, *args, **kwargs):

return Response(**json_response)

def handle_enketo_redirect(self, request, enketo_response, *args, **kwargs):
if request.path.strip('/').split('/')[-2] == 'redirect':
try:
enketo_url = enketo_response.data['url']
except KeyError:
pass
else:
return HttpResponseRedirect(enketo_url)
return enketo_response

@action(
detail=True,
methods=['GET'],
Expand All @@ -454,15 +464,7 @@ def enketo_edit(self, request, pk, *args, **kwargs):
EnketoSessionAuthentication.prepare_response_with_csrf_cookie(
request, enketo_response
)
if request.path.strip('/').split('/')[-2] == 'redirect':
try:
enketo_url = enketo_response.data['url']
except KeyError:
# Fall through to returning verbatim Enketo response
pass
else:
return HttpResponseRedirect(enketo_url)
return enketo_response
return self.handle_enketo_redirect(request, enketo_response, *args, **kwargs)

@action(
detail=True,
Expand All @@ -474,15 +476,7 @@ def enketo_edit(self, request, pk, *args, **kwargs):
def enketo_view(self, request, pk, *args, **kwargs):
submission_id = positive_int(pk)
enketo_response = self._get_enketo_link(request, submission_id, 'view')
if request.path.strip('/').split('/')[-2] == 'redirect':
try:
enketo_url = enketo_response.data['url']
except KeyError:
# Fall through to returning verbatim Enketo response
pass
else:
return HttpResponseRedirect(enketo_url)
return enketo_response
return self.handle_enketo_redirect(request, enketo_response, *args, **kwargs)

def get_queryset(self):
# This method is needed when pagination is activated and renderer is
Expand Down

0 comments on commit 5fa367a

Please sign in to comment.