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

Commit

Permalink
Merge 54767b8 into fa96aba
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-moola committed Jul 11, 2018
2 parents fa96aba + 54767b8 commit 51a80c0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
71 changes: 70 additions & 1 deletion molo/surveys/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from .widgets import NaturalDateInput


CHARACTER_COUNT_CHOICE_LIMIT = 512


class CharacterCountWidget(forms.TextInput):
class Media:
js = ('js/widgets/character_count.js',)
Expand All @@ -31,6 +34,50 @@ def render(self, name, value, attrs=None):
)


class CharacterCountMixin(object):
max_length = CHARACTER_COUNT_CHOICE_LIMIT

def __init__(self, *args, **kwargs):
self.max_length = kwargs.pop('max_length', self.max_length)
super(CharacterCountMixin, self).__init__(*args, **kwargs)

self.error_messages['max_length'] = _(
'This field can not be more than {max_length} characters long'
).format(max_length=self.max_length)

def validate(self, value):
super(CharacterCountMixin, self).validate(value)
if len(value) > self.max_length:
raise ValidationError(
self.error_messages['max_length'],
code='max_length', params={'value': value},
)


class CharacterCountMultipleChoiceField(
CharacterCountMixin, forms.MultipleChoiceField
):
""" Limit character count for Multi choice fields """


class CharacterCountChoiceField(
CharacterCountMixin, forms.ChoiceField
):
""" Limit character count for choice fields """


class CharacterCountCheckboxSelectMultiple(
CharacterCountMixin, forms.CheckboxSelectMultiple
):
""" Limit character count for checkbox fields """


class CharacterCountCheckboxInput(
CharacterCountMixin, forms.CheckboxInput
):
""" Limit character count for checkbox fields """


class MultiLineWidget(forms.Textarea):
def render(self, name, value, attrs=None):
return format_html(
Expand Down Expand Up @@ -63,6 +110,28 @@ def create_datetime_field(self, field, options):
def create_positive_number_field(self, field, options):
return forms.DecimalField(min_value=0, **options)

def create_dropdown_field(self, field, options):
options['choices'] = map(
lambda x: (x.strip(), x.strip()),
field.choices.split(','))
return CharacterCountChoiceField(**options)

def create_radio_field(self, field, options):
options['choices'] = map(
lambda x: (x.strip(), x.strip()),
field.choices.split(','))
return CharacterCountChoiceField(widget=forms.RadioSelect, **options)

def create_checkboxes_field(self, field, options):
options['choices'] = [
(x.strip(), x.strip()) for x in field.choices.split(',')
]
options['initial'] = [
x.strip() for x in field.default_value.split(',')
]
return CharacterCountMultipleChoiceField(
widget=forms.CheckboxSelectMultiple, **options)

@property
def formfields(self):
'''
Expand All @@ -76,7 +145,6 @@ def formfields(self):

for field in self.fields:
options = self.get_field_options(field)

if field.field_type in self.FIELD_TYPES:
method = getattr(self,
self.FIELD_TYPES[field.field_type].__name__)
Expand Down Expand Up @@ -163,6 +231,7 @@ def save(self, *args, **kwargs):


class BaseMoloSurveyForm(WagtailAdminPageForm):

def clean(self):
cleaned_data = super(BaseMoloSurveyForm, self).clean()

Expand Down
9 changes: 9 additions & 0 deletions molo/surveys/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
MoloSurveyForm,
PersonalisableMoloSurveyForm,
SurveysFormBuilder,
CHARACTER_COUNT_CHOICE_LIMIT,
)
from .rules import ( # noqa
ArticleTagRule,
Expand Down Expand Up @@ -550,6 +551,14 @@ def clean(self):
raise ValidationError(
{'default_value': ["Must be a valid date", ]})

if self.choices and len(self.choices) > CHARACTER_COUNT_CHOICE_LIMIT:
raise ValidationError(
{'field_type': _(
'The combined choices\' maximum characters'
' limit has been exceeded ({max_limit} character(s)).'
).format(max_limit=CHARACTER_COUNT_CHOICE_LIMIT)}
)


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

Expand Down

0 comments on commit 51a80c0

Please sign in to comment.