Disable fixed fields in forms
This commit is contained in:
parent
42c44c8c11
commit
d595d73417
2 changed files with 18 additions and 4 deletions
|
@ -53,9 +53,12 @@ class TransactionForm(NummiForm):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
_user = kwargs.get("user")
|
_user = kwargs.get("user")
|
||||||
|
_disable_snapshot = kwargs.pop("disable_snapshot", False)
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.fields["category"].queryset = Category.objects.filter(user=_user)
|
self.fields["category"].queryset = Category.objects.filter(user=_user)
|
||||||
self.fields["snapshot"].queryset = Snapshot.objects.filter(user=_user)
|
self.fields["snapshot"].queryset = Snapshot.objects.filter(user=_user)
|
||||||
|
if _disable_snapshot:
|
||||||
|
self.fields["snapshot"].disabled = True
|
||||||
|
|
||||||
|
|
||||||
class InvoiceForm(NummiForm):
|
class InvoiceForm(NummiForm):
|
||||||
|
@ -82,6 +85,7 @@ class SnapshotForm(NummiForm):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
_user = kwargs.get("user")
|
_user = kwargs.get("user")
|
||||||
|
_disable_account = kwargs.pop("disable_account", False)
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.fields["account"].queryset = Account.objects.filter(user=_user)
|
self.fields["account"].queryset = Account.objects.filter(user=_user)
|
||||||
self.fields["transactions"] = forms.MultipleChoiceField(
|
self.fields["transactions"] = forms.MultipleChoiceField(
|
||||||
|
@ -92,6 +96,8 @@ class SnapshotForm(NummiForm):
|
||||||
label=_("Add transactions"),
|
label=_("Add transactions"),
|
||||||
required=False,
|
required=False,
|
||||||
)
|
)
|
||||||
|
if _disable_account:
|
||||||
|
self.fields["account"].disabled = True
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
instance = super().save(*args, **kwargs)
|
instance = super().save(*args, **kwargs)
|
||||||
|
|
|
@ -62,15 +62,13 @@ class UserMixin(LoginRequiredMixin):
|
||||||
def get_queryset(self, **kwargs):
|
def get_queryset(self, **kwargs):
|
||||||
return super().get_queryset().filter(user=self.request.user)
|
return super().get_queryset().filter(user=self.request.user)
|
||||||
|
|
||||||
|
|
||||||
class UserFormMixin:
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
return super().get_form_kwargs() | {
|
return super().get_form_kwargs() | {
|
||||||
"user": self.request.user,
|
"user": self.request.user,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class NummiCreateView(UserMixin, UserFormMixin, CreateView):
|
class NummiCreateView(UserMixin, CreateView):
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
form.instance.user = self.request.user
|
form.instance.user = self.request.user
|
||||||
self.next = form.data.get("next", None)
|
self.next = form.data.get("next", None)
|
||||||
|
@ -80,7 +78,7 @@ class NummiCreateView(UserMixin, UserFormMixin, CreateView):
|
||||||
return self.next or super().get_success_url()
|
return self.next or super().get_success_url()
|
||||||
|
|
||||||
|
|
||||||
class NummiUpdateView(UserMixin, UserFormMixin, UpdateView):
|
class NummiUpdateView(UserMixin, UpdateView):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,6 +115,11 @@ class TransactionCreateView(NummiCreateView):
|
||||||
self.snapshot = _queryset.first()
|
self.snapshot = _queryset.first()
|
||||||
return {"snapshot": self.snapshot}
|
return {"snapshot": self.snapshot}
|
||||||
|
|
||||||
|
def get_form_kwargs(self):
|
||||||
|
if "snapshot" in self.kwargs:
|
||||||
|
return super().get_form_kwargs() | {"disable_snapshot": True}
|
||||||
|
return super().get_form_kwargs()
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
if "snapshot" in self.kwargs:
|
if "snapshot" in self.kwargs:
|
||||||
return super().get_context_data(**kwargs) | {"snapshot": self.snapshot}
|
return super().get_context_data(**kwargs) | {"snapshot": self.snapshot}
|
||||||
|
@ -158,6 +161,11 @@ class SnapshotCreateView(NummiCreateView):
|
||||||
self.account = _queryset.first()
|
self.account = _queryset.first()
|
||||||
return {"account": self.account}
|
return {"account": self.account}
|
||||||
|
|
||||||
|
def get_form_kwargs(self):
|
||||||
|
if "account" in self.kwargs:
|
||||||
|
return super().get_form_kwargs() | {"disable_account": True}
|
||||||
|
return super().get_form_kwargs()
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
if "account" in self.kwargs:
|
if "account" in self.kwargs:
|
||||||
return super().get_context_data(**kwargs) | {"account": self.account}
|
return super().get_context_data(**kwargs) | {"account": self.account}
|
||||||
|
|
Loading…
Reference in a new issue