97 lines
2.5 KiB
Python
97 lines
2.5 KiB
Python
from django import forms
|
|
from django.db import models, transaction
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from .models import Account, Category, Invoice, Snapshot, Transaction
|
|
|
|
|
|
class NummiForm(forms.ModelForm):
|
|
template_name = "main/form/base.html"
|
|
|
|
def __init__(self, *args, user, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
class AccountForm(NummiForm):
|
|
class Meta:
|
|
model = Account
|
|
fields = [
|
|
"name",
|
|
"icon",
|
|
"default",
|
|
]
|
|
|
|
|
|
class CategoryForm(NummiForm):
|
|
class Meta:
|
|
model = Category
|
|
fields = [
|
|
"name",
|
|
"icon",
|
|
]
|
|
|
|
|
|
class TransactionForm(NummiForm):
|
|
class Meta:
|
|
model = Transaction
|
|
fields = [
|
|
"snapshot",
|
|
"name",
|
|
"value",
|
|
"date",
|
|
"real_date",
|
|
"category",
|
|
"trader",
|
|
"payment",
|
|
"description",
|
|
]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
_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)
|
|
|
|
|
|
class InvoiceForm(NummiForm):
|
|
prefix = "invoice"
|
|
|
|
class Meta:
|
|
model = Invoice
|
|
fields = [
|
|
"name",
|
|
"file",
|
|
]
|
|
|
|
|
|
class SnapshotForm(NummiForm):
|
|
class Meta:
|
|
model = Snapshot
|
|
fields = ["account", "start_date", "date", "start_value", "value", "file"]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
_user = kwargs.get("user")
|
|
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,
|
|
)
|
|
|
|
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
|
|
|
|
|
|
class SearchForm(forms.Form):
|
|
template_name = "main/form/search.html"
|
|
search = forms.CharField(label=_("Search"), max_length=128)
|