From f32d0afdbf6dd738dadd6ca4e7be8f605bbc0041 Mon Sep 17 00:00:00 2001 From: Zarino Zappia Date: Fri, 1 Nov 2024 15:17:04 +0000 Subject: [PATCH] Avoid displaying ".0" decimals on question marks Fixes #699. --- caps/templatetags/caps_templatetags.py | 16 ++++++++++++++++ scoring/templates/scoring/council.html | 4 ++-- scoring/templates/scoring/section.html | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/caps/templatetags/caps_templatetags.py b/caps/templatetags/caps_templatetags.py index 0bda5514..b2b635dc 100644 --- a/caps/templatetags/caps_templatetags.py +++ b/caps/templatetags/caps_templatetags.py @@ -71,6 +71,22 @@ def percentage(value: float): return f"{value * 100:.0f}%" +@register.filter +def format_mark(value): + """ + Display whole-number marks as a whole numbers, and + fractional marks as decimals, removing trailing zeroes. + """ + try: + value = float(value) + if value.is_integer(): + return f"{int(value)}" + else: + return f"{value}" + except (ValueError, TypeError): + return value + + @register.simple_tag(takes_context=True) def council_card(context: dict, slug: str, title="", color: str = "red"): """ diff --git a/scoring/templates/scoring/council.html b/scoring/templates/scoring/council.html index f658703e..6bed6222 100644 --- a/scoring/templates/scoring/council.html +++ b/scoring/templates/scoring/council.html @@ -296,12 +296,12 @@

Visit us again on a bigger screen

- {{ answer.score }}/{{ answer.max }} + {{ answer.score|format_mark }}/{{ answer.max }} {% for comparison in answer.comparisons %} - {{ comparison.score }}/{{ comparison.max }} + {{ comparison.score|format_mark }}/{{ comparison.max }} {% endfor %} diff --git a/scoring/templates/scoring/section.html b/scoring/templates/scoring/section.html index 40d0146c..caffdfde 100644 --- a/scoring/templates/scoring/section.html +++ b/scoring/templates/scoring/section.html @@ -320,7 +320,7 @@

{{ council_type.name|title }}

{% for comparison in question.comparisons %} - {{ comparison.score }}/{{ question.details.max_score }} + {{ comparison.score|format_mark }}/{{ question.details.max_score }} {{ comparison.council_name }} {% endfor %}