From b4d20ae0bc898aea6c7d2803b72c72ae2d5e5385 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Thu, 14 Sep 2023 13:28:38 +0100 Subject: [PATCH] Show results table only when data exists --- wcivf/apps/elections/models.py | 12 ++++++++++++ .../templates/elections/includes/_single_ballot.html | 10 +++++----- wcivf/apps/elections/tests/test_election_views.py | 8 +++++++- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/wcivf/apps/elections/models.py b/wcivf/apps/elections/models.py index 1feea42f1..db2bd98ba 100644 --- a/wcivf/apps/elections/models.py +++ b/wcivf/apps/elections/models.py @@ -243,6 +243,18 @@ def pluralized_division_name(self): return pluralise.get(suffix, f"{suffix}s") + @property + def has_results(self): + """ + Returns a boolean for if the election has results + """ + return bool( + self.spoilt_ballots + or self.ballot_papers_issued + or self.turnout + or self.electorate + ) + class Post(models.Model): """ diff --git a/wcivf/apps/elections/templates/elections/includes/_single_ballot.html b/wcivf/apps/elections/templates/elections/includes/_single_ballot.html index 9780500bd..209f42991 100644 --- a/wcivf/apps/elections/templates/elections/includes/_single_ballot.html +++ b/wcivf/apps/elections/templates/elections/includes/_single_ballot.html @@ -121,7 +121,7 @@

{% if postelection.is_london_assembly_additional %}{% trans "Additional memb {% endif %}

{% endif %} - {% if postelection.election.in_past %} + {% if postelection.election.in_past and postelection.election.has_results %}
@@ -135,25 +135,25 @@

{% if postelection.is_london_assembly_additional %}{% trans "Additional memb

diff --git a/wcivf/apps/elections/tests/test_election_views.py b/wcivf/apps/elections/tests/test_election_views.py index d6c2498f9..0d5a2ac15 100644 --- a/wcivf/apps/elections/tests/test_election_views.py +++ b/wcivf/apps/elections/tests/test_election_views.py @@ -142,12 +142,18 @@ def test_results_table(self): """check that the table containing the electorate, turnout, spoilt ballots, ballot papers exist for past elections""" + today = str(datetime.today().date()) + self.election.electorate = 100 + self.election.spoilt_ballots = 5 + self.election.save() + response = self.client.get( self.post_election.get_absolute_url(), follow=True ) self.assertEqual(response.status_code, 200) - today = str(datetime.today().date()) + self.assertLess(self.election.election_date, today) + self.assertTrue(self.post_election.election.has_results) self.assertContains(response, "Electorate") self.assertContains(response, "Turnout") self.assertContains(response, "Spoilt Ballots")
{% if postelection.election.electorate %} {{ postelection.election.electorate }} {% else %} - {% trans "Electorate not available" %} + {% trans "None" %} {% endif %} {% if postelection.election.turnout %} {{ postelection.election.turnout|stringformat:"d%%" }} {% else %} - {% trans "Turnout not available" %} + {% trans "None" %} {% endif %} {% if postelection.election.ballot_papers_issued %} {{ postelection.election.ballot_papers_issued }} {% else %} - {% trans "Number of ballot papers issued not available" %} + {% trans "None" %} {% endif %} {% if postelection.election.spoilt_ballots %} {{ postelection.election.spoilt_ballots }} {% else %} - {% trans "Number of spoilt ballots not available" %} + {% trans "None" %} {% endif %}