Skip to content

Commit

Permalink
#554: Questionnaires can include repeat groups of parties or location…
Browse files Browse the repository at this point in the history
…s. (#738)

* Questionnaires can include repeats of parties or locations.

* refactored model_helper

* added unit tests for model_helper
  • Loading branch information
linzjax authored and ian-ross committed Oct 3, 2016
1 parent d01ee93 commit e715894
Show file tree
Hide file tree
Showing 18 changed files with 1,722 additions and 299 deletions.
10 changes: 6 additions & 4 deletions cadasta/questionnaires/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
def create_children(children, errors=[], project=None, kwargs={}):
if children:
for c in children:
if c.get('type') == 'group':
if c.get('type') == 'repeat':
create_children(c['children'], errors, project, kwargs)
elif c.get('type') == 'group':
model_name = 'QuestionGroup'

# parse attribute group
Expand All @@ -61,8 +63,9 @@ def create_children(children, errors=[], project=None, kwargs={}):
else:
model_name = 'Question'

model = apps.get_model('questionnaires', model_name)
model.objects.create_from_dict(dict=c, errors=errors, **kwargs)
if c.get('type') != 'repeat':
model = apps.get_model('questionnaires', model_name)
model.objects.create_from_dict(dict=c, errors=errors, **kwargs)


def create_options(options, question, errors=[]):
Expand Down Expand Up @@ -234,7 +237,6 @@ class QuestionManager(models.Manager):
def create_from_dict(self, errors=[], **kwargs):
dict = kwargs.pop('dict')
instance = self.model(**kwargs)

type_dict = {name: code for code, name in instance.TYPE_CHOICES}

instance.type = type_dict[dict.get('type')]
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
45 changes: 45 additions & 0 deletions cadasta/questionnaires/tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,51 @@ def test_create_children(self):
questionnaire=questionnaire,
question_group__isnull=False).count() == 1

def test_create_children_with_repeat_group(self):
questionnaire = factories.QuestionnaireFactory.create()
children = [{
'label': 'This form showcases the different question',
'name': 'intro',
'type': 'note'
}, {
'label': 'Text question type',
'name': 'text_questions',
'type': 'repeat',
'children': [
{
'hint': 'Can be short or long but '
'always one line (type = '
'text)',
'label': 'Text',
'name': 'my_string',
'type': 'text'
},
{
'hint': 'Nested group',
'label': 'Group',
'name': 'my_group',
'type': 'group',
'children': [
{
'hint': 'More text',
'label': 'Text',
'name': 'my_group_string',
'type': 'text'
},
]
}
],
}]
create_children(children, kwargs={'questionnaire': questionnaire})

assert models.QuestionGroup.objects.filter(
questionnaire=questionnaire).count() == 1
assert models.Question.objects.filter(
questionnaire=questionnaire).count() == 3
assert models.Question.objects.filter(
questionnaire=questionnaire,
question_group__isnull=False).count() == 1


class CreateOptionsTest(TestCase):

Expand Down
Loading

0 comments on commit e715894

Please sign in to comment.