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

Commit

Permalink
Merge 8074ee9 into 13ad47e
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-moola committed Aug 2, 2018
2 parents 13ad47e + 8074ee9 commit 626c5d0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 48 deletions.
17 changes: 16 additions & 1 deletion molo/surveys/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.utils import six

from wagtail.wagtailadmin.forms import WagtailAdminPageForm
from wagtailsurveys.forms import FormBuilder
from wagtailsurveys.forms import FormBuilder, BaseForm

from .blocks import SkipState, VALID_SKIP_LOGIC, VALID_SKIP_SELECTORS
from .widgets import NaturalDateInput
Expand All @@ -20,6 +20,17 @@
CHARACTER_COUNT_CHOICE_LIMIT = 512


class SurveyBaseForm(BaseForm):

def pk_cleaned_data(self, fields):
pk_cleaned_field_data = {}
for field in fields:
if field.clean_name in self.cleaned_data.keys():
pk_cleaned_field_data[field.pk_clean_name] \
= self.cleaned_data.get(field.clean_name)
return pk_cleaned_field_data


class CharacterCountWidget(forms.TextInput):
class Media:
js = ('js/widgets/character_count.js',)
Expand Down Expand Up @@ -154,6 +165,10 @@ def formfields(self):

return formfields

def get_form_class(self):
return type(str(
'WagtailSurveysForm'), (SurveyBaseForm,), self.formfields)


class CSVGroupCreationForm(forms.ModelForm):
"""Create group with initial users supplied via CSV file."""
Expand Down
19 changes: 9 additions & 10 deletions molo/surveys/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import json
import datetime
from unidecode import unidecode
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.paginator import EmptyPage, PageNotAnInteger
Expand All @@ -16,9 +15,6 @@
from django.shortcuts import redirect, render
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _
from django.utils.text import slugify
from django.utils.encoding import smart_str
from django.utils.six import text_type
from modelcluster.fields import ParentalKey
from molo.core.blocks import MarkDownBlock
from molo.core.models import (
Expand Down Expand Up @@ -81,9 +77,8 @@ class Meta:
ordering = ['sort_order']

@property
def clean_name(self):
return str(slugify(text_type(unidecode(
'{} {}'.format(self.pk, smart_str(self.label))))))
def pk_clean_name(self):
return '{}-{}'.format(self.pk, self.clean_name)


class TermsAndConditionsIndexPage(TranslatablePageMixinNotRoutable, MoloPage):
Expand Down Expand Up @@ -344,8 +339,9 @@ def serve_questions(self, request):
user=request.user,
)
if prev_form.is_valid():
fields = self.get_form_fields()
# If data for step is valid, update the session
survey_data.update(prev_form.cleaned_data)
survey_data.update(prev_form.pk_cleaned_data(fields))
self.save_data(request, survey_data)

if prev_step.has_next():
Expand All @@ -371,9 +367,12 @@ def serve_questions(self, request):

# We fill in the missing fields which were skipped with
# a default value
for question in self.get_form_fields():
if question.clean_name not in data:
for question in fields:
if question.pk_clean_name not in data:
form.cleaned_data[question.clean_name] = SKIP
else:
form.cleaned_data[question.clean_name]\
= data[question.pk_clean_name]

self.process_form_submission(form)
del request.session[self.session_key_data]
Expand Down
11 changes: 4 additions & 7 deletions molo/surveys/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def test_convert_to_article(self):
self.assertContains(response, molo_survey_page.introduction)
self.assertContains(response, molo_survey_form_field.label)

key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand Down Expand Up @@ -177,16 +176,15 @@ def test_export_submission_standard_survey(self):

self.client.force_login(self.user)
answer = 'PYTHON'
key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(molo_survey_page.url, {key: answer})
self.assertEqual(response.status_code, 302)

self.client.force_login(self.super_user)
response = self.client.get(
'/admin/surveys/submissions/%s/' % (molo_survey_page.id),
'/admin/surveys/submissions/%s/' % molo_survey_page.id,
{'action': 'CSV'},
)
self.assertEquals(response.status_code, 200)
Expand All @@ -202,8 +200,7 @@ def test_export_submission_personalisable_survey(self):
parent=self.section_index))

