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 CSV import error for a form that defines Entities #2754

Merged
merged 1 commit into from
Jan 7, 2025
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
24 changes: 24 additions & 0 deletions onadata/apps/logger/tests/models/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Test Instance model.
"""

import os
from datetime import datetime, timedelta
from unittest.mock import Mock, patch
Expand Down Expand Up @@ -1186,3 +1187,26 @@ def test_parse_numbers(self):
instance.refresh_from_db()
self.assertEqual(instance.json["num_integer"], -1)
self.assertEqual(instance.json["num_decimal"], -1.0)

def test_xml_entity_node_missing(self):
"""Entity node missing in submission XML"""
self.project = get_user_default_project(self.user)
xform = self._publish_registration_form(self.user)
xml = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<data xmlns:jr="http://openrosa.org/javarosa" xmlns:orx='
'"http://openrosa.org/xforms" id="trees_registration" version="2022110901">'
"<formhub><uuid>d156a2dce4c34751af57f21ef5c4e6cc</uuid></formhub>"
"<location>-1.286905 36.772845 0 0</location>"
"<species>purpleheart</species>"
"<circumference>300</circumference>"
"<intake_notes />"
"<meta>"
"<instanceID>uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b</instanceID>"
"<instanceName>300cm purpleheart</instanceName>"
"</meta>"
"</data>"
)
Instance.objects.create(xml=xml, user=self.user, xform=xform)

self.assertEqual(Entity.objects.count(), 0)
4 changes: 2 additions & 2 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,11 +1261,11 @@ def create_or_update_entity_from_instance(instance: Instance) -> None:
registration_form_qs = RegistrationForm.objects.filter(
xform=instance.xform, is_active=True
)
entity_node = get_meta_from_xml(instance.xml, "entity")

if not registration_form_qs.exists():
if not registration_form_qs.exists() or not entity_node:
return

entity_node = get_meta_from_xml(instance.xml, "entity")
registration_form = registration_form_qs.first()
mutation_success_checks = ["1", "true"]
entity_uuid = entity_node.getAttribute("id")
Expand Down
Loading