Skip to content

Commit

Permalink
Retrieve root node name from XForm survey object
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Jul 30, 2020
1 parent a133c18 commit adcd1dd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,17 @@ def test_rapidpro_post_submission(self):
self.assertContains(response, 'Successful submission', status_code=201)
self.assertTrue(response.has_header('Date'))
self.assertEqual(response['Location'], 'http://testserver/submission')
# InstanceID is returned as uuid:<uuid>
# InstanceID is returned as uuid:<uuid>.
# Retrieving the uuid without the prefix in order to retrieve
# the actual instance
# the Instance object
uuid = response.data.get('instanceID').split(':')[1]
instance = Instance.objects.get(uuid=uuid)
expected_xml = (
"<?xml version='1.0' ?><data id="
f"<?xml version='1.0' ?><{self.xform.survey.name} id="
"'transportation_2011_07_25'><fruit_name>orange"
"</fruit_name></data>")
f"</fruit_name></{self.xform.survey.name}>")
self.assertEqual(instance.xml, expected_xml)
self.assertEqual(self.xform.survey.name, 'data')

def test_legacy_rapidpro_post_submission(self):
"""
Expand Down
3 changes: 2 additions & 1 deletion onadata/libs/serializers/data_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def create_submission(request, username, data_dict, xform_id):
"""
Returns validated data object instances
"""
xml_string = dict2xform(data_dict, xform_id, root='data')
xml_string = dict2xform(
data_dict, xform_id, username=username)
xml_file = BytesIO(xml_string.encode('utf-8'))

error, instance = safe_create_instance(username, xml_file, [], None,
Expand Down
37 changes: 34 additions & 3 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,41 @@ def _get_instance(xml, new_uuid, submitted_by, status, xform, checksum):
return instance


def dict2xform(jsform, form_id, root=None):
def dict2xform(jsform, form_id, root=None, username=None):
"""
Converts a dictionary containing submission data into an XML
Submission for the appropriate form.
:param jsform (dict): A python dictionary object containing the submission
data
:param form_id (str or XForm): An XForm object or a string value
representing the forms id_string
:param root (str): An optional string that should be used as the
root nodes name. Defaults to None
:param: username (str): An optional string representing a users
username. Used alongside the `form_id` to
locate the XForm object the user is
trying to submit data too. Defaults to None
:returns: Returns a string containing the Submission XML
:rtype: str
"""
if not root:
root = form_id
return u"<?xml version='1.0' ?><{0} id='{1}'>{2}</{0}>".format(
if username:
if isinstance(form_id, XForm):
root = form_id.survey.name
else:
try:
form = XForm.objects.filter(
id_string__iexact=form_id,
user__username__iexact=username,
deleted_at__isnull=True).first()
root = form.survey.name
except XForm.DoesNotExist:
root = 'data'
else:
root = 'data'

return "<?xml version='1.0' ?><{0} id='{1}'>{2}</{0}>".format(
root, form_id, dict2xml(jsform))


Expand Down

0 comments on commit adcd1dd

Please sign in to comment.