Skip to content

Commit

Permalink
Add support for extracting the confirmation message from submission X…
Browse files Browse the repository at this point in the history
…ML based on a custom attribute
  • Loading branch information
rajpatel24 committed Sep 17, 2024
1 parent 0d7131f commit d8f8458
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
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, 'Successful submission.', status_code=201
)
else:
self.assertContains(
response, 'Successful submission.', status_code=201
)
32 changes: 32 additions & 0 deletions kobo/apps/openrosa/apps/api/viewsets/xform_submission_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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 +65,34 @@ 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 \
if confirmation_message_element[0].text else None
except XPathEvalError:
return None


class XFormSubmissionApi(
OpenRosaHeadersMixin, mixins.CreateModelMixin, OpenRosaGenericViewSet
):
Expand Down Expand Up @@ -187,6 +217,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

0 comments on commit d8f8458

Please sign in to comment.