Skip to content

Commit

Permalink
Merge pull request #14 from City-of-Turku/feature/result-model-remove…
Browse files Browse the repository at this point in the history
…-value-field

Feature/result model remove value field
  • Loading branch information
juuso-j authored Feb 14, 2024
2 parents 4bf5153 + 0023a1c commit 183c30f
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 25 deletions.
Binary file modified media/questions.xlsx
Binary file not shown.
17 changes: 9 additions & 8 deletions profiles/management/commands/import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
),
]
3 changes: 1 addition & 2 deletions profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions profiles/tests/api/test_answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions profiles/tests/api/test_postal_code_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions profiles/tests/api/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down
8 changes: 4 additions & 4 deletions profiles/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()


Expand Down
7 changes: 3 additions & 4 deletions profiles/tests/test_import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion profiles/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class OptionTranslationOptions(TranslationOptions):


class ResultTranslationOptions(TranslationOptions):
fields = ("topic", "value", "description")
fields = ("topic", "description")


translator.register(Result, ResultTranslationOptions)

0 comments on commit 183c30f

Please sign in to comment.