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.10.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaitlyn Crawford committed Aug 23, 2018
2 parents a01227a + 88aea36 commit 1a42ab8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGE LOG
==========
6.10.1
------
- Allow SurveySubmissionDataRule to be created using label or field_name

6.10.0
------
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.10.0
6.10.1
14 changes: 10 additions & 4 deletions molo/surveys/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _

from django.utils.six import text_type
from django.utils.text import slugify
from unidecode import unidecode

from wagtail.wagtailcore.blocks.stream_block import StreamBlockValidationError
from wagtail.wagtailadmin.edit_handlers import (
FieldPanel,
Expand Down Expand Up @@ -64,9 +68,7 @@ class SurveySubmissionDataRule(AbstractBaseRule):
on_delete=models.CASCADE)
field_name = models.CharField(
_('field name'), max_length=255,
help_text=_('Field\'s label in a lower-case '
'format with spaces replaced by '
'dashes. For possible choices '
help_text=_('Field\'s label. For possible choices '
'please input any text and save, '
'so it will be displayed in the '
'error messages below the '
Expand Down Expand Up @@ -153,13 +155,17 @@ def clean(self):
if not self.survey_id:
return

# Make sure field name is in correct format
self.field_name = str(slugify(text_type(unidecode(self.field_name))))

# Make sure field name is a valid name
field_names = [f.clean_name for f in self.survey.get_form_fields()]

if self.field_name not in field_names:
field_labels = [f.label for f in self.survey.get_form_fields()]
raise ValidationError({
'field_name': [_('You need to choose valid field name out '
'of: "%s".') % '", "'.join(field_names)]
'of: "%s".') % '", "'.join(field_labels)]
})

# Convert value from the rule into Python value.
Expand Down
34 changes: 34 additions & 0 deletions molo/surveys/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,40 @@ def setUp(self):

self.survey.refresh_from_db()

def test_rule_validates_with_correct_field_name(self):
rule = SurveySubmissionDataRule(
survey=self.survey, operator=SurveySubmissionDataRule.EQUALS,
expected_response='super random text',
field_name='incorrect-field-name')

with self.assertRaises(ValidationError):
rule.clean()

rule.field_name = 'singleline-text'
try:
rule.clean()
except ValidationError:
self.fail(
"SurveySubmissionDataRule.clean()raised ValidationError!")

def test_rule_validates_with_correct_label_name(self):
rule = SurveySubmissionDataRule(
survey=self.survey, operator=SurveySubmissionDataRule.EQUALS,
expected_response='super random text',
field_name='Incorrect Field name!!')

with self.assertRaises(ValidationError):
rule.clean()

rule.field_name = 'Singleline Text'
try:
rule.clean()
except ValidationError:
self.fail(
"SurveySubmissionDataRule.clean()raised ValidationError!")
# check the field_name has been changed to the correct one
self.assertEqual(rule.field_name, 'singleline-text')

def test_get_field_model(self):
rule = SurveySubmissionDataRule(
survey=self.survey, operator=SurveySubmissionDataRule.EQUALS,
Expand Down

0 comments on commit 1a42ab8

Please sign in to comment.