From d0b6be14e70f1d34154e9c261846c4800e38ca29 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sun, 8 Nov 2026 21:48:40 -0300 Subject: [PATCH] feat(investments): calmer list and detail pages, group holdings by asset class - One hero number with a single return line; secondary totals behind a Details toggle - Holdings in one list card with quiet group subheaders, value and %/yr per row - Group holdings by account group or asset class, remembered in the session - Detail page: value and chart up top, Your position and home-currency sidebar - Replace tooltips with visible text; keep amounts from wrapping on phones --- app/apps/investments/urls.py | 5 + app/apps/investments/views.py | 40 ++- .../investments/fragments/_allocation.html | 3 +- app/templates/investments/fragments/_mwr.html | 2 +- .../investments/fragments/_totals.html | 83 +++--- .../investments/fragments/detail.html | 261 +++++++++--------- app/templates/investments/fragments/list.html | 101 +++---- .../investments/fragments/rebalance.html | 3 +- 8 files changed, 251 insertions(+), 247 deletions(-) diff --git a/app/apps/investments/urls.py b/app/apps/investments/urls.py index 25c52f19..3fe69d2e 100644 --- a/app/apps/investments/urls.py +++ b/app/apps/investments/urls.py @@ -5,6 +5,11 @@ from . import views urlpatterns = [ path("investments/", views.investments_index, name="investments_index"), path("investments/list/", views.investments_list, name="investments_list"), + path( + "investments/group-by/", + views.investments_group_by, + name="investments_group_by", + ), path("investments/charts/", views.investments_charts, name="investments_charts"), path( "investments/rebalance/", diff --git a/app/apps/investments/views.py b/app/apps/investments/views.py index db0dec6c..5f94faf7 100644 --- a/app/apps/investments/views.py +++ b/app/apps/investments/views.py @@ -25,7 +25,7 @@ from apps.transactions.models import Transaction def _holdings(): return Account.objects.filter(is_investment=True, is_archived=False).select_related( - "group", "currency", "exchange_currency" + "group", "investment_class", "currency", "exchange_currency" ) @@ -34,31 +34,47 @@ def investments_index(request): return render(request, "investments/pages/index.html") +GROUP_BY = ("group", "investment_class") + + +def _grouped(performances, attr): + groups = {} + for perf in performances: + group = getattr(perf.account, attr) + groups.setdefault(group.id if group else None, (group, []))[1].append(perf) + return [ + {"group": group, "holdings": items, "totals": summarize(items)} + for group, items in sorted( + groups.values(), key=lambda g: (g[0] is None, g[0] and g[0].name) + ) + ] + + @only_htmx @login_required def investments_list(request): performances = [get_holding_performance(account) for account in _holdings()] - groups = {} - for perf in performances: - group = perf.account.group - groups.setdefault(group.id if group else None, (group, []))[1].append(perf) - + # Both groupings are rendered so the toggle flips client-side without recomputing return render( request, "investments/fragments/list.html", { - "groups": [ - {"group": group, "holdings": items, "totals": summarize(items)} - for group, items in sorted( - groups.values(), key=lambda g: (g[0] is None, g[0] and g[0].name) - ) - ], + "groupings": [(attr, _grouped(performances, attr)) for attr in GROUP_BY], "totals": summarize(performances, convert=True), + "group_by": request.session.get("investments_group_by", "group"), }, ) +@only_htmx +@login_required +def investments_group_by(request): + if request.GET.get("group_by") in GROUP_BY: + request.session["investments_group_by"] = request.GET["group_by"] + return HttpResponse(status=204) + + def _money(currency): """What chartMoney (charts.js) needs to format amounts like the rest of the app.""" return { diff --git a/app/templates/investments/fragments/_allocation.html b/app/templates/investments/fragments/_allocation.html index 3e21bc23..5fc5bdf6 100644 --- a/app/templates/investments/fragments/_allocation.html +++ b/app/templates/investments/fragments/_allocation.html @@ -7,8 +7,7 @@ {{ share.name }} {% if share.plan.off %} - + {% blocktranslate with target=share.plan.target|floatformat:'-2' %}target {{ target }}%{% endblocktranslate %} {% endif %} {{ share.share|floatformat:1 }}% diff --git a/app/templates/investments/fragments/_mwr.html b/app/templates/investments/fragments/_mwr.html index 9ade9d0e..4d57adce 100644 --- a/app/templates/investments/fragments/_mwr.html +++ b/app/templates/investments/fragments/_mwr.html @@ -1 +1 @@ -{% load i18n %}{% if mw %}{% include 'investments/fragments/_percentage.html' with value=mw.percentage %}{% if mw.annualized %}{% translate '/yr' %}{% endif %}{% endif %} +{% load i18n %}{% if mw %}{% include 'investments/fragments/_percentage.html' with value=mw.percentage %}{% if mw.annualized %}{% translate '/yr' %}{% endif %}{% endif %} diff --git a/app/templates/investments/fragments/_totals.html b/app/templates/investments/fragments/_totals.html index 8dbdbfa1..3d20e3ad 100644 --- a/app/templates/investments/fragments/_totals.html +++ b/app/templates/investments/fragments/_totals.html @@ -1,51 +1,56 @@ {% load i18n %} {% with c=total.currency %}
-
-
-
-
- -
-
-
-
- - {% include 'investments/fragments/_mwr.html' with mw=total.money_weighted %} - {% translate 'total return' %} -
-
+
+
{% translate 'Portfolio value' %}{% if totals|length > 1 %} · {{ c.name }}{% endif %}
+
+
-
-
{% translate 'Invested' %}
-
{% translate 'Income' %}
- {% if total.converted_from %} - {% if total.has_rate_history %} -
{% translate 'Currency effect' %}
- {% else %} -
{% translate 'Currency effect' %}
+
+ + {% include 'investments/fragments/_mwr.html' with mw=total.money_weighted %} + {% translate 'all time' %} +
+ + {# Secondary figures stay out of sight until asked for #} +
+ + {% translate 'Details' %} + +
+
+
{% translate 'Invested' %}
+
+
+
+
{% translate 'Income' %}
+
+
+ {% if total.converted_from %} +
+
{% translate 'Currency effect' %}
+
+ {% if total.has_rate_history %} + + {% else %} + {% translate 'Needs historical exchange rates' %} + {% endif %} +
+
{% endif %} - {% endif %} -
- {% if total.breakdown %} -
{% for part in total.breakdown %} {% with p=part.currency %} -
- - {{ p.name }} - - -
-
{% translate 'Return' %} {% include 'investments/fragments/_mwr.html' with mw=part.money_weighted %}
-
{% translate 'Invested' %}
-
{% translate 'Income' %}
-
-
+
+
{% blocktranslate with name=p.name %}In {{ name }}{% endblocktranslate %}
+
+ + {% include 'investments/fragments/_mwr.html' with mw=part.money_weighted %} +
+
{% endwith %} {% endfor %} -
- {% endif %} + +
{% endwith %} diff --git a/app/templates/investments/fragments/detail.html b/app/templates/investments/fragments/detail.html index 10b744fb..401d5446 100644 --- a/app/templates/investments/fragments/detail.html +++ b/app/templates/investments/fragments/detail.html @@ -31,69 +31,34 @@ {% endif %}
-
-
-
- - {{ account.name }} - -
-
- {% if account.group %}{{ account.group.name }} · {% endif %} - {% if perf.priced %}{{ a.name }} → {{ c.name }}{% else %}{% translate 'Tracked by value' %}{% endif %} - {% if perf.estimated %}{% endif %} -
-
+
+ +

{{ account.name }}

+ +
+
+ {% if account.group %}{{ account.group.name }} · {% endif %} + {% if perf.priced %}{{ a.name }} → {{ c.name }}{% else %}{% translate 'Tracked by value' %}{% endif %}
-
-
-
- {% if perf.market_value is not None %}{% else %}-{% endif %} -
-
- - {% include 'investments/fragments/_mwr.html' with mw=perf.money_weighted %} - {% translate 'total return' %} -
- {% if home %} - {% with h=home.currency %} -
- {% translate 'In' %} {{ h.name }}: - - · - - {% include 'investments/fragments/_mwr.html' with mw=home.money_weighted %} - {% if home.has_rate_history %} - · {% translate 'currency effect' %} - - - {% else %} - · {% translate 'currency effect' %} - {% endif %} -
- {% endwith %} - {% endif %} +
+
+ {% if perf.market_value is not None %}{% else %}-{% endif %}
- {% if perf.priced %} -
-
-
- {% if perf.price is not None %} - × - {% if perf.price_date %} · {{ perf.price_date|date:"SHORT_DATE_FORMAT" }}{% endif %} - {% else %} - {% translate "No exchange rate available" %} - {% endif %} -
-
- {% endif %} +
+ + {% include 'investments/fragments/_mwr.html' with mw=perf.money_weighted %} + {% translate 'all time' %} +
+ {% if home %}{% with h=home.currency %} +
≈
+ {% endwith %}{% endif %}
@@ -217,69 +182,96 @@ })); })(); - -
-
-
{% translate 'Invested' %}
-
-
-
-
{% if perf.priced %}{% translate 'Average cost' %}{% else %}{% translate 'Put in / taken out' %}{% endif %}
-
- {% if perf.priced %} - {% if perf.average_cost is not None %}{% else %}-{% endif %} - {% else %} - - / - - {% endif %} -
-
-
-
{% if perf.priced %}{% translate 'Unrealized' %}{% else %}{% translate 'Earnings' %}{% endif %}
-
- {% if perf.unrealized is not None %} - - {% include 'investments/fragments/_percentage.html' with value=perf.unrealized_percentage css="text-xs" %} - {% else %}-{% endif %} -
-
-
-
{% translate 'Realized' %}
-
-
-
-
{% translate 'Income' %}{% if income_entities %} · {{ income_entities|join:", " }}{% endif %}
-
-
- {% if perf.priced %} -
-
{% translate 'Put in / taken out' %}
-
- - / - -
-
- {% endif %} -
-
-
-
{% translate 'Movements' %}
- {% if transactions %} -
- - + {% if perf.estimated %} +
+ + {% translate 'Some units moved in or out without a linked payment, so they were valued at the market price of that day.' %} +
+ {% endif %} + +
+
+
+
{% translate 'Your position' %}
+
+ {% if perf.priced %} +
+
{% translate 'Units' %}
+
+
+
+
{% translate 'Price' %}
+
{% if perf.price is not None %}{% if perf.price_date %}
{{ perf.price_date|date:"SHORT_DATE_FORMAT" }}
{% endif %}{% else %}{% translate "No exchange rate available" %}{% endif %}
+
+
+
{% translate 'Average cost' %}
+
{% if perf.average_cost is not None %}{% else %}-{% endif %}
+
+ {% endif %} +
+
{% translate 'Invested' %}
+
+
+
+
{% if perf.priced %}{% translate 'Unrealized' %}{% else %}{% translate 'Earnings' %}{% endif %}
+
{% if perf.unrealized is not None %}{% else %}-{% endif %}
+
+
+
{% translate 'Realized' %}
+
+
+
+
{% translate 'Income' %}
+
+
+
+
{% translate 'Put in' %}
+
+
+
+
{% translate 'Taken out' %}
+
+
+
+
+ {% if home %}{% with h=home.currency %} +
+
{% blocktranslate with name=h.name %}In {{ name }}{% endblocktranslate %}
+
+
+
{% translate 'Value' %}
+
+
+
+
{% translate 'Return' %}
+
{% include 'investments/fragments/_mwr.html' with mw=home.money_weighted %}
+
+
+
{% translate 'From exchange rates' %}
+
{% if home.has_rate_history %}{% else %}{% translate "Needs historical exchange rates" %}{% endif %}
+
+
+
+ {% endwith %}{% endif %} +
+ +
+
+
{% translate 'Movements' %}
+ {% if transactions %} +
+
+ {% for tx in transactions %} {% with p=tx.linked_transaction %} - {% endwith %} {% endfor %} - -
{{ tx.date|date:"SHORT_DATE_FORMAT" }}
{{ tx.description }}
+ {{ tx.date|date:"SHORT_DATE_FORMAT" }} · {% if p %} {% if tx.type == 'IN' %}{% translate 'Bought / contributed' %}{% else %}{% translate 'Sold / withdrew' %}{% endif %} · {{ p.account.name }} @@ -302,42 +294,45 @@
-
- {% else %} - - {% endif %} -
- {% if income %} -
-
{% translate 'Income' %}
-
- - + +
+
+ {% else %} + + {% endif %} +
+ {% if income %} +
+
+
{% translate 'Income' %}
+ {% if income_entities %}
{{ income_entities|join:", " }}
{% endif %} +
+
+ + {% for tx in income %} - - + {% endfor %} - -
{{ tx.date|date:"SHORT_DATE_FORMAT" }}{{ tx.description }}
{{ tx.account.name }}
{{ tx.description }}
{{ tx.date|date:"SHORT_DATE_FORMAT" }} · {{ tx.account.name }}
+ + +
-
- {% endif %} + {% endif %} +
{% endwith %} diff --git a/app/templates/investments/fragments/list.html b/app/templates/investments/fragments/list.html index 4f6404c2..4c5337a6 100644 --- a/app/templates/investments/fragments/list.html +++ b/app/templates/investments/fragments/list.html @@ -10,8 +10,8 @@ {% endspaceless %}
- {% if groups %} -
+ {% if groupings.0.1 %} +
{% for total in totals %} {% include 'investments/fragments/_totals.html' %} {% endfor %} @@ -20,73 +20,58 @@
- {% for item in groups %} -
-
- {% if item.group %}{{ item.group.name }}{% else %}{% translate 'Ungrouped' %}{% endif %} -
+
+ + +
+ {% for attr, groups in groupings %} +
+ {% for item in groups %} +
+ {% if item.group %}{{ item.group.name }}{% elif attr == 'investment_class' %}{% translate 'Unclassified' %}{% else %}{% translate 'Ungrouped' %}{% endif %} +
{% for total in item.totals %} -
- - {% include 'investments/fragments/_mwr.html' with mw=total.money_weighted %} -
+ {% endfor %}
-
- - - - - - - - - - - - - - - - - {% for perf in item.holdings %} - {% with c=perf.currency a=perf.account.currency %} - - - - - - - {% endwith %} - {% endfor %} - -
{% translate 'Investment' %}{% translate 'Value' %}
-
- {{ perf.account.name }} - {% if perf.estimated %}{% endif %} -
+
- {# Phones show value + return %, wider screens split them into columns #} -
{% if perf.market_value is not None %}{% else %}-{% endif %}
- + +
+
{% if perf.market_value is not None %}{% else %}-{% endif %}
{% include 'investments/fragments/_mwr.html' with mw=perf.money_weighted %}
-
-
-
+
+ + + {% endwith %} + {% endfor %} + + {% endfor %} +
+
{% endfor %}
{{ row.class.name }} - {% if row.off %}{% endif %} - {{ row.share|floatformat:1 }}% + {{ row.share|floatformat:1 }}% {{ row.target|floatformat:"-2" }}% {% if row.move > 0 %}