41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from account.models import Account
|
|
from django import forms
|
|
from django.utils.translation import gettext_lazy as _
|
|
from main.forms import NummiFileInput, NummiForm
|
|
from transaction.models import Transaction
|
|
|
|
from .models import Statement
|
|
|
|
|
|
class StatementForm(NummiForm):
|
|
class Meta:
|
|
model = Statement
|
|
fields = ["account", "start_date", "date", "start_value", "value", "file"]
|
|
widgets = {
|
|
"file": NummiFileInput,
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
_user = kwargs.get("user")
|
|
_disable_account = kwargs.pop("disable_account", False)
|
|
super().__init__(*args, **kwargs)
|
|
self.fields["account"].queryset = Account.objects.filter(user=_user)
|
|
self.fields["transactions"] = forms.MultipleChoiceField(
|
|
choices=(
|
|
((_transaction.id), _transaction)
|
|
for _transaction in Transaction.objects.filter(user=_user)
|
|
),
|
|
label=_("Add transactions"),
|
|
required=False,
|
|
)
|
|
if _disable_account:
|
|
self.fields["account"].disabled = True
|
|
|
|
def save(self, *args, **kwargs):
|
|
instance = super().save(*args, **kwargs)
|
|
new_transactions = Transaction.objects.filter(
|
|
id__in=self.cleaned_data["transactions"]
|
|
)
|
|
|
|
instance.transaction_set.add(*new_transactions, bulk=False)
|
|
return instance
|