Skip to content

Commit

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

Feature/postal code result blur count
  • Loading branch information
juuso-j authored Mar 21, 2024
2 parents 6f3db10 + ae872e4 commit 8fdd275
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions profiles/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
SubQuestionCondition,
)

from .utils import blur_count


class ResultSerializer(serializers.ModelSerializer):
class Meta:
Expand Down Expand Up @@ -131,6 +133,11 @@ class Meta:
"count",
]

def to_representation(self, instance):
representation = super().to_representation(instance)
representation["count"] = blur_count(instance.count)
return representation


class PostalCodeSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
11 changes: 11 additions & 0 deletions profiles/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
from profiles.models import PostalCodeResult


def blur_count(count, threshold=5):
"""
Returns a blurred count, which is supposed to hide individual
postal code results.
"""
if count <= threshold:
return 0
else:
return count


class CustomValidationError(ValidationError):
# The detail field is shown also when DEBUG=False
# Ensures the error message is displayed to the user
Expand Down
15 changes: 15 additions & 0 deletions profiles/tests/api/test_postal_code_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
ANSWER_URL = reverse("profiles:answer-list")


@pytest.mark.django_db
def test_blur_count(api_client, results):
postal_code_result = PostalCodeResult.objects.create(
count=5, result=results.first()
)
url = reverse("profiles:postalcoderesult-detail", args=[str(postal_code_result.id)])
response = api_client.get(url)
assert response.status_code == 200
assert response.json()["count"] == 0
postal_code_result.count = 6
postal_code_result.save()
response = api_client.get(url)
assert response.json()["count"] == 6


@pytest.mark.django_db
def test_postal_code_result_with_postal_code_and_optional_postal_code(
api_client, users, questions, options, results
Expand Down

0 comments on commit 8fdd275

Please sign in to comment.