Renamed the get_current_object method of FormView to get_object, to follow the convention of Django form views in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-14 00:44:14 +08:00
parent be2ffbf79e
commit 21c9db396d
2 changed files with 15 additions and 15 deletions

View File

@ -991,7 +991,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_current_object() form.account = self.get_object()
return form return form
def make_form_from_model(self, obj: Account) -> AccountForm: def make_form_from_model(self, obj: Account) -> AccountForm:
@ -1011,11 +1011,11 @@ class AccountFormView(FormView):
def get_error_url(self) -> str: def get_error_url(self) -> str:
"""Returns the URL on error.""" """Returns the URL on error."""
user = self.get_current_object() user = self.get_object()
return reverse("accounting:accounts.create") if user is None\ return reverse("accounting:accounts.create") if user is None\
else reverse("accounting:accounts.edit", args=(user,)) else reverse("accounting:accounts.edit", args=(user,))
def get_current_object(self) -> Optional[Account]: def get_object(self) -> Optional[Account]:
"""Returns the current object, or None on a create form.""" """Returns the current object, or None on a create form."""
if "account" in self.kwargs: if "account" in self.kwargs:
return self.kwargs["account"] return self.kwargs["account"]

View File

@ -69,7 +69,7 @@ class FormView(View):
def do_get(self) -> HttpResponse: def do_get(self) -> HttpResponse:
"""Handles the GET requests.""" """Handles the GET requests."""
obj = self.get_current_object() obj = self.get_object()
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:
form = self.make_form_from_post(previous_post) form = self.make_form_from_post(previous_post)
@ -83,7 +83,7 @@ class FormView(View):
def do_post(self) -> HttpResponseRedirect: def do_post(self) -> HttpResponseRedirect:
"""Handles the POST requests.""" """Handles the POST requests."""
obj = self.get_current_object() obj = self.get_object()
post = self.request.POST.dict() post = self.request.POST.dict()
utils.strip_post(post) utils.strip_post(post)
form = self.make_form_from_post(post) form = self.make_form_from_post(post)
@ -93,7 +93,7 @@ class FormView(View):
return stored_post.error_redirect(self.request, url, post) return stored_post.error_redirect(self.request, url, post)
if obj is None: if obj is None:
obj = self._model() obj = self._model()
self._set_current_object(obj) self._set_object(obj)
self.fill_model_from_form(obj, form) self.fill_model_from_form(obj, form)
if isinstance(obj, DirtyFieldsMixin)\ if isinstance(obj, DirtyFieldsMixin)\
and not obj.is_dirty(check_relationship=True): and not obj.is_dirty(check_relationship=True):
@ -117,15 +117,15 @@ 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_current_object(self, obj: Model) -> None: def _set_object(self, obj: Model) -> None:
"""Sets the current object that we are operating.""" """Sets the current object that we are operating."""
self._object = obj self._object = obj
self._is_object_requested = True self._is_object_requested = True
def _get_current_object(self) -> Optional[Model]: def _get_object(self) -> Optional[Model]:
"""Returns the current object that we are operating and cached.""" """Returns the current object that we are operating and cached."""
if not self._is_object_requested: if not self._is_object_requested:
self._object = self.get_current_object() self._object = self.get_object()
self._is_object_requested = True self._is_object_requested = True
return self._object return self._object
@ -166,7 +166,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_current_object() obj = self._get_object()
get_absolute_url = getattr(obj, "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()
@ -183,7 +183,7 @@ class FormView(View):
"""Returns the success message.""" """Returns the success message."""
return self.success_message return self.success_message
def get_current_object(self) -> Optional[Model]: def get_object(self) -> Optional[Model]:
"""Finds and returns the current object, or None on a create form.""" """Finds and returns the current object, or None on a create form."""
if "pk" in self.kwargs: if "pk" in self.kwargs:
pk = self.kwargs["pk"] pk = self.kwargs["pk"]
@ -245,7 +245,7 @@ class UserFormView(FormView):
def make_form_from_post(self, post: Dict[str, str]) -> UserForm: def make_form_from_post(self, post: Dict[str, str]) -> UserForm:
"""Creates and returns the form from the POST data.""" """Creates and returns the form from the POST data."""
form = UserForm(post) form = UserForm(post)
form.user = self.get_current_object() form.user = self.get_object()
form.current_user = self.request.user form.current_user = self.request.user
return form return form
@ -256,7 +256,7 @@ class UserFormView(FormView):
"name": obj.name, "name": obj.name,
"is_disabled": obj.is_disabled, "is_disabled": obj.is_disabled,
}) })
form.user = self.get_current_object() form.user = self.get_object()
form.current_user = self.request.user form.current_user = self.request.user
return form return form
@ -272,11 +272,11 @@ class UserFormView(FormView):
def get_error_url(self) -> str: def get_error_url(self) -> str:
"""Returns the URL on error.""" """Returns the URL on error."""
user = self.get_current_object() user = self.get_object()
return reverse("mia_core:users.create") if user is None\ return reverse("mia_core:users.create") if user is None\
else reverse("mia_core:users.edit", args=(user,)) else reverse("mia_core:users.edit", args=(user,))
def get_current_object(self) -> Optional[Model]: def get_object(self) -> Optional[Model]:
"""Returns the current object, or None on a create form.""" """Returns the current object, or None on a create form."""
if "user" in self.kwargs: if "user" in self.kwargs:
return self.kwargs["user"] return self.kwargs["user"]