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,39 +66,48 @@ class FormView(View):
Returns: Returns:
The response. The response.
""" """
obj = self.get_current_object()
if self.request.method != "POST": if self.request.method != "POST":
previous_post = stored_post.get_previous_post(self.request) return self.do_get()
if previous_post is not None:
form = self.make_form_from_post(previous_post)
elif obj is not None:
form = self.make_form_from_model(obj)
else:
form = self._form()
return render(self.request, self.get_template_name(), {
self.context_object_name: form
})
else: else:
post = self.request.POST.dict() return self.do_post()
utils.strip_post(post)
form = self.make_form_from_post(post) def do_get(self) -> HttpResponse:
if not form.is_valid(): """Handles the GET requests."""
url = str(utils.UrlBuilder(self.get_error_url()) obj = self.get_current_object()
.query(r=self.request.GET.get("r"))) previous_post = stored_post.get_previous_post(self.request)
return stored_post.error_redirect(request, url, post) if previous_post is not None:
if obj is None: form = self.make_form_from_post(previous_post)
obj = self._model() elif obj is not None:
self._set_current_object(obj) form = self.make_form_from_model(obj)
self.fill_model_from_form(obj, form) else:
if isinstance(obj, DirtyFieldsMixin)\ form = self._form()
and not obj.is_dirty(check_relationship=True): return render(self.request, self.get_template_name(), {
message = self.get_not_modified_message() self.context_object_name: form
else: })
obj.save()
message = self.get_success_message() def do_post(self) -> HttpResponseRedirect:
messages.success(request, message) """Handles the POST requests."""
return redirect(str(UrlBuilder(self.get_success_url()) obj = self.get_current_object()
.query(r=self.request.GET.get("r")))) 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(self.request, url, post)
if obj is None:
obj = self._model()
self._set_current_object(obj)
self.fill_model_from_form(obj, form)
if isinstance(obj, DirtyFieldsMixin)\
and not obj.is_dirty(check_relationship=True):
message = self.get_not_modified_message()
else:
obj.save()
message = self.get_success_message()
messages.success(self.request, message)
return redirect(str(UrlBuilder(self.get_success_url())
.query(r=self.request.GET.get("r"))))
@property @property
def _form(self): def _form(self):