diff --git a/media/questions.xlsx b/media/questions.xlsx index 81b6d3c..12db33d 100644 Binary files a/media/questions.xlsx and b/media/questions.xlsx differ diff --git a/profiles/admin.py b/profiles/admin.py index 016ec3b..1918f79 100644 --- a/profiles/admin.py +++ b/profiles/admin.py @@ -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: diff --git a/profiles/management/commands/import_questions.py b/profiles/management/commands/import_questions.py index 7b8100f..5f5acd4 100644 --- a/profiles/management/commands/import_questions.py +++ b/profiles/management/commands/import_questions.py @@ -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: @@ -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" @@ -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]}") @@ -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") diff --git a/profiles/tests/test_import_questions.py b/profiles/tests/test_import_questions.py index 7b7e43d..105b409 100644 --- a/profiles/tests/test_import_questions.py +++ b/profiles/tests/test_import_questions.py @@ -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, @@ -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?" @@ -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