Skip to content

Commit

Permalink
Merge pull request #1289 from nationalarchives/FCL-180-save-answers-w…
Browse files Browse the repository at this point in the history
…hen-pressing-previous

[FCL-180] Save user input when pressing previous in licence application form
  • Loading branch information
Dranoel2 authored Jun 28, 2024
2 parents 5dd067b + be6c83c commit 84116f8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
34 changes: 34 additions & 0 deletions e2e_tests/test_transactional_licence_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,37 @@ def get_review_row(page, label):
expect(get_review_row(page, "What is the full legal name")).to_have_text(
"New Organisation name"
)


def test_data_saved_when_going_back(page: Page):
"""
Test the process to apply for a computational analysis licence.
We stop on the review page (short of actually submitting the form)
(which triggers an email to the licencing team): the final step will be tested
in isolation with a mocked email service."""

# Fill contact details in full
page.goto("/re-use-find-case-law-records/steps/contact")
page.get_by_label("Contact Full Name").fill("Full Name")
page.get_by_label("Contact Email address").fill("[email protected]")
page.get_by_label(
"This is a different person (please enter their details below)"
).click()
page.get_by_label("Licence holder Full Name").fill("Licence Holder")
page.get_by_label("Licence holder Email").fill("[email protected]")

# On the next page, fill only 1 field, leaving others blank
page.get_by_text("Next").click()
page.get_by_label("What is the full legal name of your organisation?").fill(
"Organisation name"
)

# Check previous page has information
page.get_by_text("Previous").click()
expect(page.get_by_label("Contact Full Name")).to_have_value("Full Name")

# Check incomplete next page has information
page.get_by_text("Next").click()
expect(
page.get_by_label("What is the full legal name of your organisation?")
).to_have_value("Organisation name")
46 changes: 22 additions & 24 deletions transactional_licence_form/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ def render_goto_step(self, goto_step, **kwargs):
return redirect(url)

def post(self, *args, **kwargs):
# We override this to ensure that the form is saved when returning to the review page

wizard_goto_step = self.request.POST.get("wizard_goto_step", None)
if not self.in_review():
# If we are not reviewing, we follow the default behaviour: we do not validate or save the form
# and go directly to the step.
if wizard_goto_step and wizard_goto_step in self.get_form_list():
return self.render_goto_step(wizard_goto_step)

management_form = ManagementForm(self.request.POST, prefix=self.prefix)
if not management_form.is_valid():
raise SuspiciousOperation(
Expand All @@ -58,22 +49,29 @@ def post(self, *args, **kwargs):

form = self.get_form(data=self.request.POST, files=self.request.FILES)

if form.is_valid():
self.storage.set_step_data(self.steps.current, self.process_step(form))
self.storage.set_step_files(
self.steps.current, self.process_step_files(form)
)
wizard_goto_step = self.request.POST.get("wizard_goto_step", None)

# If we're in review or pressing 'Next', we need to ensure the user fixes bad data
if not form.is_valid() and (self.in_review() or wizard_goto_step is None):
return self.render(form)

self.storage.set_step_data(self.steps.current, self.process_step(form))
self.storage.set_step_files(self.steps.current, self.process_step_files(form))

# Going to a step out of order takes priority
if wizard_goto_step and wizard_goto_step in self.get_form_list():
return self.render_goto_step(wizard_goto_step)

# If the form is invalid, re-render it with errors
if not form.is_valid():
return self.render(form)

# When moving forwards from the final review form, send the email
if self.steps.current == self.steps.last:
return self.render_done(form, **kwargs)

if self.steps.current == self.steps.last:
return self.render_done(form, **kwargs)
else:
if wizard_goto_step and wizard_goto_step in self.get_form_list():
# If we are reviewing, and there is a goto step, we go to that step.
return self.render_goto_step(wizard_goto_step)
else:
# Otherwise we follow the default behaviour.
return self.render_next_step(form)
return self.render(form)
# If the user isn't jumping and their input is valid, move forwards
return self.render_next_step(form)

def in_review(self):
has_review_parameter = bool(
Expand Down

0 comments on commit 84116f8

Please sign in to comment.