Skip to content

Commit

Permalink
Fix bug, question created insted of updated
Browse files Browse the repository at this point in the history
  • Loading branch information
juuso-j committed Feb 8, 2024
1 parent ce16831 commit 63982d9
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions profiles/management/commands/import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,30 @@ 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", ""),
}
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 +264,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

0 comments on commit 63982d9

Please sign in to comment.