Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add semantic id #350

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!--
## Unreleased
- Add skip threshold and skip high result page
- Add semantic_id for questions
-->

## v1.2.0
Expand Down
11 changes: 7 additions & 4 deletions home/export_assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class ExportRow:
max: int
answers: str
scores: str
semantic_ids: str
answer_semantic_ids: str
question_semantic_id: str

@classmethod
def headings(cls) -> list[str]:
Expand Down Expand Up @@ -68,7 +69,7 @@ def perform_export(self) -> Iterable[ExportRow]:
for question in item.questions:
answers = [a["answer"] for a in question.value.get("answers", [])]
scores = [a["score"] for a in question.value.get("answers", [])]
semantic_ids = [
answer_semantic_ids = [
a["semantic_id"] for a in question.value.get("answers", [])
]
yield ExportRow(
Expand Down Expand Up @@ -97,7 +98,8 @@ def perform_export(self) -> Iterable[ExportRow]:
max=question.value.get("max"),
answers=serialize_list(answers),
scores=serialize_list(scores),
semantic_ids=serialize_list(semantic_ids),
answer_semantic_ids=serialize_list(answer_semantic_ids),
question_semantic_id=question.value.get("semantic_id"),
)


Expand Down Expand Up @@ -177,7 +179,8 @@ def _set_xlsx_styles(wb: Workbook, sheet: Worksheet) -> None:
"max": 110,
"answers": 370,
"scores": 110,
"semantic_ids": 110,
"answer_semantic_ids": 110,
"question_semantic_id": 110,
}
for column in sheet.iter_cols(max_row=1):
[cell] = column
Expand Down
12 changes: 9 additions & 3 deletions home/import_assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def create_shadow_assessment_from_row(
answers = [
ShadowAnswerBlock(answer=answer, score=score, semantic_id=semantic_id)
for (answer, score, semantic_id) in zip(
row.answers, row.scores, row.semantic_ids, strict=False
row.answers, row.scores, row.answer_semantic_ids, strict=False
)
]
question = ShadowQuestionBlock(
Expand All @@ -186,6 +186,7 @@ def create_shadow_assessment_from_row(
answers=answers,
type=row.question_type,
explainer=row.explainer,
semantic_id=row.question_semantic_id,
)
assessment.questions.append(question)

Expand All @@ -209,6 +210,7 @@ class ShadowQuestionBlock:
min: str = ""
max: str = ""
type: str = ""
semantic_id: str = ""


@dataclass(slots=True)
Expand Down Expand Up @@ -305,6 +307,7 @@ def questions_as_streamfield(self) -> list[dict[str, Any]]:
"error": question.error,
"min": question.min,
"max": question.max,
"semantic_id": question.semantic_id,
},
}
)
Expand Down Expand Up @@ -338,7 +341,8 @@ class AssessmentRow:
max: str = ""
answers: list[str] = field(default_factory=list)
scores: list[float] = field(default_factory=list)
semantic_ids: list[str] = field(default_factory=list)
answer_semantic_ids: list[str] = field(default_factory=list)
question_semantic_id: str = ""

@classmethod
def fields(cls) -> list[str]:
Expand All @@ -360,7 +364,9 @@ def from_flat(cls, row: dict[str, str]) -> "AssessmentRow":
tags=deserialise_list(row.pop("tags", "")),
answers=deserialise_list(row.pop("answers", "")),
scores=[float(i) for i in deserialise_list(row.pop("scores", ""))],
semantic_ids=deserialise_list(row.pop("semantic_ids", "")),
answer_semantic_ids=deserialise_list(
row.pop("answer_semantic_ids", "")
),
**row,
)
except TypeError:
Expand Down
283 changes: 283 additions & 0 deletions home/migrations/0080_alter_assessment_questions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
# Generated by Django 4.2.11 on 2024-07-31 11:44

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


class Migration(migrations.Migration):

dependencies = [
("home", "0079_assessment_skip_high_result_page_and_more"),
]

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,
),
),
(
"semantic_id",
wagtail.blocks.TextBlock(
help_text="Semantic ID for this question"
),
),
(
"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,
),
),
(
"semantic_id",
wagtail.blocks.TextBlock(
help_text="Semantic ID for this question"
),
),
]
),
),
(
"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,
),
),
(
"semantic_id",
wagtail.blocks.TextBlock(
help_text="Semantic ID for this question"
),
),
(
"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,
),
),
(
"semantic_id",
wagtail.blocks.TextBlock(
help_text="Semantic ID for this question"
),
),
]
),
),
(
"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,
),
),
(
"semantic_id",
wagtail.blocks.TextBlock(
help_text="Semantic ID for this question"
),
),
(
"min",
wagtail.blocks.IntegerBlock(
default=None,
help_text="The minimum value that can be entered",
),
),
(
"max",
wagtail.blocks.IntegerBlock(
default=None,
help_text="The maximum value that can be entered",
),
),
]
),
),
(
"year_of_birth_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,
),
),
(
"semantic_id",
wagtail.blocks.TextBlock(
help_text="Semantic ID for this question"
),
),
]
),
),
],
use_json_field=True,
),
),
]
1 change: 1 addition & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@ class BaseQuestionBlock(blocks.StructBlock):
required=False,
help_text="Error message for this question if we don't understand the input",
)
semantic_id = blocks.TextBlock(help_text="Semantic ID for this question")


class CategoricalQuestionBlock(BaseQuestionBlock):
Expand Down
Loading
Loading