diff --git a/onadata/libs/serializers/data_serializer.py b/onadata/libs/serializers/data_serializer.py index 59e247639c..af3e3d93de 100644 --- a/onadata/libs/serializers/data_serializer.py +++ b/onadata/libs/serializers/data_serializer.py @@ -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, diff --git a/onadata/libs/utils/logger_tools.py b/onadata/libs/utils/logger_tools.py index 76b10c3158..d0960558c4 100644 --- a/onadata/libs/utils/logger_tools.py +++ b/onadata/libs/utils/logger_tools.py @@ -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"<{0} id='{1}'>{2}".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 "<{0} id='{1}'>{2}".format( root, form_id, dict2xml(jsform))