Split models.py into models and forms
This commit is contained in:
parent
2bd2eca26e
commit
ab894867f4
3 changed files with 66 additions and 56 deletions
60
nummi/main/forms.py
Normal file
60
nummi/main/forms.py
Normal 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)
|
|
@ -5,7 +5,6 @@ import uuid
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.validators import FileExtensionValidator
|
from django.core.validators import FileExtensionValidator
|
||||||
from django.db import models, transaction
|
from django.db import models, transaction
|
||||||
from django.forms import ModelForm
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
@ -279,54 +278,3 @@ class Invoice(CustomModel):
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _("Invoice")
|
verbose_name = _("Invoice")
|
||||||
verbose_name_plural = _("Invoices")
|
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)
|
|
||||||
|
|
|
@ -20,14 +20,16 @@ from django.views.generic.edit import ProcessFormView
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
Account,
|
Account,
|
||||||
AccountForm,
|
|
||||||
Category,
|
Category,
|
||||||
CategoryForm,
|
|
||||||
Invoice,
|
Invoice,
|
||||||
InvoiceForm,
|
|
||||||
Snapshot,
|
Snapshot,
|
||||||
SnapshotForm,
|
|
||||||
Transaction,
|
Transaction,
|
||||||
|
)
|
||||||
|
from .forms import (
|
||||||
|
AccountForm,
|
||||||
|
CategoryForm,
|
||||||
|
InvoiceForm,
|
||||||
|
SnapshotForm,
|
||||||
TransactionForm,
|
TransactionForm,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue