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

[fix] Lookup only models where the parent is h:head #2733

Merged
merged 3 commits into from
Nov 15, 2024
Merged
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
7 changes: 6 additions & 1 deletion onadata/apps/logger/models/xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
The XForm model
"""

# pylint: disable=too-many-lines
import hashlib
import json
Expand Down Expand Up @@ -243,7 +244,11 @@ def set_uuid_in_xml(self, file_name=None):
file_name, _file_ext = os.path.splitext(file_name)

doc = clean_and_parse_xml(self.xml)
model_nodes = doc.getElementsByTagName("model")
model_nodes = [
model_node
for model_node in doc.getElementsByTagName("model")
if model_node.parentNode.nodeName == "h:head"
]
if len(model_nodes) != 1:
raise Exception("xml contains multiple model nodes")

Expand Down
24 changes: 24 additions & 0 deletions onadata/apps/logger/tests/models/test_xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
test_xform module
"""

import os

from builtins import str as text
Expand Down Expand Up @@ -227,3 +228,26 @@ def test_id_string_length(self):

with self.assertRaises(XLSFormError):
xform.save()

def test_multiple_model_nodes(self):
"""
Test XForm.set_uuid_in_xml() function is able to handle
a form that has field named model which may match the XForm's
top level node also named model.
"""
md = """
| survey |
| | type | name | label |
| | select one fruits | fruit | Fruit |
| | text | model | Model |
| choices |
| | list name | name | label |
| | fruits | orange | Orange |
| | fruits | mango | Mango |
"""
dd = self._publish_markdown(md, self.user, id_string="a")
self.assertNotIn(
"<formhub>\n <uuid/>\n </formhub>\n", dd.xml
)
dd.set_uuid_in_xml()
self.assertIn("<formhub>\n <uuid/>\n </formhub>\n", dd.xml)
6 changes: 5 additions & 1 deletion onadata/apps/viewer/models/data_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ def save(self, *args, **kwargs):
super().save(*args, **kwargs)

def file_name(self):
return os.path.split(self.xls.name)[-1]
return (
os.path.split(self.xls.name)[-1]
if self.xls.name is not None
else self.id_string + ".xml"
)


# pylint: disable=unused-argument
Expand Down
Loading