diff --git a/nummi/main/forms.py b/nummi/main/forms.py new file mode 100644 index 0000000..a32bf40 --- /dev/null +++ b/nummi/main/forms.py @@ -0,0 +1,60 @@ +from django.forms import ModelForm + +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) + super().__init__(*args, **kwargs) + + +class AccountForm(NummiForm): + class Meta: + model = Account + fields = "__all__" + + +class CategoryForm(NummiForm): + class Meta: + model = Category + fields = "__all__" + + +class TransactionForm(NummiForm): + class Meta: + model = Transaction + fields = "__all__" + + def __init__(self, *args, **kwargs): + _user = kwargs.pop("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 = "__all__" + + +class SnapshotForm(NummiForm): + class Meta: + model = Snapshot + fields = "__all__" + + def __init__(self, *args, **kwargs): + _user = kwargs.pop("user") + super().__init__(*args, **kwargs) + self.fields["account"].queryset = Account.objects.filter(user=_user) diff --git a/nummi/main/models.py b/nummi/main/models.py index 0891885..3ddc8f8 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -5,7 +5,6 @@ import uuid from django.conf import settings from django.core.validators import FileExtensionValidator from django.db import models, transaction -from django.forms import ModelForm from django.urls import reverse from django.utils.translation import gettext as _ @@ -279,54 +278,3 @@ class Invoice(CustomModel): class Meta: verbose_name = _("Invoice") verbose_name_plural = _("Invoices") - - -class NummiForm(ModelForm): - template_name = "main/form/base.html" - - def __init__(self, *args, **kwargs): - kwargs.pop("user", None) - super().__init__(*args, **kwargs) - - -class AccountForm(NummiForm): - class Meta: - model = Account - fields = "__all__" - - -class CategoryForm(NummiForm): - class Meta: - model = Category - fields = "__all__" - - -class TransactionForm(NummiForm): - class Meta: - model = Transaction - fields = "__all__" - - def __init__(self, *args, **kwargs): - _user = kwargs.pop("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 = "__all__" - - -class SnapshotForm(NummiForm): - class Meta: - model = Snapshot - fields = "__all__" - - def __init__(self, *args, **kwargs): - _user = kwargs.pop("user") - super().__init__(*args, **kwargs) - self.fields["account"].queryset = Account.objects.filter(user=_user) diff --git a/nummi/main/views.py b/nummi/main/views.py index 36da9d3..2f1486d 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -20,14 +20,16 @@ from django.views.generic.edit import ProcessFormView from .models import ( Account, - AccountForm, Category, - CategoryForm, Invoice, - InvoiceForm, Snapshot, - SnapshotForm, Transaction, +) +from .forms import ( + AccountForm, + CategoryForm, + InvoiceForm, + SnapshotForm, TransactionForm, )