diff --git a/media/questions.xlsx b/media/questions.xlsx index 81b6d3c..8674da8 100644 Binary files a/media/questions.xlsx and b/media/questions.xlsx differ diff --git a/profiles/management/commands/import_questions.py b/profiles/management/commands/import_questions.py index 7b8100f..7d7aada 100644 --- a/profiles/management/commands/import_questions.py +++ b/profiles/management/commands/import_questions.py @@ -91,13 +91,13 @@ def get_and_create_results(data: pd.DataFrame) -> list: for i, column in enumerate(columns): col_data = data[column] topic = get_language_dict(data.columns[RESULT_COLUMNS[0] + i]) - value = get_language_dict(col_data[1]) description = get_language_dict(col_data[0]) - filter = {} + filter = {"topic": topic["fi"]} + update_filter = {} + for lang in LANGUAGES: - filter[f"topic_{lang}"] = topic[lang] - filter[f"value_{lang}"] = value[lang] - filter[f"description_{lang}"] = description[lang] + update_filter[f"topic_{lang}"] = topic[lang] + update_filter[f"description_{lang}"] = description[lang] queryset = Result.objects.filter(**filter) if queryset.count() == 0: result = Result.objects.create(**filter) @@ -109,6 +109,8 @@ def get_and_create_results(data: pd.DataFrame) -> list: if id in results_to_delete: results_to_delete.remove(id) + Result.objects.filter(**filter).update(**update_filter) + results.append(result) Result.objects.filter(id__in=results_to_delete).delete() logger.info(f"Created {num_created} Results") @@ -218,17 +220,16 @@ def save_questions(excel_data: pd.DataFrame, results: list): 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: questions_to_delete.remove(id) + + Question.objects.filter(**filter).update(**update_filter) if row_data[CONDITION_COLUMN]: create_conditions(row_data[CONDITION_COLUMN], question) sub_question_order_number = 0 diff --git a/profiles/migrations/0014_alter_option_options_remove_result_value_and_more.py b/profiles/migrations/0014_alter_option_options_remove_result_value_and_more.py new file mode 100644 index 0000000..8c22c7e --- /dev/null +++ b/profiles/migrations/0014_alter_option_options_remove_result_value_and_more.py @@ -0,0 +1,33 @@ +# Generated by Django 4.1.10 on 2024-02-14 07:21 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("profiles", "0013_answerother"), + ] + + operations = [ + migrations.AlterModelOptions( + name="option", + options={"ordering": ["order_number"]}, + ), + migrations.RemoveField( + model_name="result", + name="value", + ), + migrations.RemoveField( + model_name="result", + name="value_en", + ), + migrations.RemoveField( + model_name="result", + name="value_fi", + ), + migrations.RemoveField( + model_name="result", + name="value_sv", + ), + ] diff --git a/profiles/models.py b/profiles/models.py index 715fd5c..3742079 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -58,14 +58,13 @@ def __str__(self): class Result(models.Model): topic = models.CharField(max_length=64, null=True) - value = models.CharField(max_length=64, null=True) description = models.TextField(null=True) class Meta: ordering = ["id"] def __str__(self): - return f"{self.topic} / {self.value}" + return f"{self.topic}" class Answer(models.Model): diff --git a/profiles/tests/api/test_answer.py b/profiles/tests/api/test_answer.py index 764457a..3ab1460 100644 --- a/profiles/tests/api/test_answer.py +++ b/profiles/tests/api/test_answer.py @@ -34,7 +34,7 @@ def test_post_answer(api_client_authenticated, users, questions, options): assert response.status_code == 201 assert Answer.objects.count() == 1 user.refresh_from_db() - assert user.result.value == "negative result" + assert user.result.topic == "negative" @pytest.mark.django_db @@ -129,7 +129,7 @@ def test_answer_get_result(api_client_authenticated, users, answers): url = reverse("profiles:answer-get-result") response = api_client_authenticated.get(url) assert response.status_code == 200 - assert response.json()["value"] == "negative result" + assert response.json()["topic"] == "negative" @pytest.mark.django_db diff --git a/profiles/tests/api/test_postal_code_result.py b/profiles/tests/api/test_postal_code_result.py index 4d5b195..3db622c 100644 --- a/profiles/tests/api/test_postal_code_result.py +++ b/profiles/tests/api/test_postal_code_result.py @@ -19,8 +19,8 @@ def test_postal_code_result(api_client, questions, sub_questions, options, resul start_poll_url = reverse("profiles:question-start-poll") end_poll_url = reverse("profiles:question-end-poll") answer_url = reverse("profiles:answer-list") - positive_result = results.get(value="positive result") - negative_result = results.get(value="negative result") + positive_result = results.get(topic="positive") + negative_result = results.get(topic="negative") question1 = questions.get(number="1") question2 = questions.get(number="2") car_sub_q = sub_questions.get(question=question2, description="car") diff --git a/profiles/tests/api/test_question.py b/profiles/tests/api/test_question.py index 4fe56dc..acf9078 100644 --- a/profiles/tests/api/test_question.py +++ b/profiles/tests/api/test_question.py @@ -287,8 +287,8 @@ def test_questions(api_client, questions, question_conditions, options, results) == question.options.first().results.count() ) assert ( - json_response["options"][0]["results"][0]["value"] - == results.filter(options__question=question).first().value + json_response["options"][0]["results"][0]["topic"] + == results.filter(options__question=question).first().topic ) diff --git a/profiles/tests/conftest.py b/profiles/tests/conftest.py index d276249..6e4ff7a 100644 --- a/profiles/tests/conftest.py +++ b/profiles/tests/conftest.py @@ -63,8 +63,8 @@ def sub_questions(questions): @pytest.mark.django_db @pytest.fixture def options(questions, sub_questions, results): - positive_result = results.get(value="positive result") - negative_result = results.get(value="negative result") + positive_result = results.get(topic="positive") + negative_result = results.get(topic="negative") question1 = questions.get(number="1") option_no = Option.objects.create(value="no", question=question1) option_no.results.add(negative_result) @@ -93,8 +93,8 @@ def options(questions, sub_questions, results): @pytest.mark.django_db @pytest.fixture def results(): - Result.objects.create(topic="negative", value="negative result") - Result.objects.create(topic="positive", value="positive result") + Result.objects.create(topic="negative", description="negative result") + Result.objects.create(topic="positive", description="positive result") return Result.objects.all() diff --git a/profiles/tests/test_import_questions.py b/profiles/tests/test_import_questions.py index 7b7e43d..3b2f5e9 100644 --- a/profiles/tests/test_import_questions.py +++ b/profiles/tests/test_import_questions.py @@ -33,11 +33,10 @@ def test_import_questions(): assert results_qs.count() == 6 autoilija = Result.objects.get(topic_fi="Autoilija") assert results_qs[0].topic_fi == autoilija.topic_fi - assert results_qs[1].value_sv == "Rutinerad Räv" - assert results_qs[2].description_en[0:15] == "Travels by foot" + assert "You know where" in results_qs[1].description_en + assert "Fotgängare" in results_qs[2].topic_sv assert results_qs[3].topic_en == "Public transport passenger" - assert results_qs[4].value_fi == "Kokeileva Kauris" - assert results_qs[5].description_sv[-12:] == "använda bil." + assert "Maas" in results_qs[4].topic_fi # Test questions assert Question.objects.count() == 16 diff --git a/profiles/translation.py b/profiles/translation.py index 711cff0..f1983aa 100644 --- a/profiles/translation.py +++ b/profiles/translation.py @@ -31,7 +31,7 @@ class OptionTranslationOptions(TranslationOptions): class ResultTranslationOptions(TranslationOptions): - fields = ("topic", "value", "description") + fields = ("topic", "description") translator.register(Result, ResultTranslationOptions)