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"]
|
||||
|
||||
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"]
|
||||
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."""
|
||||
form = TransactionForm(post)
|
||||
form.txn_type = self.txn_type
|
||||
form.transaction = self.get_object()
|
||||
form.transaction = self.object
|
||||
return form
|
||||
|
||||
def make_form_from_model(self, obj: Transaction) -> TransactionForm:
|
||||
@ -846,7 +846,7 @@ class TransactionFormView(FormView):
|
||||
def get_success_url(self) -> str:
|
||||
"""Returns the URL on success."""
|
||||
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)
|
||||
|
||||
@property
|
||||
@ -942,7 +942,7 @@ class AccountFormView(FormView):
|
||||
def make_form_from_post(self, post: Dict[str, str]) -> AccountForm:
|
||||
"""Creates and returns the form from the POST data."""
|
||||
form = AccountForm(post)
|
||||
form.account = self.get_object()
|
||||
form.account = self.object
|
||||
return form
|
||||
|
||||
def make_form_from_model(self, obj: Account) -> AccountForm:
|
||||
@ -966,7 +966,7 @@ class AccountFormView(FormView):
|
||||
|
||||
def get_success_url(self) -> str:
|
||||
"""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)
|
||||
|
||||
|
||||
|
@ -32,6 +32,7 @@ from django.views.generic import DeleteView as CoreDeleteView
|
||||
from django.views.generic.base import View
|
||||
|
||||
from . import stored_post, utils
|
||||
from .models import BaseModel
|
||||
from .utils import UrlBuilder
|
||||
|
||||
|
||||
@ -48,11 +49,11 @@ class FormView(View):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self._object = None
|
||||
self._is_object_requested = False
|
||||
self.object: Optional[BaseModel] = None
|
||||
|
||||
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
||||
"""The view to store an accounting transaction."""
|
||||
self.object = self.get_object()
|
||||
if self.request.method != "POST":
|
||||
return self.get(request, *args, **kwargs)
|
||||
else:
|
||||
@ -83,18 +84,6 @@ class FormView(View):
|
||||
raise AttributeError("Please defined the model property.")
|
||||
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]:
|
||||
"""Returns the context data for the template."""
|
||||
return {self.context_object_name: self.get_form()}
|
||||
@ -105,9 +94,8 @@ class FormView(View):
|
||||
previous_post = stored_post.get_previous_post(self.request)
|
||||
if previous_post is not None:
|
||||
return self.make_form_from_post(previous_post)
|
||||
obj = self.get_object()
|
||||
if obj is not None:
|
||||
return self.make_form_from_model(obj)
|
||||
if self.object is not None:
|
||||
return self.make_form_from_model(self.object)
|
||||
return self.get_form_class()()
|
||||
else:
|
||||
post = self.request.POST.dict()
|
||||
@ -147,16 +135,14 @@ class FormView(View):
|
||||
|
||||
def form_valid(self, form: forms.Form) -> HttpResponseRedirect:
|
||||
"""Handles the action when the POST form is valid."""
|
||||
obj = self.get_object()
|
||||
if obj is None:
|
||||
obj = self._model()
|
||||
self._set_object(obj)
|
||||
self.fill_model_from_form(obj, form)
|
||||
if isinstance(obj, DirtyFieldsMixin)\
|
||||
and not obj.is_dirty(check_relationship=True):
|
||||
if self.object is None:
|
||||
self.object = self._model()
|
||||
self.fill_model_from_form(self.object, form)
|
||||
if isinstance(self.object, DirtyFieldsMixin)\
|
||||
and not self.object.is_dirty(check_relationship=True):
|
||||
message = self.get_not_modified_message(form.cleaned_data)
|
||||
else:
|
||||
obj.save()
|
||||
self.object.save()
|
||||
message = self.get_success_message(form.cleaned_data)
|
||||
messages.success(self.request, message)
|
||||
return redirect(str(UrlBuilder(self.get_success_url())
|
||||
@ -166,8 +152,7 @@ class FormView(View):
|
||||
"""Returns the URL on success."""
|
||||
if self.success_url is not None:
|
||||
return self.success_url
|
||||
obj = self._get_object()
|
||||
get_absolute_url = getattr(obj, "get_absolute_url", None)
|
||||
get_absolute_url = getattr(self.object, "get_absolute_url", None)
|
||||
if get_absolute_url is not None:
|
||||
return get_absolute_url()
|
||||
raise AttributeError(
|
||||
|
Loading…
Reference in New Issue
Block a user