Replaced the _set_object() method, the _get_object() method, and the _object property with the object property in FormView, and applied the object in the get_success_url().
This commit is contained in:
parent
c72d550cd4
commit
71989f424c
@ -790,7 +790,7 @@ class TransactionView(DetailView):
|
|||||||
return self.kwargs["txn"]
|
return self.kwargs["txn"]
|
||||||
|
|
||||||
def get_template_names(self):
|
def get_template_names(self):
|
||||||
model_name = self.get_object().__class__.__name__.lower()
|
model_name = self.object.__class__.__name__.lower()
|
||||||
txn_type = self.kwargs["txn_type"]
|
txn_type = self.kwargs["txn_type"]
|
||||||
return [F"accounting/{model_name}_{txn_type}_detail.html"]
|
return [F"accounting/{model_name}_{txn_type}_detail.html"]
|
||||||
|
|
||||||
@ -825,7 +825,7 @@ class TransactionFormView(FormView):
|
|||||||
"""Creates and returns the form from the POST data."""
|
"""Creates and returns the form from the POST data."""
|
||||||
form = TransactionForm(post)
|
form = TransactionForm(post)
|
||||||
form.txn_type = self.txn_type
|
form.txn_type = self.txn_type
|
||||||
form.transaction = self.get_object()
|
form.transaction = self.object
|
||||||
return form
|
return form
|
||||||
|
|
||||||
def make_form_from_model(self, obj: Transaction) -> TransactionForm:
|
def make_form_from_model(self, obj: Transaction) -> TransactionForm:
|
||||||
@ -846,7 +846,7 @@ class TransactionFormView(FormView):
|
|||||||
def get_success_url(self) -> str:
|
def get_success_url(self) -> str:
|
||||||
"""Returns the URL on success."""
|
"""Returns the URL on success."""
|
||||||
return reverse("accounting:transactions.detail",
|
return reverse("accounting:transactions.detail",
|
||||||
args=[self.txn_type, self.get_object()],
|
args=[self.txn_type, self.object],
|
||||||
current_app=self.request.resolver_match.namespace)
|
current_app=self.request.resolver_match.namespace)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -942,7 +942,7 @@ class AccountFormView(FormView):
|
|||||||
def make_form_from_post(self, post: Dict[str, str]) -> AccountForm:
|
def make_form_from_post(self, post: Dict[str, str]) -> AccountForm:
|
||||||
"""Creates and returns the form from the POST data."""
|
"""Creates and returns the form from the POST data."""
|
||||||
form = AccountForm(post)
|
form = AccountForm(post)
|
||||||
form.account = self.get_object()
|
form.account = self.object
|
||||||
return form
|
return form
|
||||||
|
|
||||||
def make_form_from_model(self, obj: Account) -> AccountForm:
|
def make_form_from_model(self, obj: Account) -> AccountForm:
|
||||||
@ -966,7 +966,7 @@ class AccountFormView(FormView):
|
|||||||
|
|
||||||
def get_success_url(self) -> str:
|
def get_success_url(self) -> str:
|
||||||
"""Returns the URL on success."""
|
"""Returns the URL on success."""
|
||||||
return reverse("accounting:accounts.detail", args=[self.get_object()],
|
return reverse("accounting:accounts.detail", args=[self.object],
|
||||||
current_app=self.request.resolver_match.namespace)
|
current_app=self.request.resolver_match.namespace)
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ from django.views.generic import DeleteView as CoreDeleteView
|
|||||||
from django.views.generic.base import View
|
from django.views.generic.base import View
|
||||||
|
|
||||||
from . import stored_post, utils
|
from . import stored_post, utils
|
||||||
|
from .models import BaseModel
|
||||||
from .utils import UrlBuilder
|
from .utils import UrlBuilder
|
||||||
|
|
||||||
|
|
||||||
@ -48,11 +49,11 @@ class FormView(View):
|
|||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self._object = None
|
self.object: Optional[BaseModel] = None
|
||||||
self._is_object_requested = False
|
|
||||||
|
|
||||||
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
||||||
"""The view to store an accounting transaction."""
|
"""The view to store an accounting transaction."""
|
||||||
|
self.object = self.get_object()
|
||||||
if self.request.method != "POST":
|
if self.request.method != "POST":
|
||||||
return self.get(request, *args, **kwargs)
|
return self.get(request, *args, **kwargs)
|
||||||
else:
|
else:
|
||||||
@ -83,18 +84,6 @@ class FormView(View):
|
|||||||
raise AttributeError("Please defined the model property.")
|
raise AttributeError("Please defined the model property.")
|
||||||
return self.model
|
return self.model
|
||||||
|
|
||||||
def _set_object(self, obj: Model) -> None:
|
|
||||||
"""Sets the current object that we are operating."""
|
|
||||||
self._object = obj
|
|
||||||
self._is_object_requested = True
|
|
||||||
|
|
||||||
def _get_object(self) -> Optional[Model]:
|
|
||||||
"""Returns the current object that we are operating and cached."""
|
|
||||||
if not self._is_object_requested:
|
|
||||||
self._object = self.get_object()
|
|
||||||
self._is_object_requested = True
|
|
||||||
return self._object
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs) -> Dict[str, Any]:
|
def get_context_data(self, **kwargs) -> Dict[str, Any]:
|
||||||
"""Returns the context data for the template."""
|
"""Returns the context data for the template."""
|
||||||
return {self.context_object_name: self.get_form()}
|
return {self.context_object_name: self.get_form()}
|
||||||
@ -105,9 +94,8 @@ class FormView(View):
|
|||||||
previous_post = stored_post.get_previous_post(self.request)
|
previous_post = stored_post.get_previous_post(self.request)
|
||||||
if previous_post is not None:
|
if previous_post is not None:
|
||||||
return self.make_form_from_post(previous_post)
|
return self.make_form_from_post(previous_post)
|
||||||
obj = self.get_object()
|
if self.object is not None:
|
||||||
if obj is not None:
|
return self.make_form_from_model(self.object)
|
||||||
return self.make_form_from_model(obj)
|
|
||||||
return self.get_form_class()()
|
return self.get_form_class()()
|
||||||
else:
|
else:
|
||||||
post = self.request.POST.dict()
|
post = self.request.POST.dict()
|
||||||
@ -147,16 +135,14 @@ class FormView(View):
|
|||||||
|
|
||||||
def form_valid(self, form: forms.Form) -> HttpResponseRedirect:
|
def form_valid(self, form: forms.Form) -> HttpResponseRedirect:
|
||||||
"""Handles the action when the POST form is valid."""
|
"""Handles the action when the POST form is valid."""
|
||||||
obj = self.get_object()
|
if self.object is None:
|
||||||
if obj is None:
|
self.object = self._model()
|
||||||
obj = self._model()
|
self.fill_model_from_form(self.object, form)
|
||||||
self._set_object(obj)
|
if isinstance(self.object, DirtyFieldsMixin)\
|
||||||
self.fill_model_from_form(obj, form)
|
and not self.object.is_dirty(check_relationship=True):
|
||||||
if isinstance(obj, DirtyFieldsMixin)\
|
|
||||||
and not obj.is_dirty(check_relationship=True):
|
|
||||||
message = self.get_not_modified_message(form.cleaned_data)
|
message = self.get_not_modified_message(form.cleaned_data)
|
||||||
else:
|
else:
|
||||||
obj.save()
|
self.object.save()
|
||||||
message = self.get_success_message(form.cleaned_data)
|
message = self.get_success_message(form.cleaned_data)
|
||||||
messages.success(self.request, message)
|
messages.success(self.request, message)
|
||||||
return redirect(str(UrlBuilder(self.get_success_url())
|
return redirect(str(UrlBuilder(self.get_success_url())
|
||||||
@ -166,8 +152,7 @@ class FormView(View):
|
|||||||
"""Returns the URL on success."""
|
"""Returns the URL on success."""
|
||||||
if self.success_url is not None:
|
if self.success_url is not None:
|
||||||
return self.success_url
|
return self.success_url
|
||||||
obj = self._get_object()
|
get_absolute_url = getattr(self.object, "get_absolute_url", None)
|
||||||
get_absolute_url = getattr(obj, "get_absolute_url", None)
|
|
||||||
if get_absolute_url is not None:
|
if get_absolute_url is not None:
|
||||||
return get_absolute_url()
|
return get_absolute_url()
|
||||||
raise AttributeError(
|
raise AttributeError(
|
||||||
|
Loading…
Reference in New Issue
Block a user