Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for GET form method #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions formtools/wizard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class based views.
"""
storage_name = None
form_list = None
form_method = 'POST'
initial_dict = None
instance_dict = None
condition_dict = None
Expand Down Expand Up @@ -254,34 +255,52 @@ def dispatch(self, request, *args, **kwargs):
def get(self, request, *args, **kwargs):
"""
This method handles GET requests.

If a GET request reaches this point, the wizard assumes that the user
just starts at the first step or wants to restart the process.
The data of the wizard will be resetted before rendering the first step
"""

if self.form_method == 'GET':
current_step = self.request.GET.get('{}-current_step'.format(self.prefix), None)

if current_step is None:
# If a GET request reaches this point, the wizard assumes that the user
# just starts at the first step or wants to restart the process.
# The data of the wizard will be resetted before rendering the first step
self.storage.reset()

# reset the current step to the first step.
self.storage.current_step = self.steps.first

return self.render(self.get_form())

return self.handle_step(*args, **kwargs)

self.storage.reset()

# reset the current step to the first step.
self.storage.current_step = self.steps.first

return self.render(self.get_form())

def post(self, *args, **kwargs):
"""
This method handles POST requests.
"""
return self.handle_step(*args, **kwargs)

def handle_step(self, *args, **kwargs):
"""
The wizard will render either the current step (if form validation
wasn't successful), the next step (if the current step was stored
successful) or the done view (if no more steps are available)
"""
# Look for a wizard_goto_step element in the posted data which
# contains a valid step name. If one was found, render the requested
# form. (This makes stepping back a lot easier).
wizard_goto_step = self.request.POST.get('wizard_goto_step', None)
wizard_goto_step = getattr(self.request, self.form_method).get('wizard_goto_step', None)
Copy link

@ShaheedHaque ShaheedHaque May 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused as to how this is supposed to work. By ignoring the actual request.method and forcing the use of self.form_method here an in the next couple of places it is used, isn't the code saying that ALL form interaction can only use self.form_method? Specifically, stepping forward and the final submit cannot use a POST if self.form_method is set to 'GET'.

Or have I not understood the purpose of self.form_method?

if wizard_goto_step and wizard_goto_step in self.get_form_list():
return self.render_goto_step(wizard_goto_step)

# Check if form was refreshed
management_form = ManagementForm(self.request.POST, prefix=self.prefix)
management_form = ManagementForm(getattr(self.request, self.form_method), prefix=self.prefix)
if not management_form.is_valid():
raise ValidationError(
_('ManagementForm data is missing or has been tampered.'),
Expand All @@ -295,7 +314,7 @@ def post(self, *args, **kwargs):
self.storage.current_step = form_current_step

# get the form for the current step
form = self.get_form(data=self.request.POST, files=self.request.FILES)
form = self.get_form(data=getattr(self.request, self.form_method), files=self.request.FILES)

# and try to validate
if form.is_valid():
Expand Down Expand Up @@ -643,6 +662,7 @@ def get(self, *args, **kwargs):
This renders the form or, if needed, does the http redirects.
"""
step_url = kwargs.get('step', None)

if step_url is None:
if 'reset' in self.request.GET:
self.storage.reset()
Expand Down Expand Up @@ -693,7 +713,7 @@ def post(self, *args, **kwargs):
Do a redirect if user presses the prev. step button. The rest of this
is super'd from WizardView.
"""
wizard_goto_step = self.request.POST.get('wizard_goto_step', None)
wizard_goto_step = getattr(self.request, self.form_method).get('wizard_goto_step', None)
if wizard_goto_step and wizard_goto_step in self.get_form_list():
return self.render_goto_step(wizard_goto_step)
return super(NamedUrlWizardView, self).post(*args, **kwargs)
Expand Down