Skip to content

Commit

Permalink
Merge pull request #64 from City-of-Turku/develop
Browse files Browse the repository at this point in the history
Production update
  • Loading branch information
juuso-j committed May 16, 2024
2 parents 006f039 + 6fa87d4 commit 03e610c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
13 changes: 8 additions & 5 deletions profiles/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ class Meta:


class PostalCodeResultSerializer(serializers.ModelSerializer):
result_topic_en = serializers.CharField(source="result.topic_en", read_only=True)
postal_code_string = serializers.CharField(
source="postal_code.postal_code", read_only=True
)
Expand All @@ -130,12 +129,16 @@ class Meta:
"postal_code_type",
"postal_code_type_string",
"result",
"result_topic_en",
"count",
]

def to_representation(self, instance):
representation = super().to_representation(instance)
results_topics = {
"fi": instance.result.value_fi,
"sv": instance.result.value_sv,
"en": instance.result.value_en,
}
representation["result_topics"] = results_topics
representation["count"] = blur_count(instance.count)
return representation

Expand All @@ -160,7 +163,7 @@ class Meta:
def to_representation(self, instance):
type_name = self.context.get("type_name")
representation = super().to_representation(instance)
representation["sum_of_count"] = instance.get_sum_of_count(
postal_code_type_name=type_name
representation["sum_of_count"] = blur_count(
instance.get_sum_of_count(postal_code_type_name=type_name)
)
return representation
3 changes: 2 additions & 1 deletion profiles/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def blur_count(count, threshold=5):
Returns a blurred count, which is supposed to hide individual
postal code results.
"""
if count <= threshold:

if not count or count <= threshold:
return 0
else:
return count
Expand Down
2 changes: 2 additions & 0 deletions profiles/tests/api/test_postal_code_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,5 @@ def test_cumulative_results(
json_data = response.json()
assert json_data["count"] == results.count()
assert json_data["results"][0]["sum_of_count"] == 6
# Test that count is blurred
assert json_data["results"][1]["sum_of_count"] == 0
6 changes: 6 additions & 0 deletions profiles/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def postal_code_results(postal_codes, postal_code_types, results):
result=results.first(),
count=2,
)
PostalCodeResult.objects.create(
postal_code=postal_codes.last(),
postal_code_type=postal_code_types.first(),
result=results.last(),
count=0,
)
return PostalCodeResult.objects.all()


Expand Down

0 comments on commit 03e610c

Please sign in to comment.