From e0c165313b361f9827149a9d459ccd2d4e1af032 Mon Sep 17 00:00:00 2001 From: "R. Miles McCain" Date: Mon, 11 Jan 2021 12:11:27 -0500 Subject: [PATCH] Add fallback to percent_change_display (fixes #89) --- shynet/dashboard/templatetags/helpers.py | 37 +++++++++++++----------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/shynet/dashboard/templatetags/helpers.py b/shynet/dashboard/templatetags/helpers.py index 88b6d74..ae1bf2d 100644 --- a/shynet/dashboard/templatetags/helpers.py +++ b/shynet/dashboard/templatetags/helpers.py @@ -61,25 +61,28 @@ def relative_stat_tone( @register.simple_tag def percent_change_display(start, end): - if start == None or end == None: - return SafeString("Δ n/a") - if start == end: - direction = "Δ " - else: - direction = "↑ " if end > start else "↓ " - - if start == 0 and end != 0: - pct_change = "100%" - elif start == 0: - pct_change = "0%" - else: - change = int(round(100 * abs(end - start) / max(start, 1))) - if change > 999: - return "> 999%" + try: + if start == None or end == None: + return SafeString("Δ n/a") + if start == end: + direction = "Δ " else: - pct_change = str(change) + "%" + direction = "↑ " if end > start else "↓ " - return SafeString(direction + pct_change) + if start == 0 and end != 0: + pct_change = "100%" + elif start == 0: + pct_change = "0%" + else: + change = int(round(100 * abs(end - start) / max(start, 1))) + if change > 999: + return "> 999%" + else: + pct_change = str(change) + "%" + + return SafeString(direction + pct_change) + except: # TODO: filter for specific issues + return SafeString("Δ ?") @register.inclusion_tag("dashboard/includes/sidebar_footer.html")