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

Backend Changes for Customizable Confirmation Screen #5112

Open
wants to merge 1 commit into
base: beta-refactored
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,60 @@ def test_submission_blocking_flag(self):
)
response = self.view(request, username=username)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_submission_customizable_confirmation_message(self):
s = 'transport_with_custom_attribute'
media_file = '1335783522563.jpg'
xml_files = [
'transport_with_custom_attribute_01',
'transport_with_custom_attribute_02'
]

path = os.path.join(
self.main_directory,
'fixtures',
'transportation',
'instances',
s,
media_file,
)
with open(path, 'rb') as f:
f = InMemoryUploadedFile(
f,
'media_file',
media_file,
'image/jpg',
os.path.getsize(path),
None,
)
for xml_file in xml_files:
submission_path = os.path.join(
self.main_directory,
'fixtures',
'transportation',
'instances',
s,
xml_file + '.xml',
)
with open(submission_path) as sf:
data = {'xml_submission_file': sf, 'media_file': f}
request = self.factory.post('/submission', data)
response = self.view(request)
self.assertEqual(response.status_code, 401)

# rewind the file and redo the request since they were
# consumed
sf.seek(0)
f.seek(0)
request = self.factory.post('/submission', data)
auth = DigestAuth('bob', 'bobbob')
request.META.update(auth(request.META, response))
response = self.view(request, username=self.user.username)
if xml_file == 'transport_with_custom_attribute_01':
self.assertContains(
response, 'Custom submit message', status_code=201
)
else:
self.assertContains(
response, 'Successful submission.', status_code=201
)
37 changes: 37 additions & 0 deletions kobo/apps/openrosa/apps/api/viewsets/xform_submission_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# coding: utf-8
import re
import io
import logging

from django.shortcuts import get_object_or_404
from django.utils.translation import gettext as t
from lxml import etree
from lxml.etree import XPathEvalError
from rest_framework import permissions
from rest_framework import status
from rest_framework import mixins
Expand Down Expand Up @@ -63,6 +66,38 @@ def create_instance_from_json(username, request):
return safe_create_instance(username, xml_file, [], None, request=request)


def extract_confirmation_message(xml_string):
"""
Extracts the confirmation message from the XML string based on the
kobo:submitResponseMessage attribute.
"""
if isinstance(xml_string, str):
xml_string = xml_string.encode('utf-8')
parser = etree.XMLParser(recover=True)
root = etree.fromstring(xml_string, parser=parser)
namespaces = {'kobo': 'http://kobotoolbox.org/xforms'}

# Extract the kobo:submitResponseMessage attribute from the root element
confirmation_message_xpath = root.get(
'{http://kobotoolbox.org/xforms}submitResponseMessage'
)

if confirmation_message_xpath:
try:
# Evaluate the XPath expression to find the message
confirmation_message_element = root.xpath(
confirmation_message_xpath.strip(), namespaces=namespaces
)
return confirmation_message_element[0].text
except Exception as e:
logging.error(
'Failed to extract confirmation message: ' + str(e),
exc_info=True
)
return None
return None


class XFormSubmissionApi(
OpenRosaHeadersMixin, mixins.CreateModelMixin, OpenRosaGenericViewSet
):
Expand Down Expand Up @@ -187,6 +222,8 @@ def create(self, request, *args, **kwargs):
return self.error_response(error, is_json_request, request)

context = self.get_serializer_context()
if instance.xml and (confirmation_message := extract_confirmation_message(instance.xml)):
context['confirmation_message'] = confirmation_message
serializer = SubmissionSerializer(instance, context=context)

return Response(serializer.data,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' ?><transportation id="transportation_2011_07_25" xmlns:kobo="http://kobotoolbox.org/xforms" kobo:submitResponseMessage="/transportation/submitMessage"><transport><loop_over_transport_types_frequency><ambulance /><bicycle /><boat_canoe /><bus /><donkey_mule_cart /><keke_pepe /><lorry /><motorbike /><taxi></taxi><other></other></loop_over_transport_types_frequency></transport><submitMessage>Custom submit message</submitMessage><meta><instanceID>uuid:7g0a1508-c3b7-4c99-be00-9b237c26bcfb</instanceID></meta></transportation>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' ?><transportation id="transportation_2011_07_25" xmlns:kobo="http://kobotoolbox.org/xforms" kobo:submitResponseMessage="/transportation/submitMessage"><transport><loop_over_transport_types_frequency><ambulance /><bicycle /><boat_canoe /><bus /><donkey_mule_cart /><keke_pepe /><lorry /><motorbike /><taxi></taxi><other></other></loop_over_transport_types_frequency></transport><meta><instanceID>uuid:4f7a1508-c3b7-5f33-be00-9b237c26bcdr</instanceID></meta></transportation>
3 changes: 2 additions & 1 deletion kobo/apps/openrosa/libs/serializers/data_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ def to_representation(self, obj):
if not hasattr(obj, 'xform'):
return super().to_representation(obj)

message = self.context.get('confirmation_message', t("Successful submission."))
return {
'message': t("Successful submission."),
'message': message,
'formid': obj.xform.id_string,
'encrypted': obj.xform.encrypted,
'instanceID': 'uuid:%s' % obj.uuid,
Expand Down