From 1abe9e9f62b3ceb719c529945df4bc5c97a5cf10 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Fri, 29 Aug 2025 22:47:00 -0300 Subject: [PATCH] feat(currencies): allow archiving --- app/apps/accounts/forms.py | 20 +++++++++++++++++++ app/apps/currencies/forms.py | 2 ++ .../migrations/0022_currency_is_archived.py | 18 +++++++++++++++++ app/apps/currencies/models.py | 5 +++++ app/apps/insights/utils/transactions.py | 2 ++ .../net_worth/utils/calculate_net_worth.py | 1 + app/templates/currencies/fragments/list.html | 2 ++ 7 files changed, 50 insertions(+) create mode 100644 app/apps/currencies/migrations/0022_currency_is_archived.py diff --git a/app/apps/accounts/forms.py b/app/apps/accounts/forms.py index 54bc3f2..e5798e2 100644 --- a/app/apps/accounts/forms.py +++ b/app/apps/accounts/forms.py @@ -3,6 +3,7 @@ from crispy_forms.bootstrap import FormActions from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Field, Column, Row from django import forms +from django.db.models import Q from django.utils.translation import gettext_lazy as _ from apps.accounts.models import Account @@ -15,6 +16,7 @@ from apps.common.widgets.crispy.submit import NoClassSubmit from apps.common.widgets.tom_select import TomSelect from apps.transactions.models import TransactionCategory, TransactionTag from apps.common.widgets.decimal import ArbitraryDecimalDisplayNumberInput +from apps.currencies.models import Currency class AccountGroupForm(forms.ModelForm): @@ -79,6 +81,24 @@ class AccountForm(forms.ModelForm): self.fields["group"].queryset = AccountGroup.objects.all() + if self.instance.id: + self.fields["currency"].queryset = Currency.objects.filter( + Q(is_archived=False) | Q(accounts=self.instance.id), + ) + + self.fields["exchange_currency"].queryset = Currency.objects.filter( + Q(is_archived=False) | Q(accounts=self.instance.id) + ) + + else: + self.fields["currency"].queryset = Currency.objects.filter( + Q(is_archived=False), + ) + + self.fields["exchange_currency"].queryset = Currency.objects.filter( + Q(is_archived=False) + ) + self.helper = FormHelper() self.helper.form_tag = False self.helper.form_method = "post" diff --git a/app/apps/currencies/forms.py b/app/apps/currencies/forms.py index eade665..cab3120 100644 --- a/app/apps/currencies/forms.py +++ b/app/apps/currencies/forms.py @@ -26,6 +26,7 @@ class CurrencyForm(forms.ModelForm): "suffix", "code", "exchange_currency", + "is_archived", ] widgets = { "exchange_currency": TomSelect(), @@ -40,6 +41,7 @@ class CurrencyForm(forms.ModelForm): self.helper.layout = Layout( "code", "name", + Switch("is_archived"), "decimal_places", "prefix", "suffix", diff --git a/app/apps/currencies/migrations/0022_currency_is_archived.py b/app/apps/currencies/migrations/0022_currency_is_archived.py new file mode 100644 index 0000000..753ad33 --- /dev/null +++ b/app/apps/currencies/migrations/0022_currency_is_archived.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2.5 on 2025-08-30 00:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('currencies', '0021_alter_exchangerateservice_service_type'), + ] + + operations = [ + migrations.AddField( + model_name='currency', + name='is_archived', + field=models.BooleanField(default=False, verbose_name='Archived'), + ), + ] diff --git a/app/apps/currencies/models.py b/app/apps/currencies/models.py index 286a87f..95ce0e1 100644 --- a/app/apps/currencies/models.py +++ b/app/apps/currencies/models.py @@ -32,6 +32,11 @@ class Currency(models.Model): help_text=_("Default currency for exchange calculations"), ) + is_archived = models.BooleanField( + default=False, + verbose_name=_("Archived"), + ) + def __str__(self): return self.name diff --git a/app/apps/insights/utils/transactions.py b/app/apps/insights/utils/transactions.py index fbfbc0a..8455075 100644 --- a/app/apps/insights/utils/transactions.py +++ b/app/apps/insights/utils/transactions.py @@ -102,4 +102,6 @@ def get_transactions( account__in=request.user.untracked_accounts.all() ) + transactions = transactions.exclude(account__currency__is_archived=True) + return transactions diff --git a/app/apps/net_worth/utils/calculate_net_worth.py b/app/apps/net_worth/utils/calculate_net_worth.py index 74fff63..7dce4a4 100644 --- a/app/apps/net_worth/utils/calculate_net_worth.py +++ b/app/apps/net_worth/utils/calculate_net_worth.py @@ -30,6 +30,7 @@ def calculate_historical_currency_net_worth(queryset): | Q(accounts__visibility="private", accounts__owner=None), accounts__is_archived=False, accounts__isnull=False, + is_archived=False, ) .values_list("name", flat=True) .distinct() diff --git a/app/templates/currencies/fragments/list.html b/app/templates/currencies/fragments/list.html index 5a3add8..db4d6b2 100644 --- a/app/templates/currencies/fragments/list.html +++ b/app/templates/currencies/fragments/list.html @@ -24,6 +24,7 @@ {% translate 'Code' %} {% translate 'Name' %} + {% translate 'Archived' %} @@ -53,6 +54,7 @@ {{ currency.code }} {{ currency.name }} + {% if currency.is_archived %}{% endif %} {% endfor %}