Split the the dispatch method in FormView into do_get() and do_post() methods in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-13 17:53:15 +08:00
parent 3c655b8f87
commit a61f705dda

View File

@ -66,8 +66,14 @@ class FormView(View):
Returns:
The response.
"""
obj = self.get_current_object()
if self.request.method != "POST":
return self.do_get()
else:
return self.do_post()
def do_get(self) -> HttpResponse:
"""Handles the GET requests."""
obj = self.get_current_object()
previous_post = stored_post.get_previous_post(self.request)
if previous_post is not None:
form = self.make_form_from_post(previous_post)
@ -78,14 +84,17 @@ class FormView(View):
return render(self.request, self.get_template_name(), {
self.context_object_name: form
})
else:
def do_post(self) -> HttpResponseRedirect:
"""Handles the POST requests."""
obj = self.get_current_object()
post = self.request.POST.dict()
utils.strip_post(post)
form = self.make_form_from_post(post)
if not form.is_valid():
url = str(utils.UrlBuilder(self.get_error_url())
.query(r=self.request.GET.get("r")))
return stored_post.error_redirect(request, url, post)
return stored_post.error_redirect(self.request, url, post)
if obj is None:
obj = self._model()
self._set_current_object(obj)
@ -96,7 +105,7 @@ class FormView(View):
else:
obj.save()
message = self.get_success_message()
messages.success(request, message)
messages.success(self.request, message)
return redirect(str(UrlBuilder(self.get_success_url())
.query(r=self.request.GET.get("r"))))