Skip to content

Commit

Permalink
Merge pull request #6 from City-of-Turku/feature/update-questions-bas…
Browse files Browse the repository at this point in the history
…ed-on-feedback

Feature/update questions based on feedback
  • Loading branch information
juuso-j authored Feb 8, 2024
2 parents 550e63e + 8444c7d commit b096413
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
Binary file modified media/questions.xlsx
Binary file not shown.
34 changes: 27 additions & 7 deletions profiles/management/commands/import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
IS_ANIMAL = 1

LANGUAGES = [language[0] for language in settings.LANGUAGES]
LANGUAGE_SEPARATOR = "/"
LANGUAGE_SEPARATOR = "//"
QUESTION_NUMBER_COLUMN = 0
QUESTION_COLUMN = 1
NUMBER_OF_OPTIONS_TO_CHOOSE = 2
Expand All @@ -30,9 +30,9 @@
MANDATORY_NUMBER_OF_SUB_QUESTIONS_TO_ANSWER_COLUMN = 6
SUB_QUESTION_DESCRIPTION_COLUMN = 7
SUB_QUESTION_CONDITION_COLUMN = 8

OPTION_COLUMN = 9
RESULT_COLUMNS = [10, 11, 12, 13, 14, 15]
OTHER_STRING_CONTAINS = ["/Other", "Something else"]


def get_root_dir() -> str:
Expand All @@ -45,6 +45,13 @@ def get_root_dir() -> str:
return settings.BASE_DIR


def is_any_substring_in_string(substrings: list, target_string: str):
for substring in substrings:
if substring in target_string:
return True
return False


