82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
from category.models import Category
|
|
from django.forms.widgets import TextInput
|
|
from main.forms import NummiFileInput, NummiForm
|
|
from statement.models import Statement
|
|
|
|
from .models import Invoice, Transaction
|
|
|
|
|
|
class TransactionForm(NummiForm):
|
|
class Meta:
|
|
model = Transaction
|
|
fields = [
|
|
"statement",
|
|
"name",
|
|
"value",
|
|
"date",
|
|
"real_date",
|
|
"category",
|
|
"trader",
|
|
"payment",
|
|
"description",
|
|
]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
_user = kwargs.get("user")
|
|
_disable_statement = kwargs.pop("disable_statement", False)
|
|
super().__init__(*args, **kwargs)
|
|
self.fields["category"].queryset = Category.objects.filter(user=_user)
|
|
self.fields["statement"].queryset = Statement.objects.filter(user=_user)
|
|
self.fields["name"].widget = DatalistInput(
|
|
options=[
|
|
t.name
|
|
for t in _user.transaction_set.exclude(name=None)
|
|
.order_by("name")
|
|
.distinct("name")
|
|
]
|
|
)
|
|
self.fields["trader"].widget = DatalistInput(
|
|
options=[
|
|
t.trader
|
|
for t in _user.transaction_set.exclude(trader=None)
|
|
.order_by("trader")
|
|
.distinct("trader")
|
|
]
|
|
)
|
|
self.fields["payment"].widget = DatalistInput(
|
|
options=[
|
|
t.payment
|
|
for t in _user.transaction_set.exclude(payment=None)
|
|
.order_by("payment")
|
|
.distinct("payment")
|
|
]
|
|
)
|
|
if _disable_statement:
|
|
self.fields["statement"].disabled = True
|
|
|
|
|
|
class InvoiceForm(NummiForm):
|
|
prefix = "invoice"
|
|
|
|
class Meta:
|
|
model = Invoice
|
|
fields = [
|
|
"name",
|
|
"file",
|
|
]
|
|
widgets = {
|
|
"file": NummiFileInput,
|
|
}
|
|
|
|
|
|
class DatalistInput(TextInput):
|
|
template_name = "transaction/forms/widgets/datalist.html"
|
|
|
|
def __init__(self, *args, options=[]):
|
|
self.options = options
|
|
super().__init__(*args)
|
|
|
|
def get_context(self, *args):
|
|
context = super().get_context(*args)
|
|
context["widget"]["options"] = self.options
|
|
return context
|