mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-19 23:28:54 +01:00
fix(export): back up asset classes and investment income links
Restore asset classes before accounts and take a second pass over accounts once categories, tags and entities exist, so their links survive a restore into a fresh install.
This commit is contained in:
1 parent
7038ce8e18
commit
494937870e
4 files changed
+84
-3
No files matched your search
@@ -101,6 +101,7 @@ class ExportForm(forms.Form):
|
||||
self.helper.layout = Layout(
|
||||
"users",
|
||||
"accounts",
|
||||
"investment_classes",
|
||||
"currencies",
|
||||
"transactions",
|
||||
"categories",
|
||||
@@ -127,6 +128,7 @@ class RestoreForm(forms.Form):
|
||||
)
|
||||
users = forms.FileField(required=False, label=_("Users"))
|
||||
accounts = forms.FileField(required=False, label=_("Accounts"))
|
||||
investment_classes = forms.FileField(required=False, label=_("Asset classes"))
|
||||
currencies = forms.FileField(required=False, label=_("Currencies"))
|
||||
transactions_categories = forms.FileField(required=False, label=_("Categories"))
|
||||
transactions_tags = forms.FileField(required=False, label=_("Tags"))
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
from import_export import fields, resources, widgets
|
||||
|
||||
from apps.accounts.models import Account, AccountGroup
|
||||
from apps.accounts.models import Account, AccountGroup, InvestmentClass
|
||||
from apps.export_app.widgets.foreign_key import AutoCreateForeignKeyWidget
|
||||
from apps.currencies.models import Currency
|
||||
|
||||
|
||||
class InvestmentClassResource(resources.ModelResource):
|
||||
class Meta:
|
||||
model = InvestmentClass
|
||||
|
||||
def get_queryset(self):
|
||||
return InvestmentClass.all_objects.all()
|
||||
|
||||
|
||||
class AllClassesWidget(widgets.ForeignKeyWidget):
|
||||
# By id, across owners: a shared account may carry another user's class
|
||||
def get_queryset(self, value, row, *args, **kwargs):
|
||||
return InvestmentClass.all_objects.all()
|
||||
|
||||
|
||||
class AccountResource(resources.ModelResource):
|
||||
group = fields.Field(
|
||||
attribute="group",
|
||||
@@ -22,6 +36,12 @@ class AccountResource(resources.ModelResource):
|
||||
widget=widgets.ForeignKeyWidget(Currency, "name"),
|
||||
)
|
||||
|
||||
investment_class = fields.Field(
|
||||
attribute="investment_class",
|
||||
column_name="investment_class",
|
||||
widget=AllClassesWidget(InvestmentClass),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Account
|
||||
|
||||
|
||||
@@ -1,3 +1,53 @@
|
||||
import io
|
||||
import zipfile
|
||||
from decimal import Decimal
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from apps.accounts.models import Account, InvestmentClass
|
||||
from apps.common.middleware.thread_local import delete_current_user, write_current_user
|
||||
from apps.currencies.models import Currency
|
||||
from apps.export_app.resources.accounts import AccountResource, InvestmentClassResource
|
||||
from apps.export_app.resources.transactions import TransactionEntityResource
|
||||
from apps.export_app.views import process_imports
|
||||
from apps.transactions.models import TransactionEntity
|
||||
|
||||
|
||||
class InvestmentRestoreTests(TestCase):
|
||||
def setUp(self):
|
||||
self.user = get_user_model().objects.create_user(email="u@test.com", password="pw")
|
||||
write_current_user(self.user)
|
||||
|
||||
def tearDown(self):
|
||||
delete_current_user()
|
||||
|
||||
def test_round_trip_keeps_asset_class_and_income_links(self):
|
||||
brl = Currency.objects.create(code="BRL", name="Real", decimal_places=2)
|
||||
stocks = InvestmentClass.objects.create(name="Stocks", target=Decimal("60"))
|
||||
broker = TransactionEntity.objects.create(name="Broker")
|
||||
account = Account.objects.create(
|
||||
name="PETR4", currency=brl, is_investment=True, investment_class=stocks
|
||||
)
|
||||
account.income_entities.add(broker)
|
||||
|
||||
backup = io.BytesIO()
|
||||
with zipfile.ZipFile(backup, "w") as z:
|
||||
for resource, name in (
|
||||
(AccountResource, "accounts"),
|
||||
(InvestmentClassResource, "investment_classes"),
|
||||
(TransactionEntityResource, "transactions_entities"),
|
||||
):
|
||||
z.writestr(f"{name}.csv", resource().export().csv)
|
||||
|
||||
Account.all_objects.all().delete()
|
||||
InvestmentClass.all_objects.all().delete()
|
||||
TransactionEntity.all_objects.all().delete()
|
||||
|
||||
backup.seek(0)
|
||||
process_imports(None, {"zip_file": backup})
|
||||
|
||||
account = Account.all_objects.get(name="PETR4")
|
||||
self.assertEqual(account.investment_class.name, "Stocks")
|
||||
self.assertEqual(account.investment_class.target, Decimal("60"))
|
||||
self.assertEqual([e.name for e in account.income_entities.all()], ["Broker"])
|
||||
@@ -14,7 +14,10 @@ from tablib import Dataset
|
||||
|
||||
from apps.common.decorators.htmx import only_htmx
|
||||
from apps.export_app.forms import ExportForm, RestoreForm
|
||||
from apps.export_app.resources.accounts import AccountResource
|
||||
from apps.export_app.resources.accounts import (
|
||||
AccountResource,
|
||||
InvestmentClassResource,
|
||||
)
|
||||
from apps.export_app.resources.currencies import (
|
||||
CurrencyResource,
|
||||
ExchangeRateResource,
|
||||
@@ -91,6 +94,9 @@ def export_form(request):
|
||||
exports.append((UserResource().export(), "users"))
|
||||
if export_accounts:
|
||||
exports.append((AccountResource().export(), "accounts"))
|
||||
exports.append(
|
||||
(InvestmentClassResource().export(), "investment_classes")
|
||||
)
|
||||
if export_currencies:
|
||||
exports.append((CurrencyResource().export(), "currencies"))
|
||||
if export_transactions:
|
||||
@@ -226,10 +232,13 @@ def process_imports(request, cleaned_data):
|
||||
"currencies",
|
||||
CurrencyResource,
|
||||
), # We do a double pass because exchange_currency may not exist when currency is initially created
|
||||
("investment_classes", InvestmentClassResource),
|
||||
("accounts", AccountResource),
|
||||
("transactions_categories", TransactionCategoyResource),
|
||||
("transactions_tags", TransactionTagResource),
|
||||
("transactions_entities", TransactionEntityResource),
|
||||
# Again: income categories, tags and entities don't exist on the first pass
|
||||
("accounts", AccountResource),
|
||||
("automatic_exchange_rates", ExchangeRateServiceResource),
|
||||
("exchange_rates", ExchangeRateResource),
|
||||
("installment_plans", InstallmentPlanResource),
|
||||
|
||||
Reference in new issue
Block a user