Skip to content

Commit

Permalink
Add integer type question with min and max fields
Browse files Browse the repository at this point in the history
  • Loading branch information
DevChima committed Jun 20, 2024
1 parent 7313dca commit 067b234
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add Semantic ID for form answers
- Add version number to distinguish question sets
- Add Freetext question type for forms
- Add Integer question with min and max values
### Changed
- Increased SMS limit to 459 characters
Expand Down
6 changes: 6 additions & 0 deletions home/export_assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ExportRow:
question: str
explainer: str
error: str
min: int
max: int
answers: str
scores: str
semantic_ids: str
Expand Down Expand Up @@ -85,6 +87,8 @@ def perform_export(self) -> Iterable[ExportRow]:
question=question.value["question"],
explainer=question.value["explainer"],
error=question.value.get("error"),
min=question.value.get("min"),
max=question.value.get("max"),
answers=serialize_list(answers),
scores=serialize_list(scores),
semantic_ids=serialize_list(semantic_ids),
Expand Down Expand Up @@ -161,6 +165,8 @@ def _set_xlsx_styles(wb: Workbook, sheet: Worksheet) -> None:
"question": 370,
"explainer": 370,
"error": 370,
"min": 110,
"max": 110,
"answers": 370,
"scores": 110,
"semantic_ids": 110,
Expand Down
8 changes: 8 additions & 0 deletions home/import_assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ def create_shadow_assessment_from_row(
question = ShadowQuestionBlock(
question=row.question,
error=row.error,
min=row.min,
max=row.max,
answers=answers,
type=row.question_type,
explainer=row.explainer,
Expand All @@ -194,6 +196,8 @@ class ShadowQuestionBlock:
answers: list[ShadowAnswerBlock]
explainer: str = ""
error: str = ""
min: str = ""
max: str = ""
type: str = ""


Expand Down Expand Up @@ -275,6 +279,8 @@ def questions_as_streamfield(self) -> list[dict[str, Any]]:
"answers": answers,
"explainer": question.explainer,
"error": question.error,
"min": question.min,
"max": question.max,
},
}
)
Expand Down Expand Up @@ -302,6 +308,8 @@ class AssessmentRow:
question: str = ""
explainer: str = ""
error: str = ""
min: str = ""
max: str = ""
answers: list[str] = field(default_factory=list)
scores: list[float] = field(default_factory=list)
semantic_ids: list[str] = field(default_factory=list)
Expand Down
220 changes: 220 additions & 0 deletions home/migrations/0070_alter_assessment_questions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# Generated by Django 4.2.11 on 2024-06-20 06:39

from django.db import migrations
import wagtail.blocks
import wagtail.fields


class Migration(migrations.Migration):

dependencies = [
("home", "0069_alter_assessment_questions"),
]

