From 46ea394422e086e589843de3eb00430d88cf05a7 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Mon, 30 Dec 2024 16:35:03 +0100 Subject: [PATCH] Allow orphan transactions Closes #18 --- nummi/main/templates/main/confirm_delete.html | 1 + nummi/statement/forms.py | 4 +- .../templates/statement/confirm_delete.html | 5 +++ nummi/statement/views.py | 1 + ...0003_alter_transaction_account_and_more.py | 38 +++++++++++++++++++ nummi/transaction/models.py | 12 ++++-- 6 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 nummi/statement/templates/statement/confirm_delete.html create mode 100644 nummi/transaction/migrations/0003_alter_transaction_account_and_more.py diff --git a/nummi/main/templates/main/confirm_delete.html b/nummi/main/templates/main/confirm_delete.html index 19dd77b..db59293 100644 --- a/nummi/main/templates/main/confirm_delete.html +++ b/nummi/main/templates/main/confirm_delete.html @@ -14,6 +14,7 @@

{% blocktranslate %}Are you sure you want do delete {{ object }} ?{% endblocktranslate %}

+ {% block additionalinfo %}{% endblock %} {{ form }}
{% translate "Cancel" %} diff --git a/nummi/statement/forms.py b/nummi/statement/forms.py index 328d921..8467680 100644 --- a/nummi/statement/forms.py +++ b/nummi/statement/forms.py @@ -23,7 +23,9 @@ class StatementForm(NummiForm): self.fields["transactions"] = forms.MultipleChoiceField( choices=( ((_transaction.id), _transaction) - for _transaction in Transaction.objects.filter(user=_user) + for _transaction in Transaction.objects.filter( + user=_user, statement=None + ) ), label=_("Add transactions"), required=False, diff --git a/nummi/statement/templates/statement/confirm_delete.html b/nummi/statement/templates/statement/confirm_delete.html new file mode 100644 index 0000000..ebdaf4b --- /dev/null +++ b/nummi/statement/templates/statement/confirm_delete.html @@ -0,0 +1,5 @@ +{% extends "main/confirm_delete.html" %} +{% load i18n %} +{% block additionalinfo %} +

{% blocktranslate %}This will delete all transactions in this statement.{% endblocktranslate %}

+{% endblock %} diff --git a/nummi/statement/views.py b/nummi/statement/views.py index 435cb85..4d393d2 100644 --- a/nummi/statement/views.py +++ b/nummi/statement/views.py @@ -52,6 +52,7 @@ class StatementUpdateView(NummiUpdateView): class StatementDeleteView(NummiDeleteView): + template_name = "statement/confirm_delete.html" model = Statement pk_url_kwarg = "statement" diff --git a/nummi/transaction/migrations/0003_alter_transaction_account_and_more.py b/nummi/transaction/migrations/0003_alter_transaction_account_and_more.py new file mode 100644 index 0000000..6346f5a --- /dev/null +++ b/nummi/transaction/migrations/0003_alter_transaction_account_and_more.py @@ -0,0 +1,38 @@ +# Generated by Django 4.2 on 2024-12-30 15:29 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("statement", "0002_alter_statement_table"), + ("account", "0003_account_archived"), + ("transaction", "0002_alter_invoice_table_alter_transaction_table"), + ] + + operations = [ + migrations.AlterField( + model_name="transaction", + name="account", + field=models.ForeignKey( + blank=True, + editable=False, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to="account.account", + verbose_name="Account", + ), + ), + migrations.AlterField( + model_name="transaction", + name="statement", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to="statement.statement", + verbose_name="Statement", + ), + ), + ] diff --git a/nummi/transaction/models.py b/nummi/transaction/models.py index 46cdd20..f831f22 100644 --- a/nummi/transaction/models.py +++ b/nummi/transaction/models.py @@ -40,11 +40,15 @@ class Transaction(UserModel): statement = models.ForeignKey( Statement, on_delete=models.CASCADE, + blank=True, + null=True, verbose_name=_("Statement"), ) account = models.ForeignKey( Account, on_delete=models.CASCADE, + blank=True, + null=True, verbose_name=_("Account"), editable=False, ) @@ -54,11 +58,13 @@ class Transaction(UserModel): prev_self = Transaction.objects.get(pk=self.pk) else: prev_self = None - self.account = self.statement.account + if self.statement: + self.account = self.statement.account super().save(*args, **kwargs) - if prev_self is not None: + if prev_self is not None and prev_self.statement: prev_self.statement.update_sum() - self.statement.update_sum() + if self.statement: + self.statement.update_sum() def __str__(self): return f"{self.name}"