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

Commit

Permalink
Merge branch 'release/6.9.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-moola committed Jul 18, 2018
2 parents 2672415 + 7aff21a commit f388bf8
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 70 deletions.
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
CHANGE LOG
==========
6.9.1
-----
- Bug fix: add pk to session stored survey answers to prevent duplicate dict keys

6.9.0
-----
- Override choice field to textfield from charfied with 512 limit

6.8.2
-----
- Bug Fix: redirect to translated surveys when another language is selected

6.8.1
-----
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.8.1
6.9.1
20 changes: 20 additions & 0 deletions molo/surveys/migrations/0027_override-512-char-limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.10 on 2018-07-12 10:37
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('surveys', '0026_remove_molosurveypageview_tag'),
]

operations = [
migrations.AlterField(
model_name='molosurveyformfield',
name='choices',
field=models.TextField(blank=True, help_text='Comma separated list of choices. Only applicable in checkboxes,radio and dropdown.', verbose_name='choices'),
),
]
62 changes: 48 additions & 14 deletions molo/surveys/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,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 @@ -14,6 +14,8 @@
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.six import text_type
from modelcluster.fields import ParentalKey
from molo.core.blocks import MarkDownBlock
from molo.core.models import (
Expand Down Expand Up @@ -69,6 +71,17 @@
FooterPage.parent_page_types += ['surveys.TermsAndConditionsIndexPage']


class SurveyAbstractFormField(AbstractFormField):
class Meta:
abstract = True
ordering = ['sort_order']

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


class TermsAndConditionsIndexPage(TranslatablePageMixinNotRoutable, MoloPage):
parent_page_types = ['surveys.SurveysIndexPage']
subpage_types = ['core.Footerpage']
Expand Down Expand Up @@ -286,6 +299,13 @@ def serve_questions(self, request):
When the last step is submitted correctly, the whole form is saved in
the DB.
"""
context = self.get_context(request)
# this will only return a page if there is a translation
page = context['page'].get_translation_for(
locale=request.LANGUAGE_CODE, site=request.site)
if page:
# if there is a translation, redirect to the translated page
return redirect(page.url)
survey_data = self.load_data(request)

paginator = SkipLogicPaginator(
Expand Down Expand Up @@ -366,7 +386,7 @@ def serve_questions(self, request):
# Create empty form for non-POST requests
form_class = self.get_form_class_for_step(step)
form = form_class(page=self, user=request.user)
context = self.get_context(request)

context['form'] = form
context['fields_step'] = step
context['is_intermediate_step'] = step.possibly_has_next()
Expand Down Expand Up @@ -453,7 +473,7 @@ class Meta:
abstract = True


surveys_models.AbstractFormField.panels.append(FieldPanel('page_break'))
SurveyAbstractFormField.panels.append(FieldPanel('page_break'))


class AdminLabelMixin(models.Model):
Expand All @@ -468,8 +488,8 @@ class Meta:
abstract = True


surveys_models.AbstractFormField.panels.append(FieldPanel('admin_label'))
surveys_models.AbstractFormField._meta.get_field('label').verbose_name = (
SurveyAbstractFormField.panels.append(FieldPanel('admin_label'))
SurveyAbstractFormField._meta.get_field('label').verbose_name = (
'Question'
)

Expand Down Expand Up @@ -528,15 +548,26 @@ def save(self, *args, **kwargs):


class MoloSurveyFormField(SkipLogicMixin, AdminLabelMixin,
QuestionPaginationMixin, AbstractFormField):
AbstractFormField.FORM_FIELD_CHOICES += (
QuestionPaginationMixin, SurveyAbstractFormField):

page = ParentalKey(MoloSurveyPage, related_name='survey_form_fields')
SurveyAbstractFormField.FORM_FIELD_CHOICES += (
('positive_number', _("Positive Number")),)
choices = models.TextField(
verbose_name=_('choices'),
blank=True,
help_text=_(
'Comma separated list of choices. Only applicable in checkboxes,'
'radio and dropdown.')
)
field_type = models.CharField(
verbose_name=_('field type'),
max_length=16, choices=AbstractFormField.FORM_FIELD_CHOICES)
page = ParentalKey(MoloSurveyPage, related_name='survey_form_fields')
max_length=16, choices=SurveyAbstractFormField.FORM_FIELD_CHOICES)

class Meta(AbstractFormField.Meta):
def __init__(self, *args, **kwargs):
super(MoloSurveyFormField, self).__init__(*args, **kwargs)

class Meta(SurveyAbstractFormField.Meta):
pass

def clean(self):
Expand All @@ -551,7 +582,7 @@ def clean(self):
{'default_value': ["Must be a valid date", ]})


surveys_models.AbstractFormField.panels[4] = SkipLogicStreamPanel('skip_logic')
SurveyAbstractFormField.panels[4] = SkipLogicStreamPanel('skip_logic')


class MoloSurveySubmission(surveys_models.AbstractFormSubmission):
Expand Down Expand Up @@ -675,7 +706,7 @@ def serve(self, request, *args, **kwargs):

class PersonalisableSurveyFormField(SkipLogicMixin, AdminLabelMixin,
QuestionPaginationMixin,
AbstractFormField):
SurveyAbstractFormField):
"""
Form field that has a segment assigned.
"""
Expand All @@ -691,12 +722,15 @@ class PersonalisableSurveyFormField(SkipLogicMixin, AdminLabelMixin,

panels = [
FieldPanel('segment')
] + AbstractFormField.panels
] + SurveyAbstractFormField.panels

def __init__(self, *args, **kwargs):
super(PersonalisableSurveyFormField, self).__init__(*args, **kwargs)

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

class Meta(AbstractFormField.Meta):
class Meta(SurveyAbstractFormField.Meta):
verbose_name = _('personalisable form field')

def clean(self):
Expand Down
28 changes: 20 additions & 8 deletions molo/surveys/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ def test_convert_to_article(self):
self.assertContains(response, molo_survey_page.title)
self.assertContains(response, molo_survey_page.introduction)
self.assertContains(response, molo_survey_form_field.label)
response = self.client.post(molo_survey_page.url, {
molo_survey_form_field.label.lower().replace(' ', '-'): 'python'
}, follow=True)

key = '{}-{}'.format(
molo_survey_form_field.pk,
molo_survey_form_field.label.lower().replace(' ', '-')
)
response = self.client.post(
molo_survey_page.url, {key: 'python'}, follow=True)
self.client.logout()
self.client.login(
username='testuser',
Expand Down Expand Up @@ -173,9 +177,12 @@ def test_export_submission_standard_survey(self):

self.client.force_login(self.user)
answer = 'PYTHON'
response = self.client.post(molo_survey_page.url, {
molo_survey_form_field.label.lower().replace(' ', '-'): answer
})
key = '{}-{}'.format(
molo_survey_form_field.pk,
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(
Expand All @@ -195,10 +202,15 @@ def test_export_submission_personalisable_survey(self):
parent=self.section_index))

answer = 'PYTHON'
key = '{}-{}'.format(
molo_survey_page.get_form_fields().first().pk,
molo_survey_page.get_form_fields().first(
).label.lower().replace(' ', '-')
)

molo_survey_page.get_submission_class().objects.create(
form_data=json.dumps({"question-1": answer},
cls=DjangoJSONEncoder),
form_data=json.dumps(
{key: answer}, cls=DjangoJSONEncoder),
page=molo_survey_page,
user=self.user
)
Expand Down
25 changes: 25 additions & 0 deletions molo/surveys/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ def setUp(self):
required=True
)

def test_survey_options_512_limit_overriden(self):
field_choices = [
'My favourite animal is a dog, because they bark',
'My favourite animal is a cat, because they meuow',
'My favourite animal is a bird, because they fly',
'My favourite animal is a lion, because that roar',
'My favourite animal is a hamster, because they have tiny legs',
'My favourite animal is a tiger, because they have stripes',
'My favourite animal is a frog, because they go crickit',
'My favourite animal is a fish, because they have nice eyes',
'My favourite animal is a chicken, because they cannot fly',
'My favourite animal is a duck, because they keep it down',
'My favourite animal is a wolf, because they howl',
'My favourite animal is a chamelion, because they fit in',
]
choice_field = MoloSurveyFormField.objects.create(
page=self.survey,
sort_order=1,
label='Your favourite animal',
field_type='dropdown',
skip_logic=skip_logic_data(field_choices),
required=True
)
self.assertTrue(len(choice_field.choices) > 512)

def test_choices_updated_from_streamfield_on_save(self):
self.assertEqual(
','.join(self.field_choices),
Expand Down
Loading

0 comments on commit f388bf8

Please sign in to comment.