Skip to content
Snippets Groups Projects
Commit 18f03eb5 authored by sks444's avatar sks444
Browse files

Implement score field without text box block

parent b4629f57
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ from hypha.apply.review.options import ( ...@@ -12,6 +12,7 @@ from hypha.apply.review.options import (
RATE_CHOICE_NA, RATE_CHOICE_NA,
RATE_CHOICES_DICT, RATE_CHOICES_DICT,
RECOMMENDATION_CHOICES, RECOMMENDATION_CHOICES,
SCORE_CHOICES,
VISIBILILTY_HELP_TEXT, VISIBILILTY_HELP_TEXT,
VISIBILITY, VISIBILITY,
) )
...@@ -53,6 +54,40 @@ class ScoreFieldBlock(OptionalFormFieldBlock): ...@@ -53,6 +54,40 @@ class ScoreFieldBlock(OptionalFormFieldBlock):
return super().render(value, context) return super().render(value, context)
class ScoreFieldWithoutTextBlock(OptionalFormFieldBlock):
"""
There are two ways score could be accepted on reviews.
One is to use ScoreFieldBlock, where you need to put text answer along with
giving score on the review.
Second is to use this block to just select a reasonable score with adding
any text as answer.
This block uses SCORE_CHOICES instead of RATE_CHOICES, only difference is to
have empty string('') in place of NA for text value `n/a - choose not to answer`
as it helps to render this value as default to the forms and also when this field is
required it automatically handles validation on empty string.
"""
name = 'score without text'
field_class = forms.ChoiceField
class Meta:
icon = 'order'
def get_field_kwargs(self, struct_value):
kwargs = super().get_field_kwargs(struct_value)
kwargs['choices'] = SCORE_CHOICES
return kwargs
def render(self, value, context=None):
data = int(context['data'])
choices = dict(SCORE_CHOICES)
context['data'] = choices[data]
return super().render(value, context)
class ReviewMustIncludeFieldBlock(MustIncludeFieldBlock): class ReviewMustIncludeFieldBlock(MustIncludeFieldBlock):
pass pass
...@@ -117,6 +152,7 @@ class ReviewCustomFormFieldsBlock(CustomFormFieldsBlock): ...@@ -117,6 +152,7 @@ class ReviewCustomFormFieldsBlock(CustomFormFieldsBlock):
text = TextFieldBlock(group=_('Fields')) text = TextFieldBlock(group=_('Fields'))
text_markup = RichTextBlock(group=_('Fields'), label=_('Paragraph')) text_markup = RichTextBlock(group=_('Fields'), label=_('Paragraph'))
score = ScoreFieldBlock(group=_('Fields')) score = ScoreFieldBlock(group=_('Fields'))
score_without_text = ScoreFieldWithoutTextBlock(group=_('Fields'))
checkbox = CheckboxFieldBlock(group=_('Fields')) checkbox = CheckboxFieldBlock(group=_('Fields'))
dropdown = DropdownFieldBlock(group=_('Fields')) dropdown = DropdownFieldBlock(group=_('Fields'))
......
...@@ -89,6 +89,15 @@ class ReviewModelForm(StreamBaseForm, forms.ModelForm, metaclass=MixedMetaClass) ...@@ -89,6 +89,15 @@ class ReviewModelForm(StreamBaseForm, forms.ModelForm, metaclass=MixedMetaClass)
score = 0 score = 0
scores.append(score) scores.append(score)
# Check if there are score_fields_without_text and also
# append scores from them.
for field in self.instance.score_fields_without_text:
score = int(data.get(field.id))
# Include '' answers as 0.
if score == '':
score = 0
scores.append(score)
try: try:
return sum(scores) / len(scores) return sum(scores) / len(scores)
except ZeroDivisionError: except ZeroDivisionError:
......
# Generated by Django 2.2.13 on 2020-07-20 10:55
from django.db import migrations
import wagtail.core.blocks
import wagtail.core.blocks.static_block
import wagtail.core.fields
class Migration(migrations.Migration):
dependencies = [
('review', '0022_add_word_limit_to_text_blocks'),
]
operations = [
migrations.AlterField(
model_name='review',
name='form_fields',
field=wagtail.core.fields.StreamField([('rich_text', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('default_value', wagtail.core.blocks.TextBlock(label='Default value', required=False)), ('word_limit', wagtail.core.blocks.IntegerBlock(default=1000, label='Word limit'))], group='Fields')), ('markdown_text', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('default_value', wagtail.core.blocks.TextBlock(label='Default value', required=False)), ('word_limit', wagtail.core.blocks.IntegerBlock(default=1000, label='Word limit'))], group='Fields')), ('char', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('format', wagtail.core.blocks.ChoiceBlock(choices=[('email', 'Email'), ('url', 'URL')], label='Format', required=False)), ('default_value', wagtail.core.blocks.CharBlock(label='Default value', required=False))], group='Fields')), ('text', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('default_value', wagtail.core.blocks.TextBlock(label='Default value', required=False)), ('word_limit', wagtail.core.blocks.IntegerBlock(default=1000, label='Word limit'))], group='Fields')), ('text_markup', wagtail.core.blocks.RichTextBlock(group='Fields', label='Paragraph')), ('score', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False))], group='Fields')), ('score_without_text', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False))], group='Fields')), ('checkbox', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('default_value', wagtail.core.blocks.BooleanBlock(required=False))], group='Fields')), ('dropdown', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('choices', wagtail.core.blocks.ListBlock(wagtail.core.blocks.CharBlock(label='Choice')))], group='Fields')), ('recommendation', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('info', wagtail.core.blocks.static_block.StaticBlock())], group=' Required')), ('comments', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('info', wagtail.core.blocks.static_block.StaticBlock())], group=' Required')), ('visibility', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('info', wagtail.core.blocks.static_block.StaticBlock())], group=' Required'))]),
),
migrations.AlterField(
model_name='reviewform',
name='form_fields',
field=wagtail.core.fields.StreamField([('rich_text', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('default_value', wagtail.core.blocks.TextBlock(label='Default value', required=False)), ('word_limit', wagtail.core.blocks.IntegerBlock(default=1000, label='Word limit'))], group='Fields')), ('markdown_text', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('default_value', wagtail.core.blocks.TextBlock(label='Default value', required=False)), ('word_limit', wagtail.core.blocks.IntegerBlock(default=1000, label='Word limit'))], group='Fields')), ('char', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('format', wagtail.core.blocks.ChoiceBlock(choices=[('email', 'Email'), ('url', 'URL')], label='Format', required=False)), ('default_value', wagtail.core.blocks.CharBlock(label='Default value', required=False))], group='Fields')), ('text', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('default_value', wagtail.core.blocks.TextBlock(label='Default value', required=False)), ('word_limit', wagtail.core.blocks.IntegerBlock(default=1000, label='Word limit'))], group='Fields')), ('text_markup', wagtail.core.blocks.RichTextBlock(group='Fields', label='Paragraph')), ('score', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False))], group='Fields')), ('score_without_text', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False))], group='Fields')), ('checkbox', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('default_value', wagtail.core.blocks.BooleanBlock(required=False))], group='Fields')), ('dropdown', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('required', wagtail.core.blocks.BooleanBlock(label='Required', required=False)), ('choices', wagtail.core.blocks.ListBlock(wagtail.core.blocks.CharBlock(label='Choice')))], group='Fields')), ('recommendation', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('info', wagtail.core.blocks.static_block.StaticBlock())], group=' Required')), ('comments', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('info', wagtail.core.blocks.static_block.StaticBlock())], group=' Required')), ('visibility', wagtail.core.blocks.StructBlock([('field_label', wagtail.core.blocks.CharBlock(label='Label')), ('help_text', wagtail.core.blocks.TextBlock(label='Help text', required=False)), ('help_link', wagtail.core.blocks.URLBlock(label='Help link', required=False)), ('info', wagtail.core.blocks.static_block.StaticBlock())], group=' Required'))]),
),
]
...@@ -20,6 +20,7 @@ from .blocks import ( ...@@ -20,6 +20,7 @@ from .blocks import (
RecommendationCommentsBlock, RecommendationCommentsBlock,
ReviewCustomFormFieldsBlock, ReviewCustomFormFieldsBlock,
ScoreFieldBlock, ScoreFieldBlock,
ScoreFieldWithoutTextBlock,
VisibilityBlock, VisibilityBlock,
) )
from .options import ( from .options import (
...@@ -46,6 +47,10 @@ class ReviewFormFieldsMixin(models.Model): ...@@ -46,6 +47,10 @@ class ReviewFormFieldsMixin(models.Model):
def score_fields(self): def score_fields(self):
return self._get_field_type(ScoreFieldBlock, many=True) return self._get_field_type(ScoreFieldBlock, many=True)
@property
def score_fields_without_text(self):
return self._get_field_type(ScoreFieldWithoutTextBlock, many=True)
@property @property
def recommendation_field(self): def recommendation_field(self):
return self._get_field_type(RecommendationBlock) return self._get_field_type(RecommendationBlock)
......
...@@ -9,6 +9,18 @@ RATE_CHOICES = ( ...@@ -9,6 +9,18 @@ RATE_CHOICES = (
(5, '5. Excellent'), (5, '5. Excellent'),
(NA, 'n/a - choose not to answer'), (NA, 'n/a - choose not to answer'),
) )
SCORE_CHOICES = (
(0, '0. Need more info'),
(1, '1. Poor'),
(2, '2. Not so good'),
(3, '3. Is o.k.'),
(4, '4. Good'),
(5, '5. Excellent'),
('', 'n/a - choose not to answer'),
)
RATE_CHOICES_DICT = dict(RATE_CHOICES) RATE_CHOICES_DICT = dict(RATE_CHOICES)
RATE_CHOICE_NA = RATE_CHOICES_DICT[NA] RATE_CHOICE_NA = RATE_CHOICES_DICT[NA]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment