From 80ff09dc31315132048d9021dc3495b3faffe450 Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Wed, 28 Feb 2024 15:23:41 +0000 Subject: [PATCH] Add deselected to API response --- wcivf/apps/api/serializers.py | 17 +++++++++++------ wcivf/apps/people/tests/test_person_views.py | 9 +++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/wcivf/apps/api/serializers.py b/wcivf/apps/api/serializers.py index 9decd640c..028487ea0 100644 --- a/wcivf/apps/api/serializers.py +++ b/wcivf/apps/api/serializers.py @@ -10,9 +10,7 @@ class PersonSerializer(serializers.ModelSerializer): def get_absolute_url(self, obj): if "request" in self.context: - return self.context["request"].build_absolute_uri( - obj.get_absolute_url() - ) + return self.context["request"].build_absolute_uri(obj.get_absolute_url()) return obj.get_absolute_url() class Meta: @@ -49,14 +47,21 @@ class Meta: "party", "person", "previous_party_affiliations", + "deselected", + "deselected_source", ) person = PersonSerializer(many=False, read_only=True) party = PartySerializer(many=False, read_only=True) list_position = serializers.SerializerMethodField(allow_null=True) - previous_party_affiliations = serializers.SerializerMethodField( - allow_null=True - ) + previous_party_affiliations = serializers.SerializerMethodField(allow_null=True) + + def to_representation(self, instance): + ret = super().to_representation(instance) + if not ret["deselected"]: + del ret["deselected"] + del ret["deselected_source"] + return ret def get_list_position(self, obj): """ diff --git a/wcivf/apps/people/tests/test_person_views.py b/wcivf/apps/people/tests/test_person_views.py index 72ddab065..cec2aa487 100644 --- a/wcivf/apps/people/tests/test_person_views.py +++ b/wcivf/apps/people/tests/test_person_views.py @@ -587,6 +587,15 @@ def test_deselected(self): ) self.assertContains(response, "www.candidate-party-page.co.uk") + response = self.client.get( + f"/api/candidates_for_ballots/?ballot_ids={person_post.post_election.ballot_paper_id}" + ) + self.assertTrue(response.json()[0]["candidates"][0]["deselected"]) + self.assertEqual( + response.json()[0]["candidates"][0]["deselected_source"], + "www.candidate-party-page.co.uk", + ) + class TestPersonViewUnitTests: @pytest.fixture