From 2b1f182d62a623540abb976b37d07246c8e3f924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 14 Mar 2016 12:14:46 +0100 Subject: [PATCH] [FIX] fix comparison rendering in presence of AccountingNone --- mis_builder/models/accounting_none.py | 5 +++++ mis_builder/models/mis_builder.py | 12 +++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mis_builder/models/accounting_none.py b/mis_builder/models/accounting_none.py index 57f7f549..5249e540 100644 --- a/mis_builder/models/accounting_none.py +++ b/mis_builder/models/accounting_none.py @@ -27,6 +27,8 @@ AccountingNone -1 >>> 1 - AccountingNone 1 +>>> abs(AccountingNone) +AccountingNone >>> AccountingNone - None AccountingNone >>> AccountingNone / 2 @@ -115,6 +117,9 @@ class AccountingNoneType(object): return AccountingNone return -other + def __abs__(self): + return self + def __pos__(self): return self diff --git a/mis_builder/models/mis_builder.py b/mis_builder/models/mis_builder.py index c206a00c..19f212f7 100644 --- a/mis_builder/models/mis_builder.py +++ b/mis_builder/models/mis_builder.py @@ -156,17 +156,19 @@ class MisReportKpi(models.Model): average_value, average_base_value): """ render the comparison of two KPI values, ready for display """ assert len(self) == 1 - if value is None or base_value is None: - return '' + if value is None: + value = AccountingNone + if base_value is None: + base_value = AccountingNone if self.type == 'pct': return self._render_num( lang_id, value - base_value, 0.01, self.dp, '', _('pp'), sign='+') elif self.type == 'num': - if average_value: + if value and average_value: value = value / float(average_value) - if average_base_value: + if base_value and average_base_value: base_value = base_value / float(average_base_value) if self.compare_method == 'diff': return self._render_num( @@ -174,7 +176,7 @@ class MisReportKpi(models.Model): value - base_value, self.divider, self.dp, self.prefix, self.suffix, sign='+') elif self.compare_method == 'pct': - if round(base_value, self.dp) != 0: + if base_value and round(base_value, self.dp) != 0: return self._render_num( lang_id, (value - base_value) / abs(base_value),