Skip to content

Commit

Permalink
Merge pull request #128 from gushil/upstream-release-fixed-preview
Browse files Browse the repository at this point in the history
Fixed form snapshot preview
  • Loading branch information
svadla-oc authored May 10, 2024
2 parents 7323e44 + df5c0ba commit 925ba00
Showing 1 changed file with 7 additions and 26 deletions.
33 changes: 7 additions & 26 deletions kpi/views/v2/asset_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,34 +185,15 @@ def preview(self, request, *args, **kwargs):
# **Not** part of the OpenRosa API
snapshot = self.get_object()
if snapshot.details.get('status') == 'success':
data = {
'server_url': reverse(viewname='assetsnapshot-detail',
preview_url = "{}/{}?form={}".format(
settings.ENKETO_URL,
settings.ENKETO_PREVIEW_ENDPOINT,
reverse(viewname='assetsnapshot-detail',
format='xml',
kwargs={'uid': snapshot.uid},
request=request
request=request,
),
'form_id': snapshot.uid
}

# Use Enketo API to create preview instead of `preview?form=`,
# which does not load any form media files.
response = requests.post(
f'{settings.ENKETO_URL}/{settings.ENKETO_PREVIEW_ENDPOINT}',
# bare tuple implies basic auth
auth=(settings.ENKETO_API_TOKEN, ''),
data=data
)
response.raise_for_status()

# Ask Celery to remove the preview from its XSLT cache after some
# reasonable delay; see
# https://github.com/enketo/enketo-express/issues/357
enketo_flush_cached_preview.apply_async(
kwargs=data, # server_url and form_id
countdown=settings.ENKETO_FLUSH_CACHED_PREVIEW_DELAY,
)

json_response = response.json()
preview_url = json_response.get('preview_url')
)

return HttpResponseRedirect(preview_url)
else:
Expand Down

0 comments on commit 925ba00

Please sign in to comment.