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 #63 from praekelt/feature/issue-63-Fix-survey-resp…
Browse files Browse the repository at this point in the history
…onse-CSV-headers

Fix survey response CSV headers
  • Loading branch information
nathanbegbie committed Nov 30, 2017
2 parents 051bbad + cddeede commit 93d4ffb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
5 changes: 5 additions & 0 deletions molo/surveys/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def create_multiline_field(self, field, options):

@property
def formfields(self):
'''
Override parent function in order to ensure that the
overridden create_..._field methods are referenced
instead of the parent class methods.
'''
formfields = OrderedDict()

for field in self.fields:
Expand Down
15 changes: 8 additions & 7 deletions molo/surveys/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def create_survey_index_pages(sender, instance, **kwargs):
if not instance.get_children().filter(
title='Surveys').exists():
survey_index = SurveysIndexPage(
title='Surveys', slug=('surveys-%s' % (
title='Surveys', slug=('surveys-{}'.format(
generate_slug(instance.title), )))
instance.add_child(instance=survey_index)
survey_index.save_revision().publish()
Expand Down Expand Up @@ -268,7 +268,7 @@ def serve_questions(self, request):
When the last step is submitted correctly, the whole form is saved in
the DB.
"""
session_key_data = 'survey_data-%s' % self.pk
session_key_data = 'survey_data-{}'.format(self.pk)
survey_data = json.loads(request.session.get(session_key_data, '{}'))

paginator = SkipLogicPaginator(
Expand Down Expand Up @@ -366,8 +366,8 @@ def has_page_breaks(self):
)

def serve(self, request, *args, **kwargs):
if not self.allow_multiple_submissions_per_user \
and self.has_user_submitted_survey(request, self.id):
if (not self.allow_multiple_submissions_per_user and
self.has_user_submitted_survey(request, self.id)):
return render(request, self.template, self.get_context(request))

if self.has_page_breaks or self.multi_step:
Expand Down Expand Up @@ -582,15 +582,16 @@ def get_data_fields(self):
if there's one associated.
"""
data_fields = [
('username', _('Username')),
('created_at', _('Submission Date')),
]

# Add segment name to a field label if it is segmented.
for field in self.get_form_fields():
label = field.label
label = field.admin_label

if field.segment:
label = '%s (%s)' % (label, field.segment.name)
label = '{} ({})'.format(label, field.segment.name)

data_fields.append((field.clean_name, label))

Expand Down Expand Up @@ -631,7 +632,7 @@ class PersonalisableSurveyFormField(SkipLogicMixin, AdminLabelMixin,
] + AbstractFormField.panels

def __str__(self):
return '%s - %s' % (self.page, self.label)
return '{} - {}'.format(self.page, self.label)

class Meta(AbstractFormField.Meta):
verbose_name = _('personalisable form field')
Expand Down
76 changes: 72 additions & 4 deletions molo/surveys/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import json

from django.core.serializers.json import DjangoJSONEncoder
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.test.client import Client

from molo.core.models import SiteLanguageRelation, Main, Languages, ArticlePage
from molo.core.tests.base import MoloTestCaseMixin
from molo.surveys.models import (MoloSurveyPage, MoloSurveyFormField,
SurveysIndexPage)

from molo.surveys.models import (
MoloSurveyPage,
MoloSurveyFormField,
SurveysIndexPage,
PersonalisableSurvey,
PersonalisableSurveyFormField,
)
from wagtail_personalisation.models import Segment
from wagtail_personalisation.rules import UserIsLoggedInRule

User = get_user_model()

Expand Down Expand Up @@ -61,6 +70,32 @@ def create_molo_survey_page(self, parent, **kwargs):
)
return molo_survey_page, molo_survey_form_field

def create_personalisable_molo_survey_page(self, parent, **kwargs):
# create segment for personalisation
test_segment = Segment.objects.create(name="Test Segment")
UserIsLoggedInRule.objects.create(
segment=test_segment,
is_logged_in=True)

personalisable_survey = PersonalisableSurvey(
title='Test Survey', slug='test-survey',
intro='Introduction to Test Survey ...',
thank_you_text='Thank you for taking the Test Survey',
**kwargs
)

parent.add_child(instance=personalisable_survey)
personalisable_survey.save_revision().publish()

molo_survey_form_field = PersonalisableSurveyFormField.objects.create(
field_type='singleline',
label='Question 1',
admin_label='question_1',
page=personalisable_survey,
segment=test_segment)

return personalisable_survey, molo_survey_form_field

def test_convert_to_article(self):
molo_survey_page, molo_survey_form_field = \
self.create_molo_survey_page(parent=self.section_index)
Expand Down Expand Up @@ -123,7 +158,7 @@ def test_convert_to_article(self):
# it should not show convert to article as there is already article
self.assertNotContains(response, 'Convert to Article')

def test_export_submission(self):
def test_export_submission_standard_survey(self):
molo_survey_page, molo_survey_form_field = \
self.create_molo_survey_page(parent=self.section_index)

Expand All @@ -144,3 +179,36 @@ def test_export_submission(self):
self.assertNotContains(response, molo_survey_form_field.label)
self.assertContains(response, molo_survey_form_field.admin_label)
self.assertContains(response, answer)

def test_export_submission_personalisable_survey(self):
molo_survey_page, molo_survey_form_field = (
self.create_personalisable_molo_survey_page(
parent=self.section_index))

answer = 'PYTHON'

molo_survey_page.get_submission_class().objects.create(
form_data=json.dumps({"question-1": answer},
cls=DjangoJSONEncoder),
page=molo_survey_page,
user=self.user
)

self.client.force_login(self.super_user)
response = self.client.get(
'/admin/surveys/submissions/{}/'.format(molo_survey_page.id),
{'action': 'CSV'},
)

self.assertEquals(response.status_code, 200)
self.assertContains(response, 'Username')
self.assertContains(response, 'Submission Date')
self.assertNotContains(response, molo_survey_form_field.label)

self.assertContains(
response,
'{} ({})'.format(molo_survey_form_field.admin_label,
molo_survey_form_field.segment.name))

self.assertContains(response, self.user.username)
self.assertContains(response, answer)

0 comments on commit 93d4ffb

Please sign in to comment.