fix: networth doesn't load if there aren't any transactions

This commit is contained in:
Herculino Trotta
2024-10-17 13:04:41 -03:00
parent 255fe6f771
commit e9359a3dfd
2 changed files with 61 additions and 35 deletions

View File

@@ -1,11 +1,7 @@
import json
from datetime import datetime
from dateutil.relativedelta import relativedelta
from django.core.serializers.json import DjangoJSONEncoder
from django.http import JsonResponse
from django.shortcuts import render
from django.utils import timezone
from apps.net_worth.utils.calculate_net_worth import (
calculate_currency_net_worth,
@@ -13,7 +9,6 @@ from apps.net_worth.utils.calculate_net_worth import (
calculate_account_net_worth,
calculate_historical_account_balance,
)
from apps.currencies.models import Currency
# Create your views here.
@@ -21,15 +16,24 @@ def net_worth_main(request):
currency_net_worth = calculate_currency_net_worth()
account_net_worth = calculate_account_net_worth()
historical_net_worth = calculate_historical_currency_net_worth()
historical_currency_net_worth = calculate_historical_currency_net_worth()
labels = list(historical_net_worth.keys())
currencies = list(historical_net_worth[labels[0]].keys())
labels = (
list(historical_currency_net_worth.keys())
if historical_currency_net_worth
else []
)
currencies = (
list(historical_currency_net_worth[labels[0]].keys())
if historical_currency_net_worth
else []
)
datasets = []
for i, currency in enumerate(currencies):
data = [
float(month_data[currency]) for month_data in historical_net_worth.values()
float(month_data[currency])
for month_data in historical_currency_net_worth.values()
]
datasets.append(
{
@@ -47,8 +51,14 @@ def net_worth_main(request):
historical_account_balance = calculate_historical_account_balance()
labels = list(historical_account_balance.keys())
accounts = list(historical_account_balance[labels[0]].keys())
labels = (
list(historical_account_balance.keys()) if historical_account_balance else []
)
accounts = (
list(historical_account_balance[labels[0]].keys())
if historical_account_balance
else []
)
datasets = []
for i, account in enumerate(accounts):