revise to use session instead of a timer

This commit is contained in:
Rachid Mrad 2024-06-11 15:58:11 -04:00
parent 7809ab5978
commit f8f1ec447c
No known key found for this signature in database
3 changed files with 41 additions and 21 deletions

View file

@ -110,10 +110,12 @@ class DomainRequestTests(TestWithUser, WebTest):
This tests that the domain requests get created only when they should. This tests that the domain requests get created only when they should.
""" """
# Get the intro page # Get the intro page
intro_page = self.app.get(reverse("domain-request:")) self.app.get(reverse("home"))
session_id = self.app.cookies[settings.SESSION_COOKIE_NAME] session_id = self.app.cookies[settings.SESSION_COOKIE_NAME]
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
intro_page = self.app.get(reverse("domain-request:"))
# Select the form # Select the form
intro_form = intro_page.forms[0] intro_form = intro_page.forms[0]
@ -129,22 +131,26 @@ class DomainRequestTests(TestWithUser, WebTest):
domain_request_count = DomainRequest.objects.count() domain_request_count = DomainRequest.objects.count()
self.assertEqual(domain_request_count, 1) self.assertEqual(domain_request_count, 1)
# AGAIN right away, this should NOT cause a new request to be created # Let's go back to intro and submit again, this should not create a new request
# as a small threshold between 'Continue' submits indicate a use of the # This is the equivalent of a back button nav from step 1 to intro -> continue
# browser back button.
intro_form = intro_page.forms[0] intro_form = intro_page.forms[0]
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id) self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
response = intro_form.submit(name="submit_button", value="intro_acknowledge") type_form = intro_form.submit(name="submit_button", value="intro_acknowledge")
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id) self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
response.follow() type_form.follow()
domain_request_count = DomainRequest.objects.count() domain_request_count = DomainRequest.objects.count()
self.assertEqual(domain_request_count, 1) self.assertEqual(domain_request_count, 1)
# One more time, dropping session which will set the last_submit_time to 0, # Go home, which will reset the session flag for new request
# therefore increasing the threshold and having us expect the creation of a new request self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
self.app.get(reverse("home"))
# This time, clicking continue will create a new request
intro_form = intro_page.forms[0] intro_form = intro_page.forms[0]
response = intro_form.submit(name="submit_button", value="intro_acknowledge") self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
response.follow() intro_result = intro_form.submit(name="submit_button", value="intro_acknowledge")
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
intro_result.follow()
domain_request_count = DomainRequest.objects.count() domain_request_count = DomainRequest.objects.count()
self.assertEqual(domain_request_count, 2) self.assertEqual(domain_request_count, 2)

View file

@ -1,6 +1,5 @@
import logging import logging
from collections import defaultdict from collections import defaultdict
import time
from django.http import Http404, HttpResponse, HttpResponseRedirect from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from django.urls import resolve, reverse from django.urls import resolve, reverse
@ -108,6 +107,12 @@ class DomainRequestWizard(DomainRequestWizardPermissionView, TemplateView):
Step.ABOUT_YOUR_ORGANIZATION: lambda w: w.from_model("show_about_your_organization", False), Step.ABOUT_YOUR_ORGANIZATION: lambda w: w.from_model("show_about_your_organization", False),
} }
def get_session_data(self, request):
"""Retrieve 'global' data from session:
The homepage sets new_request to True for the first Create domain request."""
is_a_new_request = self.request.session.get("new_request", "No data found")
return is_a_new_request
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.steps = StepsHelper(self) self.steps = StepsHelper(self)
@ -436,6 +441,16 @@ class DomainRequestWizard(DomainRequestWizardPermissionView, TemplateView):
return step_list return step_list
def goto(self, step): def goto(self, step):
if step == "generic_org_type":
# We need to avoid creating a new domain request if the user
# clicks the back button
self.request.session["new_request"] = False
else:
# Reset the above logic to be extra safe;
# we do not want to stumble into a situation where a user
# unkowingly overrites when she thinks she's working on a
# new request
self.request.session["new_request"] = True
self.steps.current = step self.steps.current = step
return redirect(reverse(f"{self.URL_NAMESPACE}:{step}")) return redirect(reverse(f"{self.URL_NAMESPACE}:{step}"))
@ -458,22 +473,17 @@ class DomainRequestWizard(DomainRequestWizardPermissionView, TemplateView):
# which button did the user press? # which button did the user press?
button: str = request.POST.get("submit_button", "") button: str = request.POST.get("submit_button", "")
current_time = time.time() # If a user hits the new request url directly
if "new_request" not in request.session:
request.session["new_request"] = True
# if user has acknowledged the intro message # if user has acknowledged the intro message
if button == "intro_acknowledge": if button == "intro_acknowledge":
if request.path_info == self.NEW_URL_NAME: if request.path_info == self.NEW_URL_NAME:
last_submit_time = request.session.get("last_submit_time", 0)
# Check if the last submit was very recent, indicating a back button -> submit sequence
if current_time - last_submit_time > 5: # 5 seconds threshold
if self.request.session["new_request"] is True:
# This will trigger the domain_request getter into creating a new DomainRequest # This will trigger the domain_request getter into creating a new DomainRequest
del self.storage del self.storage
# Update the last submit time
request.session["last_submit_time"] = current_time
return self.goto(self.steps.first) return self.goto(self.steps.first)
# if accessing this class directly, redirect to the first step # if accessing this class directly, redirect to the first step

View file

@ -30,6 +30,10 @@ def index(request):
) )
context["modal_button"] = modal_button context["modal_button"] = modal_button
# This controls the creation of a new domain request in the wizard
print("will set session")
request.session["new_request"] = True
return render(request, "home.html", context) return render(request, "home.html", context)