operations = [
migrations.AlterField(
model_name="assessment",
name="questions",
field=wagtail.fields.StreamField(
[
(
"categorical_question",
wagtail.blocks.StructBlock(
[
(
"question",
wagtail.blocks.TextBlock(
help_text="The question to ask the user"
),
),
(
"explainer",
wagtail.blocks.TextBlock(
help_text="Explainer message which tells the user why we need this question",
required=False,
),
),
(
"error",
wagtail.blocks.TextBlock(
help_text="Error message for this question if we don't understand the input",
required=False,
),
),
(
"answers",
wagtail.blocks.ListBlock(
wagtail.blocks.StructBlock(
[
(
"answer",
wagtail.blocks.TextBlock(
help_text="The choice shown to the user for this option"
),
),
(
"score",
wagtail.blocks.FloatBlock(
help_text="How much to add to the total score if this answer is chosen"
),
),
(
"semantic_id",
wagtail.blocks.TextBlock(
help_text="Semantic ID for this answer"
),
),
]
)
),
),
]
),
),
(
"age_question",
wagtail.blocks.StructBlock(
[
(
"question",
wagtail.blocks.TextBlock(
help_text="The question to ask the user"
),
),
(
"explainer",
wagtail.blocks.TextBlock(
help_text="Explainer message which tells the user why we need this question",
required=False,
),
),
(
"error",
wagtail.blocks.TextBlock(
help_text="Error message for this question if we don't understand the input",
required=False,
),
),
]
),
),
(
"multiselect_question",
wagtail.blocks.StructBlock(
[
(
"question",
wagtail.blocks.TextBlock(
help_text="The question to ask the user"
),
),
(
"explainer",
wagtail.blocks.TextBlock(
help_text="Explainer message which tells the user why we need this question",
required=False,
),
),
(
"error",
wagtail.blocks.TextBlock(
help_text="Error message for this question if we don't understand the input",
required=False,
),
),
(
"answers",
wagtail.blocks.ListBlock(
wagtail.blocks.StructBlock(
[
(
"answer",
wagtail.blocks.TextBlock(
help_text="The choice shown to the user for this option"
),
),
(
"score",
wagtail.blocks.FloatBlock(
help_text="How much to add to the total score if this answer is chosen"
),
),
(
"semantic_id",
wagtail.blocks.TextBlock(
help_text="Semantic ID for this answer"
),
),
]
)
),
),
]
),
),
(
"freetext_question",
wagtail.blocks.StructBlock(
[
(
"question",
wagtail.blocks.TextBlock(
help_text="The question to ask the user"
),
),
(
"explainer",
wagtail.blocks.TextBlock(
help_text="Explainer message which tells the user why we need this question",
required=False,
),
),
]
),
),
(
"integer_question",
wagtail.blocks.StructBlock(
[
(
"question",
wagtail.blocks.TextBlock(
help_text="The question to ask the user"
),
),
(
"explainer",
wagtail.blocks.TextBlock(
help_text="Explainer message which tells the user why we need this question",
required=False,
),
),
(
"error",
wagtail.blocks.TextBlock(
help_text="Error message for this question if we don't understand the input",
required=False,
),
),
(
"min",
wagtail.blocks.IntegerBlock(
help_text="The minimum value that can be entered",
required=False,
),
),
(
"max",
wagtail.blocks.IntegerBlock(
help_text="The maximum value that can be entered",
required=False,
),
),
]
),
),
],
use_json_field=True,
),
),
]
13 changes: 13 additions & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,18 @@ class FreeTextQuestionBlock(BaseQuestionBlock):
error = None


class IntegerQuestionBlock(BaseQuestionBlock):
min = blocks.IntegerBlock(
required=False,
help_text="The minimum value that can be entered",
)
max = blocks.IntegerBlock(
required=False,
help_text="The maximum value that can be entered",
)
answers = None


