Skip to content

Commit

Permalink
refactor to publish forms in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Apr 11, 2024
1 parent 3fbb3cb commit 9dce20f
Show file tree
Hide file tree
Showing 23 changed files with 467 additions and 660 deletions.
81 changes: 80 additions & 1 deletion onadata/apps/api/tests/viewsets/test_abstract_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
from onadata.apps.api.viewsets.project_viewset import ProjectViewSet
from onadata.apps.api.viewsets.team_viewset import TeamViewSet
from onadata.apps.api.viewsets.widget_viewset import WidgetViewSet
from onadata.apps.logger.models import Attachment, Instance, Project, XForm
from onadata.apps.logger.models import (
Attachment,
Instance,
Project,
XForm,
XFormVersion,
)
from onadata.apps.logger.models.data_view import DataView
from onadata.apps.logger.models.widget import Widget
from onadata.apps.logger.views import submission
Expand Down Expand Up @@ -699,3 +705,76 @@ def _publish_markdown(self, md, user, project=None, **kwargs):
xform.save()

return xform

def _publish_registration_form(self):
md = """
| survey |
| | type | name | label | save_to |
| | geopoint | location | Tree location | geometry |
| | select_one species | species | Tree species | species |
| | integer | circumference | Tree circumference in cm | circumference_cm |
| | text | intake_notes | Intake notes | |
| choices | | | | |
| | list_name | name | label | |
| | species | wallaba | Wallaba | |
| | species | mora | Mora | |
| | species | purpleheart | Purpleheart | |
| | species | greenheart | Greenheart | |
| settings | | | | |
| | form_title | form_id | version | instance_name |
| | Trees registration | trees_registration | 2022110901 | concat(${circumference}, "cm ", ${species})|
| entities | | | | |
| | list_name | label | | |
| | trees | concat(${circumference}, "cm ", ${species})| | |"""

if not hasattr(self, "project"):
self._project_create()
elif self.project.created_by != self.user:
self._project_create()

data_dict = self._publish_markdown(
md,
self.user,
self.project,
id_string="trees_registration",
title="Trees registration",
)
latest_form = XForm.objects.all().order_by("-pk").first()
XFormVersion.objects.create(
xform=latest_form,
version="2022110901",
xml=data_dict.xml,
json=json.dumps(data_dict.json),
)
return latest_form

def _publish_follow_up_form(self):
md = """
| survey |
| | type | name | label | required |
| | select_one_from_file trees.csv | tree | Select the tree you are visiting | yes |
| settings| | | | |
| | form_title | form_id | version | |
| | Trees follow-up | trees_follow_up | 2022111801 | |
"""

if not hasattr(self, "project"):
self._project_create()
elif self.project.created_by != self.user:
self._project_create()

data_dict = self._publish_markdown(
md,
self.user,
self.project,
id_string="trees_follow_up",
title="Trees follow-up",
)
latest_form = XForm.objects.all().order_by("-pk").first()
XFormVersion.objects.create(
xform=latest_form,
version="2022111801",
xml=data_dict.xml,
json=json.dumps(data_dict.json),
)
return latest_form
66 changes: 6 additions & 60 deletions onadata/apps/api/tests/viewsets/test_entity_list_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,9 @@ def setUp(self):
def test_get_all(self):
"""GET all EntityLists works"""
# Publish registration form and create "trees" EntityList dataset
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_registration.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self._publish_registration_form()
# Publish follow up form for "trees" dataset
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_follow_up.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self._publish_follow_up_form()
# Make submission on tree_registration form
submission_path = os.path.join(
settings.PROJECT_ROOT,
Expand Down Expand Up @@ -180,27 +162,9 @@ def setUp(self):

self.view = EntityListViewSet.as_view({"get": "retrieve"})
# Publish registration form and create "trees" EntityList dataset
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_registration.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self._publish_registration_form()
# Publish follow up form for "trees" dataset
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_follow_up.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self._publish_follow_up_form()
self.entity_list = EntityList.objects.first()
# Make submission on tree_registration form
submission_path = os.path.join(
Expand Down Expand Up @@ -304,27 +268,9 @@ def setUp(self):

self.view = EntityListViewSet.as_view({"get": "entities"})
# Publish registration form and create "trees" EntityList dataset
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_registration.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self._publish_registration_form()
# Publish follow up form for "trees" dataset
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_follow_up.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self._publish_follow_up_form()
# Make submissions which will then create Entities
paths = [
os.path.join(
Expand Down
11 changes: 1 addition & 10 deletions onadata/apps/api/tests/viewsets/test_xform_list_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,16 +968,7 @@ def test_retrieve_xform_media_linked_xform(self):
def test_retrieve_xform_media_entity_list_dataset(self):
"""EntityList dataset is returned"""
# Publish registration form and create "trees" Entitylist dataset
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_registration.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self._publish_registration_form()
# Make submission to trees_registration form
submission_path = os.path.join(
self.main_directory,
Expand Down
22 changes: 2 additions & 20 deletions onadata/apps/api/tests/viewsets/test_xform_submission_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,16 +1323,7 @@ def test_edit_submission_sent_to_rapidpro(self, mock_send):

def test_create_entity(self):
"""An Entity is created for if the form is a RegistrationForm"""
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_registration.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self.xform = self._publish_registration_form()
submission_path = os.path.join(
self.main_directory,
"fixtures",
Expand Down Expand Up @@ -1373,16 +1364,7 @@ def test_create_entity(self):

def test_registration_form_inactive(self):
"""When the RegistrationForm is inactive, Entity should not be created"""
xlsform_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"entities",
"trees_registration.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xlsform_path)
self.xform = self._publish_registration_form()
registration_form = self.xform.registration_forms.first()
# deactivate registration form
registration_form.is_active = False
Expand Down
Loading

0 comments on commit 9dce20f

Please sign in to comment.