Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ballot results to ballot serializer #2174

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions ynr/apps/api/tests/test_api_next.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from parties.tests.fixtures import DefaultPartyFixtures
from people.models import PersonImage
from people.tests.factories import PersonFactory
from uk_results.models import ResultSet


class TestAPI(
Expand Down Expand Up @@ -287,3 +288,42 @@ def test_sopn_on_ballot(self):
"source_url": "",
},
)

def test_no_results_on_ballot(self):
response = self.app.get(
"/api/next/ballots/{}/".format(
self.edinburgh_east_post_ballot.ballot_paper_id
)
)
result = response.json
self.assertEqual(
result["results"],
None,
)

def test_results_on_ballot(self):
ResultSet.objects.create(
ballot=self.edinburgh_east_post_ballot,
created="2015-05-08T00:00:00Z",
modified="2015-05-08T00:00:00Z",
source="Example ResultSet for testing",
num_spoilt_ballots=100,
num_turnout_reported=900,
total_electorate=1000,
)
response = self.app.get(
"/api/next/ballots/{}/".format(
self.edinburgh_east_post_ballot.ballot_paper_id
)
)
result = response.json
self.assertEqual(
result["results"],
{
"num_turnout_reported": 900,
"turnout_percentage": 90.0,
"num_spoilt_ballots": 100,
"source": "Example ResultSet for testing",
"total_electorate": 1000,
},
)
2 changes: 1 addition & 1 deletion ynr/apps/elections/api/next/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BallotViewSet(viewsets.ReadOnlyModelViewSet):
lookup_value_regex = r"(?!\.json$)[^/]+"
queryset = (
extra_models.Ballot.objects.select_related(
"election", "post", "replaces", "replaced_by"
"election", "post", "replaces", "replaced_by", "resultset"
)
.prefetch_related(
Prefetch(
Expand Down
3 changes: 3 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,7 @@
MinimalPostSerializer,
)
from rest_framework import serializers
from uk_results.api.next.serializers import MinimalResultSerializer


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

replaces = serializers.SlugRelatedField(
Expand All @@ -126,6 +128,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
2 changes: 1 addition & 1 deletion ynr/apps/elections/tests/test_ballot_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_ballot_with_candidates_no_sopn(self):
self.assertEqual(response.context["candidates"].count(), 9)
self.assertDataTimelineCandidateAddingInProgress(response)
self.assertInHTML(
"<h1>Candidates for Bar Ward on <br>7 September 2023</h1>",
f"<h1>Candidates for Bar Ward on <br>{ self.election.election_date.strftime('%d %B %Y') }</h1>",
response.text,
)
self.assertInHTML(
Expand Down
1 change: 1 addition & 0 deletions ynr/apps/official_documents/api/next/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ class OfficialDocumentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = OfficialDocument
fields = ("document_type", "uploaded_file", "source_url")
ref_name = None # Tells swagger that this is always embedded
13 changes: 13 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,19 @@ 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",
)
ref_name = None # Tells swagger that this is always embedded


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