Forms update
Add transactions to snapshot Fix snapshot delete
This commit is contained in:
parent
ea9cd1b9b8
commit
0184cba30d
3 changed files with 59 additions and 19 deletions
|
@ -1,4 +1,6 @@
|
|||
from django.forms import ModelForm
|
||||
from django.db import models, transaction
|
||||
from django.forms import CheckboxSelectMultiple, ModelForm, MultipleChoiceField
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from .models import Account, Category, Invoice, Snapshot, Transaction
|
||||
|
||||
|
@ -6,15 +8,17 @@ from .models import Account, Category, Invoice, Snapshot, Transaction
|
|||
class NummiForm(ModelForm):
|
||||
template_name = "main/form/base.html"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs.pop("user", None)
|
||||
def __init__(self, *args, user, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class AccountForm(NummiForm):
|
||||
class Meta:
|
||||
model = Account
|
||||
fields = "__all__"
|
||||
fields = [
|
||||
"name",
|
||||
"icon",
|
||||
]
|
||||
|
||||
|
||||
class CategoryForm(NummiForm):
|
||||
|
@ -29,7 +33,7 @@ class TransactionForm(NummiForm):
|
|||
fields = "__all__"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
_user = kwargs.pop("user")
|
||||
_user = kwargs.get("user")
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields["category"].queryset = Category.objects.filter(user=_user)
|
||||
self.fields["snapshot"].queryset = Snapshot.objects.filter(user=_user)
|
||||
|
@ -49,6 +53,33 @@ class SnapshotForm(NummiForm):
|
|||
fields = "__all__"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
_user = kwargs.pop("user")
|
||||
_user = kwargs.get("user")
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields["account"].queryset = Account.objects.filter(user=_user)
|
||||
self.fields["transactions"] = MultipleChoiceField(
|
||||
choices=(
|
||||
((transaction.id), transaction)
|
||||
for transaction in Transaction.objects.filter(user=_user)
|
||||
),
|
||||
label=_("Add transactions"),
|
||||
required=False,
|
||||
)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
instance = super().save(*args, **kwargs)
|
||||
new_transactions = Transaction.objects.filter(
|
||||
id__in=self.cleaned_data["transactions"]
|
||||
)
|
||||
|
||||
to_update = list(
|
||||
new_transactions.order_by().only("snapshot").distinct("snapshot")
|
||||
)
|
||||
|
||||
instance.account.transaction_set.add(*new_transactions)
|
||||
instance.transaction_set.add(*new_transactions)
|
||||
instance.update_sum()
|
||||
|
||||
for upd in to_update:
|
||||
upd.snapshot.update_sum()
|
||||
|
||||
return instance
|
||||
|
|
|
@ -165,9 +165,10 @@ class Snapshot(AccountModel):
|
|||
self.sum = self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0)
|
||||
super().save()
|
||||
|
||||
def delete(self, *args, only_super=False, **kwargs):
|
||||
def delete(self, *args, **kwargs):
|
||||
if self.file:
|
||||
self.file.delete()
|
||||
super().delete(*args, **kwargs)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse("snapshot", kwargs={"pk": self.pk})
|
||||
|
@ -218,8 +219,10 @@ class Transaction(CustomModel):
|
|||
)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
prev_self = Transaction.objects.get(pk=self.pk)
|
||||
self.account = self.snapshot.account
|
||||
super().save(*args, **kwargs)
|
||||
prev_self.snapshot.update_sum()
|
||||
self.snapshot.update_sum()
|
||||
|
||||
def __str__(self):
|
||||
|
|
|
@ -38,16 +38,22 @@ class UserMixin(LoginRequiredMixin):
|
|||
def get_queryset(self, **kwargs):
|
||||
return super().get_queryset().filter(user=self.request.user)
|
||||
|
||||
|
||||
class UserFormMixin:
|
||||
def get_form_kwargs(self):
|
||||
return super().get_form_kwargs() | {"user": self.request.user}
|
||||
|
||||
|
||||
class UserCreateView(UserMixin, CreateView):
|
||||
class NummiCreateView(UserMixin, UserFormMixin, CreateView):
|
||||
def form_valid(self, form):
|
||||
form.instance.user = self.request.user
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class NummiUpdateView(UserMixin, UserFormMixin, UpdateView):
|
||||
pass
|
||||
|
||||
|
||||
class NummiDeleteView(UserMixin, DeleteView):
|
||||
template_name = "main/confirm_delete.html"
|
||||
success_url = reverse_lazy("index")
|
||||
|
@ -69,17 +75,17 @@ class TransactionListView(UserMixin, ListView):
|
|||
context_object_name = "transactions"
|
||||
|
||||
|
||||
class AccountCreateView(UserCreateView):
|
||||
class AccountCreateView(NummiCreateView):
|
||||
model = Account
|
||||
form_class = AccountForm
|
||||
|
||||
|
||||
class TransactionCreateView(UserCreateView):
|
||||
class TransactionCreateView(NummiCreateView):
|
||||
model = Transaction
|
||||
form_class = TransactionForm
|
||||
|
||||
|
||||
class InvoiceCreateView(UserCreateView):
|
||||
class InvoiceCreateView(NummiCreateView):
|
||||
model = Invoice
|
||||
form_class = InvoiceForm
|
||||
|
||||
|
@ -94,27 +100,27 @@ class InvoiceCreateView(UserCreateView):
|
|||
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
|
||||
|
||||
|
||||
class CategoryCreateView(UserCreateView):
|
||||
class CategoryCreateView(NummiCreateView):
|
||||
model = Category
|
||||
form_class = CategoryForm
|
||||
|
||||
|
||||
class SnapshotCreateView(UserCreateView):
|
||||
class SnapshotCreateView(NummiCreateView):
|
||||
model = Snapshot
|
||||
form_class = SnapshotForm
|
||||
|
||||
|
||||
class AccountUpdateView(UserMixin, UpdateView):
|
||||
class AccountUpdateView(NummiUpdateView):
|
||||
model = Account
|
||||
form_class = AccountForm
|
||||
|
||||
|
||||
class TransactionUpdateView(UserMixin, UpdateView):
|
||||
class TransactionUpdateView(NummiUpdateView):
|
||||
model = Transaction
|
||||
form_class = TransactionForm
|
||||
|
||||
|
||||
class InvoiceUpdateView(UserMixin, UpdateView):
|
||||
class InvoiceUpdateView(NummiUpdateView):
|
||||
model = Invoice
|
||||
form_class = InvoiceForm
|
||||
|
||||
|
@ -133,12 +139,12 @@ class InvoiceUpdateView(UserMixin, UpdateView):
|
|||
)
|
||||
|
||||
|
||||
class CategoryUpdateView(UserMixin, UpdateView):
|
||||
class CategoryUpdateView(NummiUpdateView):
|
||||
model = Category
|
||||
form_class = CategoryForm
|
||||
|
||||
|
||||
class SnapshotUpdateView(UserMixin, UpdateView):
|
||||
class SnapshotUpdateView(NummiUpdateView):
|
||||
model = Snapshot
|
||||
form_class = SnapshotForm
|
||||
|
||||
|
@ -171,7 +177,7 @@ class SnapshotDeleteView(NummiDeleteView):
|
|||
model = Snapshot
|
||||
|
||||
|
||||
class SearchView(UserMixin, ListView, ProcessFormView):
|
||||
class SearchView(UserMixin, UserFormMixin, ListView):
|
||||
paginate_by = 24
|
||||
model = Transaction
|
||||
template_name = "main/transactions.html"
|
||||
|
|
Loading…
Reference in a new issue