Skip to content

Commit

Permalink
WIP add result_set to ballot serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed Sep 8, 2023
1 parent 2ba77e7 commit 7b7cb05
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ynr/apps/elections/api/next/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
MinimalPostSerializer,
)
from rest_framework import serializers
from uk_results import models as uk_results_models
from uk_results.api.next.serializers import MinimalResultSerializer


class MinimalElectionSerializer(serializers.HyperlinkedModelSerializer):
Expand Down Expand Up @@ -102,6 +104,7 @@ class Meta:
"replaces",
"replaced_by",
"uncontested",
"results",
)

replaces = serializers.SlugRelatedField(
Expand All @@ -126,6 +129,7 @@ class Meta:
post = MinimalPostSerializer(read_only=True)
sopn = serializers.SerializerMethodField()
candidacies = serializers.SerializerMethodField()
results = MinimalResultSerializer(read_only=True, source="resultset")

results_url = serializers.HyperlinkedIdentityField(
view_name="resultset-detail",
Expand Down Expand Up @@ -182,3 +186,15 @@ def get_cancelled(self, instance):
if instance.cancelled:
return True
return instance.uncontested

@swagger_serializer_method(serializer_or_field=MinimalResultSerializer)
def get_result_set(self, instance):
"""
If the ballot has a result set, return it
"""
try:
return MinimalResultSerializer(
instance.result_set, read_only=True
).data
except uk_results_models.MinimalResultsSerializer.DoesNotExist:
return None
12 changes: 12 additions & 0 deletions ynr/apps/elections/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ def test_when_uncontested_and_cancelled(self):
serializer = BallotSerializer(instance=ballot)
cancelled = serializer.get_cancelled(instance=ballot)
self.assertTrue(cancelled)

def test_get_results_set_when_none(self):
ballot = BallotPaperFactory()
serializer = BallotSerializer(instance=ballot)
result_set = serializer.get_result_set(instance=ballot)
self.assertIsNone(result_set)

def test_get_results_set_with_results(self):
ballot = BallotPaperFactory()
serializer = BallotSerializer(instance=ballot)
results_set = serializer.get_result_set(instance=ballot)
self.assertIsNotNone(results_set)
12 changes: 12 additions & 0 deletions ynr/apps/uk_results/api/next/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ def get_url(self, obj):
candidate_results = CandidateResultSerializer(many=True)


class MinimalResultSerializer(serializers.ModelSerializer):
class Meta:
model = ResultSet
fields = (
"num_turnout_reported",
"turnout_percentage",
"num_spoilt_ballots",
"source",
"total_electorate",
)


class ElectedSerializer(CandidacyOnBallotSerializer):
class Meta:
model = Membership
Expand Down

0 comments on commit 7b7cb05

Please sign in to comment.