From 4c1c86adb898ce47110a8e4b92ef42f875c515fc Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Tue, 24 Dec 2024 11:59:27 -0300 Subject: [PATCH] fix(transactions:calculations:percentages): KeyError when calculating exchange --- app/apps/transactions/utils/calculations.py | 38 ++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/app/apps/transactions/utils/calculations.py b/app/apps/transactions/utils/calculations.py index 4f9da15..ccf9a36 100644 --- a/app/apps/transactions/utils/calculations.py +++ b/app/apps/transactions/utils/calculations.py @@ -203,10 +203,12 @@ def calculate_percentage_distribution(currency_totals): if "exchanged" in data: exchanged_total = sum( [ - abs(data["exchanged"]["income_current"]), - abs(data["exchanged"]["income_projected"]), - abs(data["exchanged"]["expense_current"]), - abs(data["exchanged"]["expense_projected"]), + abs(data.get("exchanged", {}).get("income_current", Decimal("0"))), + abs( + data.get("exchanged", {}).get("income_projected", Decimal("0")) + ), + abs(data.get("exchanged", {}).get("expense_current", Decimal("0"))), + abs(data.get("exchanged", {}).get("income_current", Decimal("0"))), ] ) @@ -218,19 +220,39 @@ def calculate_percentage_distribution(currency_totals): if exchanged_total > 0: percentages[currency_id]["exchanged"]["percentages"] = { "income_current": ( - abs(data["exchanged"]["income_current"]) / exchanged_total + abs( + data.get("exchanged", {}).get( + "income_current", Decimal("0") + ) + ) + / exchanged_total ) * 100, "income_projected": ( - abs(data["exchanged"]["income_projected"]) / exchanged_total + abs( + data.get("exchanged", {}).get( + "income_projected", Decimal("0") + ) + ) + / exchanged_total ) * 100, "expense_current": ( - abs(data["exchanged"]["expense_current"]) / exchanged_total + abs( + data.get("exchanged", {}).get( + "expense_current", Decimal("0") + ) + ) + / exchanged_total ) * 100, "expense_projected": ( - abs(data["exchanged"]["expense_projected"]) / exchanged_total + abs( + data.get("exchanged", {}).get( + "income_current", Decimal("0") + ) + ) + / exchanged_total ) * 100, }