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

Feature/import living conditions question #12

Closed
wants to merge 4 commits into from
Closed
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
Binary file modified media/questions.xlsx
Binary file not shown.
6 changes: 3 additions & 3 deletions profiles/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ def other(self, obj):

def question_description(self, obj):
if obj.question:
return obj.question.question_en
return obj.question.question_fi
else:
return obj.sub_question.question.question_en
return obj.sub_question.question.question_fi

def sub_question_description(self, obj):
if obj.sub_question:
return obj.sub_question.description_en
return obj.sub_question.description_fi
return None

class Meta:
Expand Down
19 changes: 12 additions & 7 deletions profiles/management/commands/import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
OPTION_COLUMN = 9
RESULT_COLUMNS = [10, 11, 12, 13, 14, 15]
OTHER_STRING_CONTAINS = ["/Other", "Something else"]
SKIP_QUESTIONS = ["11", "12", "13", "14", "15", "17", "18", "19", "20"]


def get_root_dir() -> str:
Expand Down Expand Up @@ -174,19 +175,23 @@ def save_questions(excel_data: pd.DataFrame, results: list):
num_created = 0
questions_to_delete = list(Question.objects.all().values_list("id", flat=True))
options_to_delete = list(Option.objects.all().values_list("id", flat=True))
in_skipped_question = False

for index, row_data in excel_data.iterrows():
# The end of the questions sheet includes questions that will not be imported.
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():

if question_number in SKIP_QUESTIONS:
in_skipped_question = True
# Row containing the question starts with a digit
if question_number[0].isdigit() and question_number not in SKIP_QUESTIONS:
in_skipped_question = False

questions = get_language_dict(row_data[QUESTION_COLUMN])
descriptions = get_language_dict(row_data[QUESTION_DESCRIPTION_COLUMN])

number_of_options_to_choose = row_data[NUMBER_OF_OPTIONS_TO_CHOOSE]
if not number_of_options_to_choose:
number_of_options_to_choose = "1"
Expand Down Expand Up @@ -234,7 +239,8 @@ def save_questions(excel_data: pd.DataFrame, results: list):
sub_question_order_number = 0
option_order_number = 0
sub_question = None

elif in_skipped_question:
continue
# Create SubQuestion
if question and row_data[SUB_QUESTION_COLUMN]:
logger.info(f"created sub question {row_data[SUB_QUESTION_COLUMN]}")
Expand Down Expand Up @@ -283,7 +289,6 @@ def save_questions(excel_data: pd.DataFrame, results: list):
options_to_delete.remove(option.id)
Question.objects.filter(id__in=questions_to_delete).delete()
Option.objects.filter(id__in=options_to_delete).delete()

logger.info(f"Created {num_created} questions")


Expand Down
11 changes: 7 additions & 4 deletions profiles/tests/test_import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from django.core.management import call_command

from profiles.management.commands.import_questions import SKIP_QUESTIONS
from profiles.models import (
Option,
Question,
Expand Down Expand Up @@ -40,7 +41,7 @@ def test_import_questions():
assert results_qs[5].description_sv[-12:] == "använda bil."

# Test questions
assert Question.objects.count() == 16
assert Question.objects.count() == 17
# Test question without sub questions
question1b1 = Question.objects.get(number="1b1")
assert question1b1.question_fi == "Miksi et koskaan kulje autolla?"
Expand Down Expand Up @@ -113,15 +114,17 @@ def test_import_questions():
assert condition.option_conditions.count() == 4
assert condition.option_conditions.all()[0].value == "1"
assert condition.option_conditions.all()[3].value == "6-7"

assert Question.objects.get(number=16).options.count() == 5
for number in SKIP_QUESTIONS:
assert Question.objects.filter(number=number).count() == 0
# Test other options
other_options_qs = Option.objects.filter(is_other=True)
assert other_options_qs.count() == 12
assert other_options_qs.count() == 13
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() == 16
assert Question.objects.count() == 17
assert QuestionCondition.objects.all().count() == 9

assert question4.id == Question.objects.get(number="4").id
Expand Down
Loading