def save_translated_field(obj: Any, field_name: str, data: dict):
"""
Sets the value of all languages for given field_name.
Expand Down Expand Up @@ -168,13 +175,13 @@ def save_questions(excel_data: pd.DataFrame, results: list):
questions_to_delete = list(Question.objects.all().values_list("id", flat=True))
for index, row_data in excel_data.iterrows():
# The end of the questions sheet includes questions that will not be imported.
if index > 214:
if index > 201:
break
try:
question_number = str(row_data[QUESTION_NUMBER_COLUMN])
except TypeError:
continue

# Rows containg the question starts with a digit
if question_number[0].isdigit():
questions = get_language_dict(row_data[QUESTION_COLUMN])
descriptions = get_language_dict(row_data[QUESTION_DESCRIPTION_COLUMN])
Expand All @@ -187,24 +194,35 @@ def save_questions(excel_data: pd.DataFrame, results: list):
]
if not mandatory_number_of_sub_questions_to_answer:
mandatory_number_of_sub_questions_to_answer = "*"

filter = {
"number": question_number,
}

update_filter = {
"number_of_options_to_choose": str(number_of_options_to_choose),
"mandatory_number_of_sub_questions_to_answer": str(
mandatory_number_of_sub_questions_to_answer
).replace(".0", ""),
}
update_filter["question"] = questions["fi"]
update_filter["description"] = descriptions["fi"]

for lang in LANGUAGES:
filter[f"question_{lang}"] = questions[lang]
filter[f"description_{lang}"] = descriptions[lang]
update_filter[f"question_{lang}"] = questions[lang]
update_filter[f"description_{lang}"] = descriptions[lang]

queryset = Question.objects.filter(**filter)

if queryset.count() == 0:
question = Question.objects.create(**filter)
Question.objects.filter(**filter).update(**update_filter)
logger.info(f"Created question: {questions['fi']}")
num_created += 1
else:
logger.info(f"Found question: {questions['fi']}")
Question.objects.filter(**filter).update(**update_filter)

question = queryset.first()
id = queryset.first().id
if id in questions_to_delete:
Expand Down Expand Up @@ -251,7 +269,9 @@ def save_questions(excel_data: pd.DataFrame, results: list):
option, _ = Option.objects.get_or_create(
question=question, order_number=option_order_number
)

if is_any_substring_in_string(OTHER_STRING_CONTAINS, str(val_str)):
option.is_other = True
option.save()
option_order_number += 1
save_translated_field(option, "value", get_language_dict(val_str))
for a_i, a_c in enumerate(RESULT_COLUMNS):
Expand Down
57 changes: 32 additions & 25 deletions profiles/tests/test_import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_import_questions():
assert results_qs[5].description_sv[-12:] == "använda bil."

# Test questions
assert Question.objects.count() == 20
assert Question.objects.count() == 16
# Test question without sub questions
question1b1 = Question.objects.get(number="1b1")
assert question1b1.question_fi == "Miksi et koskaan kulje autolla?"
Expand All @@ -54,8 +54,8 @@ def test_import_questions():
assert sub_q_qs.count() == 8
sq_auto = sub_q_qs.get(order_number=0)
assert sq_auto.description_en == "Car"
assert Option.objects.filter(sub_question=sq_auto).count() == 6
sq_auto_4_5 = Option.objects.get(sub_question=sq_auto, order_number=4)
assert Option.objects.filter(sub_question=sq_auto).count() == 5
sq_auto_4_5 = Option.objects.get(sub_question=sq_auto, order_number=3)
assert sq_auto_4_5.value == "4-5"
sq_auto_4_5_results = sq_auto_4_5.results.all()
assert sq_auto_4_5_results.count() == 2
Expand All @@ -64,8 +64,8 @@ def test_import_questions():

sq_walk = sub_q_qs.get(order_number=6)
sq_walk.description_sv == "Gående"
assert Option.objects.filter(sub_question=sq_walk).count() == 6
assert Option.objects.get(sub_question=sq_walk, order_number=2).value == "1"
assert Option.objects.filter(sub_question=sq_walk).count() == 5
assert Option.objects.get(sub_question=sq_walk, order_number=1).value == "1"
question1d = Question.objects.get(number="1d")
assert question1d.num_sub_questions == 0
assert Option.objects.filter(question=question1d).count() == 3
Expand Down Expand Up @@ -94,27 +94,35 @@ def test_import_questions():
)
assert sub_question_condition.option == Option.objects.get(value_fi="Linja-autolla")

question14 = Question.objects.get(number="14")
assert question14.options.count() == 2
assert (
question14.options.all().order_by("id")[1].value_en == "most comfortable route"
)
question8 = Question.objects.get(number="8")
assert question8.options.count() == 5
assert question8.options.all().order_by("id")[4].value_en == "Other:"
# Test question condition
assert QuestionCondition.objects.all().count() == 13
conditions = QuestionCondition.objects.filter(question=question14)
assert conditions.count() == 2
sq_train = SubQuestion.objects.get(question=question1, order_number=2)
condition = conditions.get(
question_condition=question1, sub_question_condition=sq_train
assert QuestionCondition.objects.all().count() == 9
conditions = QuestionCondition.objects.filter(question=question8)
assert conditions.count() == 1
condition_qs = QuestionCondition.objects.filter(
question_condition=question1, sub_question_condition=sq_auto
)
assert condition_qs.count() == 3
condition = QuestionCondition.objects.get(
question=Question.objects.get(number="1a"),
question_condition=question1,
sub_question_condition=sq_auto,
)
assert condition.option_conditions.count() == 5
assert condition.option_conditions.all()[0].value == "<1"
assert condition.option_conditions.all()[4].value == "6-7"
assert condition.option_conditions.count() == 4
assert condition.option_conditions.all()[0].value == "1"
assert condition.option_conditions.all()[3].value == "6-7"

# Test other options
other_options_qs = Option.objects.filter(is_other=True)
assert other_options_qs.count() == 11
assert other_options_qs.first().question == Question.objects.get(number="1a")
# Test that rows are preserved and duplicates are not generated
import_command()
assert Result.objects.count() == 6
assert Question.objects.count() == 20
assert QuestionCondition.objects.all().count() == 13
assert Question.objects.count() == 16
assert QuestionCondition.objects.all().count() == 9

assert question4.id == Question.objects.get(number="4").id
new_joukkoliikenne = SubQuestion.objects.get(question=question4, order_number=2)
Expand All @@ -125,13 +133,12 @@ def test_import_questions():
assert option_saa_results[0].id == new_option_saa_results[0].id
assert option_saa_results[1].id == new_option_saa_results[1].id
assert option_saa_results[2].id == new_option_saa_results[2].id
assert question14.id == Question.objects.get(number="14").id
assert question8.id == Question.objects.get(number="8").id
assert autoilija.id == Result.objects.get(topic_fi="Autoilija").id
question8 = Question.objects.get(number="8")

# Test that rows with info of Category 2 is skipped
question8 = Question.objects.get(number="8")
assert Option.objects.filter(question=question8).count() == 5
condition = QuestionCondition.objects.get(question=question8)
assert condition.question_condition == Question.objects.get(number="7")
assert condition.option_conditions.all()[0].value_fi == "Ei"
question10 = Question.objects.get(number="10")
assert question10.options.count() == 5

0 comments on commit b096413

Please sign in to comment.