answer = 'PYTHON'
key = '{}-{}'.format(
molo_survey_page.get_form_fields().first().pk,
key = '{}'.format(
molo_survey_page.get_form_fields().first(
).label.lower().replace(' ', '-')
)
Expand Down
42 changes: 14 additions & 28 deletions molo/surveys/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ def test_submit_survey_as_logged_in_user(self):
self.assertContains(response, molo_survey_form_field.label)
self.assertContains(response, molo_survey_page.submit_text)

key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand All @@ -154,8 +153,7 @@ def test_anonymous_submissions_option(self):
self.assertContains(response, molo_survey_page.introduction)
self.assertContains(response, molo_survey_form_field.label)

key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand Down Expand Up @@ -199,8 +197,7 @@ def test_multiple_submissions_option(self, anonymous=False):
self.assertContains(response, molo_survey_page.introduction)
self.assertContains(response, molo_survey_form_field.label)

key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand All @@ -222,8 +219,7 @@ def test_show_results_option(self):
self.assertContains(response, molo_survey_page.title)
self.assertContains(response, molo_survey_page.introduction)
self.assertContains(response, molo_survey_form_field.label)
key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand All @@ -247,8 +243,7 @@ def test_show_results_as_percentage_option(self):
self.assertContains(response, molo_survey_page.title)
self.assertContains(response, molo_survey_page.introduction)
self.assertContains(response, molo_survey_form_field.label)
key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand All @@ -258,8 +253,7 @@ def test_show_results_as_percentage_option(self):
self.assertContains(response, molo_survey_form_field.label)
self.assertContains(response, 'python</span> 100%')

key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand Down Expand Up @@ -292,8 +286,7 @@ def test_multi_step_option(self):
self.assertContains(response, molo_survey_form_field.label)
self.assertNotContains(response, extra_molo_survey_form_field.label)
self.assertContains(response, 'Next Question')
key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(molo_survey_page.url + '?p=2', {
Expand All @@ -306,8 +299,7 @@ def test_multi_step_option(self):
self.assertContains(response, extra_molo_survey_form_field.label)
self.assertContains(response, molo_survey_page.submit_text)

key = '{}-{}'.format(
extra_molo_survey_form_field.pk,
key = '{}'.format(
extra_molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand Down Expand Up @@ -338,8 +330,7 @@ def test_can_submit_after_validation_error(self):

self.assertContains(response, 'This field is required.')

key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand Down Expand Up @@ -447,16 +438,14 @@ def test_can_see_translated_survey_submissions_in_admin(self):
self.assertContains(response, 'French translation of Test Survey')

# Submit responses to both surveys
key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
self.client.post(molo_survey_page.url, {
key: 'an english answer'
})

key = '{}-{}'.format(
translated_survey_form_field.pk,
key = '{}'.format(
translated_survey_form_field.label.lower().replace(' ', '-')
)
self.client.post(translated_survey.url, {
Expand Down Expand Up @@ -617,8 +606,7 @@ def test_survey_list_display_direct_logged_in(self):
self.assertNotContains(response, 'Please log in to take this survey')
self.assertContains(response, molo_survey_form_field.label)

key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand All @@ -642,8 +630,7 @@ def test_anonymous_submissions_option_display_direct(self):
response = self.client.get('/')

self.assertContains(response, molo_survey_form_field.label)
key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand All @@ -665,8 +652,7 @@ def test_multiple_submissions_display_direct(self):

self.user = self.login()

key = '{}-{}'.format(
molo_survey_form_field.pk,
key = '{}'.format(
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
Expand Down
20 changes: 18 additions & 2 deletions molo/surveys/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, object_list, data=dict(), answered=dict()):
super(SkipLogicPaginator, self).__init__(object_list, per_page=1)

self.question_labels = [
question.clean_name for question in self.object_list
question.pk_clean_name for question in self.object_list
]

self.page_breaks = [
Expand All @@ -44,9 +44,13 @@ def __init__(self, object_list, data=dict(), answered=dict()):

# add the missing data
self.new_answers.update({
checkbox.clean_name: 'off'
checkbox.pk_clean_name: 'off'
for checkbox in self.missing_checkboxes
})
self.new_answers.update({
checkbox.clean_name: 'off'
for checkbox in self.pk_cleaned_missing_checkboxes
})

def _get_page(self, *args, **kwargs):
return SkipLogicPage(*args, **kwargs)
Expand Down Expand Up @@ -138,6 +142,18 @@ def index_of_questions(self, data):

@cached_property
def missing_checkboxes(self):
return [
question
for question in self.object_list[
# Correct for the slice
self.first_question_index:self.last_question_index + 1
]
if question.field_type == 'checkbox' and
question.pk_clean_name not in self.new_answers
]

@cached_property
def pk_cleaned_missing_checkboxes(self):
return [
question
for question in self.object_list[
Expand Down

0 comments on commit 626c5d0

Please sign in to comment.