Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #118 from praekelt/feature/GETOOL-441-decode-unico…
Browse files Browse the repository at this point in the history
…de-checkbox-responses

Combine lists in submission data to strings so they are decoded
  • Loading branch information
KaitCrawford committed Aug 1, 2018
2 parents 3d8753b + 1caed08 commit 333edc5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions molo/surveys/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ class MoloSurveySubmission(surveys_models.AbstractFormSubmission):

def get_data(self):
form_data = super(MoloSurveySubmission, self).get_data()
for key, value in form_data.items():
# Convert lists to strings so they display properly in the view
if isinstance(value, list):
form_data[key] = u', '.join(value)
form_data.update({
'username': self.user.username if self.user else 'Anonymous',
})
Expand Down
9 changes: 9 additions & 0 deletions molo/surveys/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ def test_user(self, request, user=None):
if self.operator == self.EQUALS:
return set(python_value) == set(user_response)

if isinstance(python_value, list) \
and isinstance(user_response, six.string_types):
user_response = user_response.split(u', ')
if self.operator == self.CONTAINS:
return set(python_value).issubset(user_response)

if self.operator == self.EQUALS:
return set(python_value) == set(user_response)

if isinstance(python_value, six.string_types) \
and isinstance(user_response, six.string_types):
if self.operator == self.CONTAINS:
Expand Down
7 changes: 7 additions & 0 deletions molo/surveys/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def test_submission_class_get_data_includes_username(self):
).get_data()
self.assertIn('username', data)

def test_submission_class_get_data_converts_list_to_string(self):
data = MoloSurveyPage().get_submission_class()(
form_data='{"checkbox-question": ["option 1", "option 2"]}'
).get_data()
self.assertIn('checkbox-question', data)
self.assertEqual(data['checkbox-question'], u"option 1, option 2")


class TestSkipLogicMixin(TestCase, MoloTestCaseMixin):
def setUp(self):
Expand Down
6 changes: 3 additions & 3 deletions molo/surveys/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ def test_failing_string_rule_with_contain_operator(self):

self.assertFalse(rule.test_user(self.request))

def test_padding_checkboxes_rule_with_equal_operator(self):
def test_passing_checkboxes_rule_with_equal_operator(self):
rule = SurveySubmissionDataRule(
survey=self.survey, operator=SurveySubmissionDataRule.CONTAINS,
survey=self.survey, operator=SurveySubmissionDataRule.EQUALS,
expected_response=' choice 3 , choice 1 ',
field_name=self.checkboxes.clean_name)

self.assertTrue(rule.test_user(self.request))

def test_failing_checkboxes_rule_with_equal_operator(self):
rule = SurveySubmissionDataRule(
survey=self.survey, operator=SurveySubmissionDataRule.CONTAINS,
survey=self.survey, operator=SurveySubmissionDataRule.EQUALS,
expected_response='choice2,choice1',
field_name=self.checkboxes.clean_name)

Expand Down

0 comments on commit 333edc5

Please sign in to comment.