From 9057482d5727de007d232b3c136af99cda346848 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Thu, 7 Sep 2023 12:03:35 +0100 Subject: [PATCH 1/4] Add result_set to ballot serializer --- ynr/apps/api/tests/test_api_next.py | 40 +++++++++++++++++++++ ynr/apps/elections/api/next/serializers.py | 3 ++ ynr/apps/uk_results/api/next/serializers.py | 12 +++++++ 3 files changed, 55 insertions(+) diff --git a/ynr/apps/api/tests/test_api_next.py b/ynr/apps/api/tests/test_api_next.py index c78207a68..5d76010f6 100644 --- a/ynr/apps/api/tests/test_api_next.py +++ b/ynr/apps/api/tests/test_api_next.py @@ -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( @@ -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, + }, + ) diff --git a/ynr/apps/elections/api/next/serializers.py b/ynr/apps/elections/api/next/serializers.py index 2bbe92fb4..32ee4f29b 100644 --- a/ynr/apps/elections/api/next/serializers.py +++ b/ynr/apps/elections/api/next/serializers.py @@ -9,6 +9,7 @@ MinimalPostSerializer, ) from rest_framework import serializers +from uk_results.api.next.serializers import MinimalResultSerializer class MinimalElectionSerializer(serializers.HyperlinkedModelSerializer): @@ -102,6 +103,7 @@ class Meta: "replaces", "replaced_by", "uncontested", + "results", ) replaces = serializers.SlugRelatedField( @@ -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", diff --git a/ynr/apps/uk_results/api/next/serializers.py b/ynr/apps/uk_results/api/next/serializers.py index c527e918a..54371d961 100644 --- a/ynr/apps/uk_results/api/next/serializers.py +++ b/ynr/apps/uk_results/api/next/serializers.py @@ -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 From 347635261a5474f37fa0047fa961ee7fc3c88fb1 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Fri, 8 Sep 2023 13:33:15 +0100 Subject: [PATCH 2/4] Add election results to select_related call on BallotViewSet --- ynr/apps/elections/api/next/api_views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ynr/apps/elections/api/next/api_views.py b/ynr/apps/elections/api/next/api_views.py index 6a6b730c8..5e87ccbc9 100644 --- a/ynr/apps/elections/api/next/api_views.py +++ b/ynr/apps/elections/api/next/api_views.py @@ -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( From da767a6cbecee0365fd89f36a747a0dd7ff8ac6b Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Mon, 11 Sep 2023 16:14:43 +0100 Subject: [PATCH 3/4] Fix test to reflect current date --- ynr/apps/elections/tests/test_ballot_view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ynr/apps/elections/tests/test_ballot_view.py b/ynr/apps/elections/tests/test_ballot_view.py index a3f967972..fc2eb2fd1 100644 --- a/ynr/apps/elections/tests/test_ballot_view.py +++ b/ynr/apps/elections/tests/test_ballot_view.py @@ -131,7 +131,7 @@ def test_ballot_with_candidates_no_sopn(self): self.assertEqual(response.context["candidates"].count(), 9) self.assertDataTimelineCandidateAddingInProgress(response) self.assertInHTML( - "

Candidates for Bar Ward on
7 September 2023

", + f"

Candidates for Bar Ward on
{ self.election.election_date.strftime('%d %B %Y') }

", response.text, ) self.assertInHTML( From 8714c5ed4b9e04f22b389ccda46f48697e587145 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Tue, 12 Sep 2023 15:47:49 +0100 Subject: [PATCH 4/4] Tell swagger this object is always embedded --- ynr/apps/official_documents/api/next/serializers.py | 1 + ynr/apps/uk_results/api/next/serializers.py | 1 + 2 files changed, 2 insertions(+) diff --git a/ynr/apps/official_documents/api/next/serializers.py b/ynr/apps/official_documents/api/next/serializers.py index 9c61451d7..d588a2b97 100644 --- a/ynr/apps/official_documents/api/next/serializers.py +++ b/ynr/apps/official_documents/api/next/serializers.py @@ -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 diff --git a/ynr/apps/uk_results/api/next/serializers.py b/ynr/apps/uk_results/api/next/serializers.py index 54371d961..d6897a030 100644 --- a/ynr/apps/uk_results/api/next/serializers.py +++ b/ynr/apps/uk_results/api/next/serializers.py @@ -78,6 +78,7 @@ class Meta: "source", "total_electorate", ) + ref_name = None # Tells swagger that this is always embedded class ElectedSerializer(CandidacyOnBallotSerializer):