Skip to content

Commit

Permalink
Permit to export survey with None value to CSV
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Pierre-Sassoulas committed Feb 5, 2020
1 parent fa61592 commit edf4e2c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 13 deletions.
8 changes: 7 additions & 1 deletion survey/exporter/csv/survey2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions survey/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
Expand Down Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions survey/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions survey/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
Expand Down Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions survey/locale/ja/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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' <[email protected]>'\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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 "匿名"

Expand Down
4 changes: 2 additions & 2 deletions survey/locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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' <[email protected]>'\n"
"Language-Team: \n"
Expand Down Expand Up @@ -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 "Анонимно"

Expand Down
4 changes: 2 additions & 2 deletions survey/locale/zh/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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 "匿名"

Expand Down
2 changes: 2 additions & 0 deletions survey/models/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
38 changes: 38 additions & 0 deletions survey/tests/exporter/csv/test_issue70.py
Original file line number Diff line number Diff line change
@@ -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,[email protected],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))

0 comments on commit edf4e2c

Please sign in to comment.