From edf4e2ccdfcde6c5ea771de85cd227b237a97203 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 5 Feb 2020 23:06:37 +0100 Subject: [PATCH] Permit to export survey with None value to CSV Closes #70, this would be prevented by resolving #73 and #41 but we probably need to be able to handle None value anyway. Thanks to @ebdavison for its test data. --- survey/exporter/csv/survey2csv.py | 8 ++++- survey/locale/de/LC_MESSAGES/django.po | 4 +-- survey/locale/es/LC_MESSAGES/django.po | 4 +-- survey/locale/fr/LC_MESSAGES/django.po | 4 +-- survey/locale/ja/LC_MESSAGES/django.po | 4 +-- survey/locale/ru/LC_MESSAGES/django.po | 4 +-- survey/locale/zh/LC_MESSAGES/django.po | 4 +-- survey/models/answer.py | 2 ++ survey/tests/exporter/csv/test_issue70.py | 38 +++++++++++++++++++++++ 9 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 survey/tests/exporter/csv/test_issue70.py diff --git a/survey/exporter/csv/survey2csv.py b/survey/exporter/csv/survey2csv.py index 85a5b9ac..5985e719 100755 --- a/survey/exporter/csv/survey2csv.py +++ b/survey/exporter/csv/survey2csv.py @@ -2,6 +2,8 @@ import logging +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured from django.utils.translation import gettext_lazy as _ from survey.exporter.survey2x import Survey2X @@ -36,7 +38,11 @@ def get_user_line(question_order, response): answers = answer.values cell = "" for i, ans in enumerate(answers): - if i < len(answers) - 1: + if ans is None: + if settings.USER_DID_NOT_ANSWER is None: + raise ImproperlyConfigured("USER_DID_NOT_ANSWER need to be set in your settings file.") + cell += settings.USER_DID_NOT_ANSWER + elif i < len(answers) - 1: # Separate by a pipe if its not the last cell += ans + "|" else: diff --git a/survey/locale/de/LC_MESSAGES/django.po b/survey/locale/de/LC_MESSAGES/django.po index a8d1987a..ffaf28e4 100644 --- a/survey/locale/de/LC_MESSAGES/django.po +++ b/survey/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-01-26 18:37+0000\n" +"POT-Creation-Date: 2020-02-05 22:05+0000\n" "PO-Revision-Date: 2020-01-24 17:05+0000\n" "Last-Translator: b' <>'\n" "Language-Team: LANGUAGE \n" @@ -38,7 +38,7 @@ msgstr[1] "%(count)d Umfragen erfolgreich als veröffentlicht markiert." msgid "Mark selected surveys as published" msgstr "Markiere ausgewählte Umfragen als veröffentlicht" -#: survey/exporter/csv/survey2csv.py:33 +#: survey/exporter/csv/survey2csv.py:35 msgid "Anonymous" msgstr "Anonym" diff --git a/survey/locale/es/LC_MESSAGES/django.po b/survey/locale/es/LC_MESSAGES/django.po index 6074249a..a0395e83 100644 --- a/survey/locale/es/LC_MESSAGES/django.po +++ b/survey/locale/es/LC_MESSAGES/django.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-01-26 18:37+0000\n" +"POT-Creation-Date: 2020-02-05 22:05+0000\n" "PO-Revision-Date: 2018-10-06 20:42+0000\n" "Last-Translator: Javier Ordóñez <>\n" "Language-Team: \n" @@ -37,7 +37,7 @@ msgstr[1] "encuestas %(count)d se marcaron exitosamente como publicadas." msgid "Mark selected surveys as published" msgstr "Marca las encuestas seleccionadas como publicadas" -#: survey/exporter/csv/survey2csv.py:33 +#: survey/exporter/csv/survey2csv.py:35 msgid "Anonymous" msgstr "Anónimo" diff --git a/survey/locale/fr/LC_MESSAGES/django.po b/survey/locale/fr/LC_MESSAGES/django.po index 17f98bc4..9e2fed28 100644 --- a/survey/locale/fr/LC_MESSAGES/django.po +++ b/survey/locale/fr/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-01-26 18:37+0000\n" +"POT-Creation-Date: 2020-02-05 22:05+0000\n" "PO-Revision-Date: 2018-04-01 11:49+0000\n" "Last-Translator: b' <>'\n" "Language-Team: LANGUAGE \n" @@ -37,7 +37,7 @@ msgstr[1] "%(count)d questionnaires ont été publiés avec succès." msgid "Mark selected surveys as published" msgstr "Marquer le sondage comme publié" -#: survey/exporter/csv/survey2csv.py:33 +#: survey/exporter/csv/survey2csv.py:35 msgid "Anonymous" msgstr "Anonyme" diff --git a/survey/locale/ja/LC_MESSAGES/django.po b/survey/locale/ja/LC_MESSAGES/django.po index c55dff36..e626a2a9 100644 --- a/survey/locale/ja/LC_MESSAGES/django.po +++ b/survey/locale/ja/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-01-26 18:37+0000\n" +"POT-Creation-Date: 2020-02-05 22:05+0000\n" "PO-Revision-Date: 2018-04-09 00:52+0000\n" "Last-Translator: b' '\n" "Language-Team: LANGUAGE \n" @@ -37,7 +37,7 @@ msgstr[1] "%(count)d つの調査が「公開」に設定されました" msgid "Mark selected surveys as published" msgstr "選択した調査を公開する" -#: survey/exporter/csv/survey2csv.py:33 +#: survey/exporter/csv/survey2csv.py:35 msgid "Anonymous" msgstr "匿名" diff --git a/survey/locale/ru/LC_MESSAGES/django.po b/survey/locale/ru/LC_MESSAGES/django.po index f03963e1..aa4a2379 100644 --- a/survey/locale/ru/LC_MESSAGES/django.po +++ b/survey/locale/ru/LC_MESSAGES/django.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-01-26 18:37+0000\n" +"POT-Creation-Date: 2020-02-05 22:05+0000\n" "PO-Revision-Date: 2019-03-10 22:53+0230\n" "Last-Translator: b' '\n" "Language-Team: \n" @@ -37,7 +37,7 @@ msgstr[1] "опросы %(count)d помечены как опубликован msgid "Mark selected surveys as published" msgstr "Помечено как опублиовано" -#: survey/exporter/csv/survey2csv.py:33 +#: survey/exporter/csv/survey2csv.py:35 msgid "Anonymous" msgstr "Анонимно" diff --git a/survey/locale/zh/LC_MESSAGES/django.po b/survey/locale/zh/LC_MESSAGES/django.po index 8ca31785..8a818ad7 100644 --- a/survey/locale/zh/LC_MESSAGES/django.po +++ b/survey/locale/zh/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-01-26 18:37+0000\n" +"POT-Creation-Date: 2020-02-05 22:05+0000\n" "PO-Revision-Date: 2018-04-09 00:52+0000\n" "Last-Translator: Zhu Sheng Li \n" "Language-Team: LANGUAGE \n" @@ -37,7 +37,7 @@ msgstr[1] "%(count)d 件问卷成功标记为「已发布」" msgid "Mark selected surveys as published" msgstr "标记选择的问卷为「已发布」" -#: survey/exporter/csv/survey2csv.py:33 +#: survey/exporter/csv/survey2csv.py:35 msgid "Anonymous" msgstr "匿名" diff --git a/survey/models/answer.py b/survey/models/answer.py index 74dd8e26..29ae160c 100644 --- a/survey/models/answer.py +++ b/survey/models/answer.py @@ -38,6 +38,8 @@ def __init__(self, *args, **kwargs): @property def values(self): + if self.body is None: + return [None] if len(self.body) < 3 or self.body[0:3] != "[u'": return [self.body] #  We do not use eval for security reason but it could work with : diff --git a/survey/tests/exporter/csv/test_issue70.py b/survey/tests/exporter/csv/test_issue70.py new file mode 100644 index 00000000..3610dba2 --- /dev/null +++ b/survey/tests/exporter/csv/test_issue70.py @@ -0,0 +1,38 @@ +import os + +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured +from django.test import TestCase +from django.test.utils import override_settings + +from survey.exporter.csv.survey2csv import Survey2Csv +from survey.models import Survey + + +class TestIssue70(TestCase): + fixtures = [os.path.join(settings.ROOT, "survey", "tests", "issue70.json")] + maxDiff = None + + def setUp(self): + self.survey = Survey.objects.get(id=4) + print(self.survey) + self.s2csv = Survey2Csv(self.survey) + header = ( + "user,Email Address,Your Name,Date,STARTS Who? What? Where?,Donations,Books Sold,Your Total Volunteer hours" + " for this week,Your Volunteer Activities this week.,Included in the above hours; how many of those above " + "hours were on filing,New Hires,Charitable Donations,Events held What? Where? Products gotten?" + ) + self.expected_content = """{} +adminebd,user@example.com,Ed Davison,2020-02-03,Left blank,0,0,8,['Admin'; 'Calls'; 'Events'],0,Left blank,100,""".format( + header + ) + + @override_settings(USER_DID_NOT_ANSWER="Left blank") + def test_get_survey_as_csv(self): + self.assertEqual(self.s2csv.survey_to_x(), self.expected_content) + + @override_settings(USER_DID_NOT_ANSWER=None) + def test_get_survey_as_csv_wrong_settings(self): + with self.assertRaises(ImproperlyConfigured) as e: + self.s2csv.survey_to_x() + self.assertIn("USER_DID_NOT_ANSWER need to be set", str(e.exception))