class AssessmentTag(TaggedItemBase):
content_object = ParentalKey(
"Assessment", on_delete=models.CASCADE, related_name="tagged_items"
Expand Down Expand Up @@ -1396,6 +1408,7 @@ class Assessment(DraftStateMixin, RevisionMixin, index.Indexed, ClusterableModel
("age_question", AgeQuestionBlock()),
("multiselect_question", MultiselectQuestionBlock()),
("freetext_question", FreeTextQuestionBlock()),
("integer_question", IntegerQuestionBlock()),
],
use_json_field=True,
)
Expand Down
4 changes: 4 additions & 0 deletions home/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ class QuestionField(serializers.Field):
"question": "How much wood would a woodchuck chuck if a woodchuck could chuck wood?",
"explainer": None,
"error": "Unknown answer given",
"min": 100,
"max": 500,
"answers": [
{
"answer": "Yes",
Expand All @@ -567,6 +569,8 @@ def to_representation(self, page):
"question": question["value"]["question"],
"explainer": question.get("value", {}).get("explainer"),
"error": question.get("value", {}).get("error"),
"min": question.get("value", {}).get("min"),
"max": question.get("value", {}).get("max"),
"answers": [
x["value"] for x in question["value"].get("answers", [])
],
Expand Down
9 changes: 5 additions & 4 deletions home/tests/import-export-data/assessment_less_simple.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
title,question_type,tags,slug,version,locale,high_result_page,high_inflection,medium_result_page,medium_inflection,low_result_page,generic_error,question,explainer,error,answers,scores,semantic_ids
Health Assessment,categorical_question,checker,health-assessment,v1.0,en,high-inflection,4.0,medium-score,2.0,low-score,"Sorry, we didn't quite get that.",How often do you check your blood pressure,We need to know your blood pressure for a proper risk assessment,Sorry we don't understand.,"Once a week,Twice a week,Thrice a week","1.0,3.0,4.5","test-1,test-2,test-3"
Health Assessment,categorical_question,checker,health-assessment,v1.0,en,high-inflection,4.0,medium-score,2.0,low-score,"Sorry, we didn't quite get that.",Have you been vaccinated against COVID-19?,We need to know if you have been vaccinated for a proper risk assessment,Sorry we didn't quite catch that.,"Yes,No","3.0,1.0","test-1,test-2"
Health Assessment,categorical_question,checker,health-assessment,v1.0,en,high-inflection,4.0,medium-score,2.0,low-score,"Sorry, we didn't quite get that.",How high is your temperature?,We need to know your temperature for a proper risk assessment. A very high temperature could suggest an infection is present.,Please choose the option that matches your answer.,"Between 37.5C and 38C,Between 38.1C and 40C,Between 40.1C and 41.1C","3.0,2.0,1.0","test-1,test-2,test-3"
title,question_type,tags,slug,version,locale,high_result_page,high_inflection,medium_result_page,medium_inflection,low_result_page,generic_error,question,explainer,error,min,max,answers,scores,semantic_ids
Health Assessment,categorical_question,checker,health-assessment,v1.0,en,high-inflection,4.0,medium-score,2.0,low-score,"Sorry, we didn't quite get that.",How often do you check your blood pressure,We need to know your blood pressure for a proper risk assessment,Sorry we don't understand.,,,"Once a week,Twice a week,Thrice a week","1.0,3.0,4.5","test-1,test-2,test-3"
Health Assessment,categorical_question,checker,health-assessment,v1.0,en,high-inflection,4.0,medium-score,2.0,low-score,"Sorry, we didn't quite get that.",Have you been vaccinated against COVID-19?,We need to know if you have been vaccinated for a proper risk assessment,Sorry we didn't quite catch that.,,,"Yes,No","3.0,1.0","test-1,test-2"
Health Assessment,categorical_question,checker,health-assessment,v1.0,en,high-inflection,4.0,medium-score,2.0,low-score,"Sorry, we didn't quite get that.",How high is your temperature?,We need to know your temperature for a proper risk assessment. A very high temperature could suggest an infection is present.,Please choose the option that matches your answer.,,,"Between 37.5C and 38C,Between 38.1C and 40C,Between 40.1C and 41.1C","3.0,2.0,1.0","test-1,test-2,test-3"
Integer Type Question,integer_question,integer-type-question,integer-type-question,v1.0,en,high-inflection,5.0,medium-score,3.0,low-score,This is a generic error,How much do you weigh in kilograms?,We need to test integer type questions,Sorry your min and max weight should be between 40 and 500kg.,40,500,,,
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title,question_type,tags,slug,locale,high_result_page,high_inflection,medium_result_page,medium_inflection,low_result_page,generic_error,questions,error,answers,score,semantic_ids
Fake Test,categorical_question,"Random, selection",fake-test,en,fake-page,4,fake-page,3,fake-page,We did not quite get that,Select a number for the first question,Please retry,"A,B","4.0,1.0","test-1,test-2"
Fake Test,categorical_question,"Random, selection",fake-test,en,fake-page,4,fake-page,3,fake-page,We did not quite get that,Select a number for the second question,We did not quite get that,"A,B,C","1.0,3.0,4.0","test-1,test-2,test-3"
Fake Test,categorical_question,"Random, selection",fake-test,en,fake-page,4,fake-page,3,fake-page,We did not quite get that,Select a number for the third question,Sorry we did not quite get that,"A,B,C","2.0,1.0,2.0","test-1,test-2,test-3"
title,question_type,tags,slug,locale,high_result_page,high_inflection,medium_result_page,medium_inflection,low_result_page,generic_error,questions,error,min,max,answers,score,semantic_ids
Fake Test,categorical_question,"Random, selection",fake-test,en,fake-page,4,fake-page,3,fake-page,We did not quite get that,Select a number for the first question,Please retry,,,"A,B","4.0,1.0","test-1,test-2"
Fake Test,categorical_question,"Random, selection",fake-test,en,fake-page,4,fake-page,3,fake-page,We did not quite get that,Select a number for the second question,We did not quite get that,,,"A,B,C","1.0,3.0,4.0","test-1,test-2,test-3"
Fake Test,categorical_question,"Random, selection",fake-test,en,fake-page,4,fake-page,3,fake-page,We did not quite get that,Select a number for the third question,Sorry we did not quite get that,,,"A,B,C","2.0,1.0,2.0","test-1,test-2,test-3"
Loading

0 comments on commit 067b234

Please sign in to comment.