Moved the form creating logic from the post() method to the get_form() method, and applied get_form() to the post() method in FormView in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-17 07:48:46 +08:00
parent 8f6c8f3497
commit 35b246077c

View File

@ -75,9 +75,7 @@ class FormView(View):
def post(self, request: HttpRequest, *args, def post(self, request: HttpRequest, *args,
**kwargs) -> HttpResponseRedirect: **kwargs) -> HttpResponseRedirect:
"""Handles the POST requests.""" """Handles the POST requests."""
post = self.request.POST.dict() form = self.get_form(**kwargs)
utils.strip_post(post)
form = self.make_form_from_post(post)
if not form.is_valid(): if not form.is_valid():
return self.form_invalid(form) return self.form_invalid(form)
return self.form_valid(form) return self.form_valid(form)
@ -112,13 +110,18 @@ class FormView(View):
def get_form(self, **kwargs) -> forms.Form: def get_form(self, **kwargs) -> forms.Form:
"""Returns the form for the template.""" """Returns the form for the template."""
previous_post = stored_post.get_previous_post(self.request) if self.request.method != "POST":
if previous_post is not None: previous_post = stored_post.get_previous_post(self.request)
return self.make_form_from_post(previous_post) if previous_post is not None:
obj = self.get_object() return self.make_form_from_post(previous_post)
if obj is not None: obj = self.get_object()
return self.make_form_from_model(obj) if obj is not None:
return self.get_form_class()() return self.make_form_from_model(obj)
return self.get_form_class()()
else:
post = self.request.POST.dict()
utils.strip_post(post)
return self.make_form_from_post(post)
def get_template_name(self) -> str: def get_template_name(self) -> str:
"""Returns the name of the template.""" """Returns the name of the template."""