Skip to content

Commit

Permalink
Fix repeat groups and question order in XForms (#1014)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverroick authored and amplifi committed Dec 22, 2016
1 parent df50166 commit 1f9eef8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
7 changes: 6 additions & 1 deletion cadasta/questionnaires/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class QuestionnaireSerializer(serializers.ModelSerializer):
)
version = serializers.IntegerField(required=False, default=1)
questions = serializers.SerializerMethodField()
question_groups = QuestionGroupSerializer(many=True, read_only=True)
question_groups = serializers.SerializerMethodField()

class Meta:
model = models.Questionnaire
Expand Down Expand Up @@ -211,3 +211,8 @@ def get_questions(self, instance):
questions = instance.questions.filter(question_group__isnull=True)
serializer = QuestionSerializer(questions, many=True)
return serializer.data

def get_question_groups(self, instance):
groups = instance.question_groups.filter(question_group__isnull=True)
serializer = QuestionGroupSerializer(groups, many=True)
return serializer.data
17 changes: 17 additions & 0 deletions cadasta/questionnaires/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ def test_invalid_deserialize_json(self):

def test_serialize(self):
questionnaire = factories.QuestionnaireFactory()

factories.QuestionFactory.create(questionnaire=questionnaire,
question_group=None)
group = factories.QuestionGroupFactory.create(
questionnaire=questionnaire,
question_group=None)
factories.QuestionGroupFactory.create(questionnaire=questionnaire,
question_group=group)
factories.QuestionFactory.create(questionnaire=questionnaire,
question_group=group)

serializer = serializers.QuestionnaireSerializer(questionnaire)

assert serializer.data['id'] == questionnaire.id
Expand All @@ -123,6 +134,12 @@ def test_serialize(self):
assert serializer.data['version'] == questionnaire.version
assert 'project' not in serializer.data

assert len(serializer.data['questions']) == 1
assert len(serializer.data['question_groups']) == 1
assert len(serializer.data['question_groups'][0]['questions']) == 1
assert (
len(serializer.data['question_groups'][0]['question_groups']) == 1)

def test_huge(self):
data = {
"filename": "wa6hrqr4e4vcf49q6kxjc443",
Expand Down
8 changes: 6 additions & 2 deletions cadasta/xforms/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ def transform_questions(self, questions):
def transform_groups(self, groups):
transformed_groups = []
for g in groups:
questions = self.transform_questions(g.get('questions', []))
groups = self.transform_groups(g.get('question_groups', []))
children = sorted(questions + groups,
key=lambda x: x['index'])
group = {
'type': 'group',
'type': g.get('type', 'group'),
'name': g.get('name'),
'label': g.get('label'),
'children': self.transform_questions(g.get('questions')),
'children': children,
'index': g.get('index')
}
if group['label'] is None:
Expand Down
21 changes: 18 additions & 3 deletions cadasta/xforms/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,50 @@ def test_transform_groups(self):
groups = [{
'id': '123',
'name': 'group_1',
'label': 'Group 2',
'label': 'Group 1',
'type': 'group',
'relevant': "${party_type}='IN'",
'index': 0,
'questions': [{
'id': "bzs2984c3gxgwcjhvambdt3w",
'name': "start",
'label': None,
'type': "ST",
'index': 0
}]
}, {
'id': '456',
'name': 'group_2',
'label': None,
'type': 'repeat',
'index': 1,
'questions': [{
'id': "xp8vjr6dsk46p47u22fft7bg",
'name': "tenure_type",
'label': "What is the social tenure type?",
'type': "TX",
'index': 0
}],
'question_groups': [{
'id': "xp8vjr6dsk46p47u22fft7aa",
'name': 'group_3',
'label': 'Group 3',
'type': 'group',
'index': 1
}]
}]
renderer = XFormRenderer()
transformed = renderer.transform_groups(groups)
assert len(transformed) == 2
for g in transformed:
assert g['type'] == 'group'
assert len(g['children']) == 1
if g['name'] == 'group_1':
assert g['bind']['relevant'] == "${party_type}='IN'"
assert g['type'] == 'group'
assert len(g['children']) == 1
if g['name'] == 'group_2':
assert 'label' not in g
assert g['type'] == 'repeat'
assert len(g['children']) == 2

def test_transform_to_xform_json(self):
data = {
Expand Down

0 comments on commit 1f9eef8

Please sign in to comment.