Skip to content

Commit

Permalink
Merge pull request #29 from City-of-Turku/feature/improve-postal-code…
Browse files Browse the repository at this point in the history
…-result

Feature/improve postal code result
  • Loading branch information
juuso-j authored Mar 6, 2024
2 parents ff832f0 + 73f3801 commit f795d93
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 190 deletions.
1 change: 0 additions & 1 deletion mpbackend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"drf_spectacular",
"corsheaders",
"django_filters",
"memoize",
]

MIDDLEWARE = [
Expand Down
33 changes: 20 additions & 13 deletions profiles/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,28 @@ def update_postal_code_result(user):
return
postal_code = None
postal_code_type = None
if user.profile.postal_code:
postal_code, _ = PostalCode.objects.get_or_create(
postal_code=user.profile.postal_code
)
postal_code_type, _ = PostalCodeType.objects.get_or_create(
type_name=PostalCodeType.HOME_POSTAL_CODE
)
if user.profile.optional_postal_code:
postal_code, _ = PostalCode.objects.get_or_create(
postal_code=user.profile.optional_postal_code
)
postal_code_type, _ = PostalCodeType.objects.get_or_create(
type_name=PostalCodeType.OPTIONAL_POSTAL_CODE
postal_code, _ = PostalCode.objects.get_or_create(
postal_code=user.profile.postal_code
)
postal_code_type, _ = PostalCodeType.objects.get_or_create(
type_name=PostalCodeType.HOME_POSTAL_CODE
)
try:
postal_code_result, _ = PostalCodeResult.objects.get_or_create(
postal_code=postal_code, postal_code_type=postal_code_type, result=result
)
except IntegrityError as e:
logger.error(f"IntegrityError while creating PostalCodeResult: {e}")
return
postal_code_result.count += 1
postal_code_result.save()

postal_code, _ = PostalCode.objects.get_or_create(
postal_code=user.profile.optional_postal_code
)
postal_code_type, _ = PostalCodeType.objects.get_or_create(
type_name=PostalCodeType.OPTIONAL_POSTAL_CODE
)
try:
postal_code_result, _ = PostalCodeResult.objects.get_or_create(
postal_code=postal_code, postal_code_type=postal_code_type, result=result
Expand Down
8 changes: 8 additions & 0 deletions profiles/management/commands/import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ def get_and_create_results(data: pd.DataFrame) -> list:
return results


@db.transaction.atomic
def update_results_num_options():
for result in Result.objects.all():
result.num_options = Option.objects.filter(results=result).count()
result.save()


@db.transaction.atomic
def create_sub_question_condition(row_data: str, sub_question: SubQuestion):
question_number, option_order_number = row_data.split(".")
Expand Down Expand Up @@ -305,3 +312,4 @@ def handle(self, *args, **options):
excel_data = excel_data.fillna("").replace([""], [None])
results = get_and_create_results(excel_data)
save_questions(excel_data, results)
update_results_num_options()
17 changes: 17 additions & 0 deletions profiles/migrations/0021_result_num_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2024-03-05 07:50

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("profiles", "0020_postalcode_postalcodetype_add_ordering"),
]

operations = [
migrations.AddField(
model_name="result",
name="num_options",
field=models.PositiveSmallIntegerField(null=True),
),
]
2 changes: 2 additions & 0 deletions profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Result(models.Model):
topic = models.CharField(max_length=64, null=True)
description = models.TextField(null=True)
value = models.CharField(max_length=64, null=True)
num_options = models.PositiveSmallIntegerField(null=True)

class Meta:
ordering = ["id"]
Expand Down Expand Up @@ -96,6 +97,7 @@ class Meta:

class AnswerOther(Answer):
# Proxy model that allows registerin Answer model twice to the Admin

class Meta:
proxy = True

Expand Down
11 changes: 2 additions & 9 deletions profiles/tests/api/test_answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from account.models import User
from profiles.models import Answer, Option, Question, SubQuestion
from profiles.tests.utils import delete_memoized_functions_cache


def test_answer_post_unauthenticated(api_client):
Expand All @@ -14,16 +13,15 @@ def test_answer_post_unauthenticated(api_client):


@pytest.mark.django_db
def test_poll_start(api_client):
def test_start_poll(api_client):
User.objects.all().count() == 0
url = reverse("profiles:question-start-poll")
response = api_client.post(url)
assert response.status_code == 200
User.objects.all().count() == 1
assert User.objects.all().count() == 1


@pytest.mark.django_db
@delete_memoized_functions_cache
def test_post_answer(api_client_authenticated, users, questions, options):
user = users.get(username="test1")
assert Answer.objects.count() == 0
Expand All @@ -40,7 +38,6 @@ def test_post_answer(api_client_authenticated, users, questions, options):


@pytest.mark.django_db
@delete_memoized_functions_cache
def test_post_answer_with_other_option(api_client, users, answers, questions, options):
user = users.get(username="no answers user")
token = Token.objects.create(user=user)
Expand All @@ -66,7 +63,6 @@ def test_post_answer_with_other_option(api_client, users, answers, questions, op


@pytest.mark.django_db
@delete_memoized_functions_cache
def test_post_answer_answer_is_updated(
api_client_authenticated, users, answers, questions, options
):
Expand Down Expand Up @@ -129,7 +125,6 @@ def test_post_answer_where_question_not_related_to_option(


@pytest.mark.django_db
@delete_memoized_functions_cache
def test_answer_get_result(api_client_authenticated, users, answers):
url = reverse("profiles:answer-get-result")
response = api_client_authenticated.get(url)
Expand All @@ -138,7 +133,6 @@ def test_answer_get_result(api_client_authenticated, users, answers):


@pytest.mark.django_db
@delete_memoized_functions_cache
def test_post_answer_where_condition_not_met(
api_client,
users,
Expand All @@ -163,7 +157,6 @@ def test_post_answer_where_condition_not_met(


@pytest.mark.django_db
@delete_memoized_functions_cache
def test_post_answer_where_condition_is_met(
api_client,
users,
Expand Down
Loading

0 comments on commit f795d93

Please sign in to comment.