Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed form snapshot preview #128

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading