From 23d093a1172554e0de870ef366fd72eaffcbb6f7 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Tue, 22 Aug 2023 23:48:31 -0700 Subject: [PATCH 1/4] Fixes for fava update --- fava_investor/common/favainvestorapi.py | 2 +- fava_investor/common/libinvestor.py | 3 ++- .../modules/assetalloc_class/libassetalloc.py | 3 ++- fava_investor/modules/summarizer/libsummarizer.py | 1 + fava_investor/templates/Investor.html | 10 ++++------ 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/fava_investor/common/favainvestorapi.py b/fava_investor/common/favainvestorapi.py index 849787e..8e1e07a 100644 --- a/fava_investor/common/favainvestorapi.py +++ b/fava_investor/common/favainvestorapi.py @@ -7,7 +7,7 @@ class FavaInvestorAPI: def build_price_map(self): - return g.ledger.price_map + return g.ledger.prices def build_filtered_price_map(self, pcur, base_currency): """pcur and base_currency are currency strings""" diff --git a/fava_investor/common/libinvestor.py b/fava_investor/common/libinvestor.py index 8982d98..da88619 100644 --- a/fava_investor/common/libinvestor.py +++ b/fava_investor/common/libinvestor.py @@ -4,6 +4,7 @@ import decimal from beancount.core.inventory import Inventory from beancount.core import convert +from fava.core.conversion import convert_position class Node(object): @@ -74,7 +75,7 @@ def sum_inventories(invs): total = '' if t == Inventory: total = sum_inventories([getattr(r, label) for r in rows]) - total = total.reduce(convert.convert_position, accapi.get_operating_currencies()[0], + total = total.reduce(convert_position, accapi.get_operating_currencies()[0], accapi.build_price_map()) elif t == decimal.Decimal: total = sum([getattr(r, label) for r in rows]) diff --git a/fava_investor/modules/assetalloc_class/libassetalloc.py b/fava_investor/modules/assetalloc_class/libassetalloc.py index c10b2cd..0e9ad23 100644 --- a/fava_investor/modules/assetalloc_class/libassetalloc.py +++ b/fava_investor/modules/assetalloc_class/libassetalloc.py @@ -5,6 +5,7 @@ import collections import re +from fava.core.conversion import convert_position from beancount.core import convert from beancount.core import inventory from beancount.core import position @@ -100,7 +101,7 @@ def bucketize(vbalance, accapi): # what we want is the conversion to be done on the end date, or on a date # closest to it, either earlier or later. convert_position does this via bisect - amount = convert.convert_position(pos, base_currency, price_map, date=end_date) + amount = convert_position(pos, base_currency, price_map, date=end_date) if amount.currency == pos.units.currency and amount.currency != base_currency: # Ideally, we would automatically figure out the currency to hop via, based on the cost # currency of the position. However, with vbalance, cost currency info is not diff --git a/fava_investor/modules/summarizer/libsummarizer.py b/fava_investor/modules/summarizer/libsummarizer.py index d813041..44ac55d 100644 --- a/fava_investor/modules/summarizer/libsummarizer.py +++ b/fava_investor/modules/summarizer/libsummarizer.py @@ -8,6 +8,7 @@ from beancount.core import realization from beancount.core import convert from fava_investor.common.libinvestor import build_table_footer +from fava.core.conversion import convert_position # TODO: diff --git a/fava_investor/templates/Investor.html b/fava_investor/templates/Investor.html index 3c4dcf1..540288e 100644 --- a/fava_investor/templates/Investor.html +++ b/fava_investor/templates/Investor.html @@ -1,5 +1,4 @@ {% import "_query_table.html" as querytable with context %} -{% import "_charts.html" as charts with context %} {% set new_querytable = extension.use_new_querytable() %} {% set module = request.args.get('module') %} @@ -11,7 +10,7 @@ ('summarizer', _('Summarizer')), ('minimizegains', _('Gains Minimizer')) ] %} -

{% if not (module == key) %}{{ label }}{% else %} {{ label }}{% endif %}

+

{% if not (module == key) %}{{ label }}{% else %} {{ label }}{% endif %}

{% endfor %} @@ -23,8 +22,6 @@

{% if not (module == key) %} - {% macro table_list_renderer(title, tables) -%}

{{ title }}

@@ -206,7 +203,7 @@

What not to buy

{% endmacro %} {% macro asset_allocation_hierarchy(serialised_tree, label='Asset Allocation') %} -{% do charts.chart_data.append({ +{% do chart_data.append({ 'type': 'hierarchy', 'label': label, 'data': { @@ -219,10 +216,11 @@

What not to buy

{% if (module == 'aa_class') %}

Portfolio: Asset Allocation by Class

+ {% set chart_data = [] %} {% set results = extension.build_assetalloc_by_class() %} {{ asset_allocation_hierarchy(results[0].serialise(results[0]['currency']), label='Asset Allocation') }} - + {{ asset_tree(results[0]) }} From 8a7f82918b7d849c7b41196100f20e52160dda2f Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Tue, 22 Aug 2023 23:49:12 -0700 Subject: [PATCH 2/4] Fix commodities --- fava_investor/common/beancountinvestorapi.py | 2 +- fava_investor/common/favainvestorapi.py | 2 +- fava_investor/modules/cashdrag/libcashdrag.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fava_investor/common/beancountinvestorapi.py b/fava_investor/common/beancountinvestorapi.py index 7d36ec4..7931dc6 100644 --- a/fava_investor/common/beancountinvestorapi.py +++ b/fava_investor/common/beancountinvestorapi.py @@ -48,7 +48,7 @@ def get_operating_currencies(self): def get_operating_currencies_regex(self): currencies = self.get_operating_currencies() - return '(' + '|'.join(currencies) + ')' + return '(' + '|'.join(map(lambda cur: f'^cur$', currencies)) + ')' def get_account_open_close(self): return getters.get_account_open_close(self.entries) diff --git a/fava_investor/common/favainvestorapi.py b/fava_investor/common/favainvestorapi.py index 8e1e07a..a27d81d 100644 --- a/fava_investor/common/favainvestorapi.py +++ b/fava_investor/common/favainvestorapi.py @@ -39,7 +39,7 @@ def get_operating_currencies(self): def get_operating_currencies_regex(self): currencies = self.get_operating_currencies() - return '(' + '|'.join(currencies) + ')' + return '(' + '|'.join(map(lambda cur: f'^cur$', currencies)) + ')' def get_account_open_close(self): return getters.get_account_open_close(g.filtered.entries) diff --git a/fava_investor/modules/cashdrag/libcashdrag.py b/fava_investor/modules/cashdrag/libcashdrag.py index 7a541fe..b68abc5 100644 --- a/fava_investor/modules/cashdrag/libcashdrag.py +++ b/fava_investor/modules/cashdrag/libcashdrag.py @@ -16,7 +16,7 @@ def find_cash_commodities(accapi, options): operating_currencies = accapi.get_operating_currencies() cash_commodities += operating_currencies cash_commodities = set(cash_commodities) - commodities_pattern = '(' + '|'.join(cash_commodities) + ')' + commodities_pattern = '(' + '|'.join(map(lambda cur: f'^{cur}$', cash_commodities)) + ')' return commodities_pattern, operating_currencies[0] From c768a9f922ce94f01eddd29200aab839fbffe9a5 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Tue, 22 Aug 2023 22:35:06 -0700 Subject: [PATCH 3/4] Support dev installs --- fava_investor/__init__.py | 2 ++ fava_investor/common/favainvestorapi.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/fava_investor/__init__.py b/fava_investor/__init__.py index 542ed42..4e034b1 100644 --- a/fava_investor/__init__.py +++ b/fava_investor/__init__.py @@ -62,6 +62,8 @@ def use_new_querytable(self): we have to detect the version and adjust how we call it from inside our template """ + if "dev" in fava_version: + return True split_version = fava_version.split('.') if len(split_version) != 2: split_version = split_version[:2] diff --git a/fava_investor/common/favainvestorapi.py b/fava_investor/common/favainvestorapi.py index a27d81d..8998a10 100644 --- a/fava_investor/common/favainvestorapi.py +++ b/fava_investor/common/favainvestorapi.py @@ -28,7 +28,7 @@ def root_tree(self): def query_func(self, sql): # Based on the fava version, determine if we need to add a new # positional argument to fava's execute_query() - if version.parse(fava_version) >= version.parse("1.22"): + if version.parse(fava_version) >= version.parse("1.22") or "dev" in fava_version: _, rtypes, rrows = g.ledger.query_shell.execute_query(g.filtered.entries, sql) else: _, rtypes, rrows = g.ledger.query_shell.execute_query(sql) From d7b3c5db78a4fa54397cf146edef8aff25ffd26b Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Fri, 1 Sep 2023 02:13:51 -0700 Subject: [PATCH 4/4] Remove unnecessary f-strings --- fava_investor/common/beancountinvestorapi.py | 2 +- fava_investor/common/favainvestorapi.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fava_investor/common/beancountinvestorapi.py b/fava_investor/common/beancountinvestorapi.py index 7931dc6..2de4029 100644 --- a/fava_investor/common/beancountinvestorapi.py +++ b/fava_investor/common/beancountinvestorapi.py @@ -48,7 +48,7 @@ def get_operating_currencies(self): def get_operating_currencies_regex(self): currencies = self.get_operating_currencies() - return '(' + '|'.join(map(lambda cur: f'^cur$', currencies)) + ')' + return '(' + '|'.join(map(lambda cur: '^cur$', currencies)) + ')' def get_account_open_close(self): return getters.get_account_open_close(self.entries) diff --git a/fava_investor/common/favainvestorapi.py b/fava_investor/common/favainvestorapi.py index 8998a10..3af2666 100644 --- a/fava_investor/common/favainvestorapi.py +++ b/fava_investor/common/favainvestorapi.py @@ -39,7 +39,7 @@ def get_operating_currencies(self): def get_operating_currencies_regex(self): currencies = self.get_operating_currencies() - return '(' + '|'.join(map(lambda cur: f'^cur$', currencies)) + ')' + return '(' + '|'.join(map(lambda cur: '^cur$', currencies)) + ')' def get_account_open_close(self): return getters.get_account_open_close(g.filtered.entries)