diff --git a/molo/surveys/models.py b/molo/surveys/models.py index 6d594e3..32f443f 100644 --- a/molo/surveys/models.py +++ b/molo/surveys/models.py @@ -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', }) diff --git a/molo/surveys/rules.py b/molo/surveys/rules.py index 0ca1b97..a2f3fad 100644 --- a/molo/surveys/rules.py +++ b/molo/surveys/rules.py @@ -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: diff --git a/molo/surveys/tests/test_models.py b/molo/surveys/tests/test_models.py index 060127d..205c6c6 100644 --- a/molo/surveys/tests/test_models.py +++ b/molo/surveys/tests/test_models.py @@ -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): diff --git a/molo/surveys/tests/test_rules.py b/molo/surveys/tests/test_rules.py index 18aab0c..8f92263 100644 --- a/molo/surveys/tests/test_rules.py +++ b/molo/surveys/tests/test_rules.py @@ -129,9 +129,9 @@ 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) @@ -139,7 +139,7 @@ def test_padding_checkboxes_rule_with_equal_operator(self): 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)