Split models.py into models and forms

This commit is contained in:
Edgar P. Burkhart 2022-12-29 22:08:20 +01:00
parent 2bd2eca26e
commit ab894867f4
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 66 additions and 56 deletions

60
nummi/main/forms.py Normal file
View file

@ -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)

View file

@ -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)

View file

@ -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,
)