mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-24 17:48:41 +02:00
Merge pull request #310
refactor(networth): convert into a single page instead of two
This commit is contained in:
@@ -5,4 +5,5 @@ from . import views
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("net-worth/current/", views.net_worth_current, name="net_worth_current"),
|
path("net-worth/current/", views.net_worth_current, name="net_worth_current"),
|
||||||
path("net-worth/projected/", views.net_worth_projected, name="net_worth_projected"),
|
path("net-worth/projected/", views.net_worth_projected, name="net_worth_projected"),
|
||||||
|
path("net-worth/", views.net_worth, name="net_worth"),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import json
|
|||||||
|
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.core.serializers.json import DjangoJSONEncoder
|
from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render, redirect
|
||||||
from django.views.decorators.http import require_http_methods
|
from django.views.decorators.http import require_http_methods
|
||||||
|
|
||||||
from apps.net_worth.utils.calculate_net_worth import (
|
from apps.net_worth.utils.calculate_net_worth import (
|
||||||
@@ -18,18 +18,38 @@ from apps.transactions.utils.calculations import (
|
|||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@require_http_methods(["GET"])
|
@require_http_methods(["GET"])
|
||||||
def net_worth_current(request):
|
def net_worth(request):
|
||||||
transactions_currency_queryset = Transaction.objects.filter(
|
if "view_type" in request.GET:
|
||||||
is_paid=True, account__is_archived=False
|
print(request.GET["view_type"])
|
||||||
).order_by(
|
view_type = request.GET["view_type"]
|
||||||
"account__currency__name",
|
request.session["networth_view_type"] = view_type
|
||||||
)
|
else:
|
||||||
transactions_account_queryset = Transaction.objects.filter(
|
view_type = request.session.get("networth_view_type", "current")
|
||||||
is_paid=True, account__is_archived=False
|
|
||||||
).order_by(
|
if view_type == "current":
|
||||||
"account__group__name",
|
transactions_currency_queryset = Transaction.objects.filter(
|
||||||
"account__name",
|
is_paid=True, account__is_archived=False
|
||||||
)
|
).order_by(
|
||||||
|
"account__currency__name",
|
||||||
|
)
|
||||||
|
transactions_account_queryset = Transaction.objects.filter(
|
||||||
|
is_paid=True, account__is_archived=False
|
||||||
|
).order_by(
|
||||||
|
"account__group__name",
|
||||||
|
"account__name",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
transactions_currency_queryset = Transaction.objects.filter(
|
||||||
|
account__is_archived=False
|
||||||
|
).order_by(
|
||||||
|
"account__currency__name",
|
||||||
|
)
|
||||||
|
transactions_account_queryset = Transaction.objects.filter(
|
||||||
|
account__is_archived=False
|
||||||
|
).order_by(
|
||||||
|
"account__group__name",
|
||||||
|
"account__name",
|
||||||
|
)
|
||||||
|
|
||||||
currency_net_worth = calculate_currency_totals(
|
currency_net_worth = calculate_currency_totals(
|
||||||
transactions_queryset=transactions_currency_queryset, deep_search=True
|
transactions_queryset=transactions_currency_queryset, deep_search=True
|
||||||
@@ -116,111 +136,22 @@ def net_worth_current(request):
|
|||||||
"currencies": currencies,
|
"currencies": currencies,
|
||||||
"chart_data_accounts_json": chart_data_accounts_json,
|
"chart_data_accounts_json": chart_data_accounts_json,
|
||||||
"accounts": accounts,
|
"accounts": accounts,
|
||||||
"type": "current",
|
"type": view_type,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
@require_http_methods(["GET"])
|
||||||
|
def net_worth_current(request):
|
||||||
|
request.session["networth_view_type"] = "current"
|
||||||
|
|
||||||
|
return redirect("net_worth")
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@require_http_methods(["GET"])
|
@require_http_methods(["GET"])
|
||||||
def net_worth_projected(request):
|
def net_worth_projected(request):
|
||||||
transactions_currency_queryset = Transaction.objects.filter(
|
request.session["networth_view_type"] = "projected"
|
||||||
account__is_archived=False
|
|
||||||
).order_by(
|
|
||||||
"account__currency__name",
|
|
||||||
)
|
|
||||||
transactions_account_queryset = Transaction.objects.filter(
|
|
||||||
account__is_archived=False
|
|
||||||
).order_by(
|
|
||||||
"account__group__name",
|
|
||||||
"account__name",
|
|
||||||
)
|
|
||||||
|
|
||||||
currency_net_worth = calculate_currency_totals(
|
return redirect("net_worth")
|
||||||
transactions_queryset=transactions_currency_queryset, deep_search=True
|
|
||||||
)
|
|
||||||
account_net_worth = calculate_account_totals(
|
|
||||||
transactions_queryset=transactions_account_queryset
|
|
||||||
)
|
|
||||||
|
|
||||||
historical_currency_net_worth = calculate_historical_currency_net_worth(
|
|
||||||
queryset=transactions_currency_queryset
|
|
||||||
)
|
|
||||||
|
|
||||||
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_currency_net_worth.values()
|
|
||||||
]
|
|
||||||
datasets.append(
|
|
||||||
{
|
|
||||||
"label": currency,
|
|
||||||
"data": data,
|
|
||||||
"yAxisID": f"y{i}",
|
|
||||||
"fill": False,
|
|
||||||
"tension": 0.1,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
chart_data_currency = {"labels": labels, "datasets": datasets}
|
|
||||||
|
|
||||||
chart_data_currency_json = json.dumps(chart_data_currency, cls=DjangoJSONEncoder)
|
|
||||||
|
|
||||||
historical_account_balance = calculate_historical_account_balance(
|
|
||||||
queryset=transactions_account_queryset
|
|
||||||
)
|
|
||||||
|
|
||||||
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):
|
|
||||||
data = [
|
|
||||||
float(month_data[account])
|
|
||||||
for month_data in historical_account_balance.values()
|
|
||||||
]
|
|
||||||
datasets.append(
|
|
||||||
{
|
|
||||||
"label": account,
|
|
||||||
"data": data,
|
|
||||||
"fill": False,
|
|
||||||
"tension": 0.1,
|
|
||||||
"yAxisID": f"y-axis-{i}", # Assign each dataset to its own Y-axis
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
chart_data_accounts = {"labels": labels, "datasets": datasets}
|
|
||||||
|
|
||||||
chart_data_accounts_json = json.dumps(chart_data_accounts, cls=DjangoJSONEncoder)
|
|
||||||
|
|
||||||
return render(
|
|
||||||
request,
|
|
||||||
"net_worth/net_worth.html",
|
|
||||||
{
|
|
||||||
"currency_net_worth": currency_net_worth,
|
|
||||||
"account_net_worth": account_net_worth,
|
|
||||||
"chart_data_currency_json": chart_data_currency_json,
|
|
||||||
"currencies": currencies,
|
|
||||||
"chart_data_accounts_json": chart_data_accounts_json,
|
|
||||||
"accounts": accounts,
|
|
||||||
"type": "projected",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -273,7 +273,6 @@
|
|||||||
function setupChart() {
|
function setupChart() {
|
||||||
var rawData = JSON.parse(document.getElementById('categoryOverviewData').textContent);
|
var rawData = JSON.parse(document.getElementById('categoryOverviewData').textContent);
|
||||||
var showing_string = JSON.parse(document.getElementById('showingString').textContent);
|
var showing_string = JSON.parse(document.getElementById('showingString').textContent);
|
||||||
console.log(showing_string)
|
|
||||||
|
|
||||||
// --- Dynamic Data Processing ---
|
// --- Dynamic Data Processing ---
|
||||||
var categories = [];
|
var categories = [];
|
||||||
|
|||||||
@@ -9,7 +9,29 @@
|
|||||||
{% block title %}{% if type == "current" %}{% translate 'Current Net Worth' %}{% else %}{% translate 'Projected Net Worth' %}{% endif %}{% endblock %}
|
{% block title %}{% if type == "current" %}{% translate 'Current Net Worth' %}{% else %}{% translate 'Projected Net Worth' %}{% endif %}{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div hx-trigger="every 60m" class="show-loading" hx-get="" hx-target="body">
|
<div hx-trigger="every 60m, updated from:window" hx-include="#view-type" class="show-loading" hx-get="" hx-target="body">
|
||||||
|
<div class="h-100 text-center mb-4 pt-2">
|
||||||
|
<div class="btn-group gap-3" role="group" id="view-type" _="on change trigger updated">
|
||||||
|
<input type="radio" class="btn-check"
|
||||||
|
name="view_type"
|
||||||
|
id="current-view"
|
||||||
|
autocomplete="off"
|
||||||
|
value="current"
|
||||||
|
{% if type == "current" %}checked{% endif %}>
|
||||||
|
<label class="btn btn-outline-primary rounded-5" for="current-view"><i
|
||||||
|
class="fa-solid fa-sack-dollar fa-fw me-2"></i>{% trans 'Current' %}</label>
|
||||||
|
|
||||||
|
<input type="radio"
|
||||||
|
class="btn-check"
|
||||||
|
name="view_type"
|
||||||
|
id="projected-view"
|
||||||
|
autocomplete="off"
|
||||||
|
value="projected"
|
||||||
|
{% if type == "projected" %}checked{% endif %}>
|
||||||
|
<label class="btn btn-outline-primary rounded-5" for="projected-view"><i
|
||||||
|
class="fa-solid fa-rocket fa-fw me-2"></i>{% trans 'Projected' %}</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="container px-md-3 py-3" _="init call initializeAccountChart() then initializeCurrencyChart() end">
|
<div class="container px-md-3 py-3" _="init call initializeAccountChart() then initializeCurrencyChart() end">
|
||||||
<div class="row gx-xl-4 gy-3 mb-4">
|
<div class="row gx-xl-4 gy-3 mb-4">
|
||||||
<div class="col-12 col-xl-5">
|
<div class="col-12 col-xl-5">
|
||||||
|
|||||||
Reference in New Issue
Block a user