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 29, 2020
1 parent 67ee81b commit 54ac36c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
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
:optional:param: root (str): An optional string that should be used as the
root nodes name.
:optional: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.
: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 54ac36c

Please sign in to comment.