added checks for conditions that produce info message; added info message; changed submit button if conditions exist; changed behavior of submit button

This commit is contained in:
David Kennedy 2023-11-24 13:31:59 -05:00
parent ebafb31f70
commit 6a7a8cd0f6
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
2 changed files with 65 additions and 0 deletions

View file

@ -66,6 +66,13 @@
value="next"
class="usa-button"
>Save and continue</button>
{% elif pending_requests_exist %}
<button
type="submit"
name="submit_button"
value="save_and_return"
class="usa-button usa-button--outline"
>Save and return to manage your domains</button>
{% else %}
<button
type="submit"

View file

@ -1,11 +1,14 @@
import logging
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.db.models import Q
from django.shortcuts import redirect, render
from django.urls import resolve, reverse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView
from django.contrib import messages
from typing import List
from registrar.forms import application_wizard as forms
from registrar.models import DomainApplication
@ -218,6 +221,23 @@ class ApplicationWizard(ApplicationWizardPermissionView, TemplateView):
self.steps.current = current_url
context = self.get_context_data()
context["forms"] = self.get_forms()
# if pending requests exist and user does not have approved domains,
# present message that domain application cannot be submitted
pending_requests = self.pending_requests()
if len(pending_requests) > 0:
# TODO may want to move this whole thing to utility/errors.py
message_header = "You cannot submit this request yet"
message_content = (
f"<h4 class='usa-alert__heading'>{message_header}</h4>"
"<p>New domain requests cannot be submitted until we have finished reviewing your pending request: "
f"<strong>{pending_requests[0].requested_domain}</strong>. You can continue to fill out this request and "
"save it as a draft to be submitted later. "
f"<a class='usa-link' href='{reverse('home')}'>View your pending requests.</a></p>"
)
messages.info(request, mark_safe(message_content))
context["pending_requests_exist"] = len(pending_requests) > 0
return render(request, self.template_name, context)
def get_all_forms(self, **kwargs) -> list:
@ -266,6 +286,39 @@ class ApplicationWizard(ApplicationWizardPermissionView, TemplateView):
return instantiated
def pending_requests(self) -> List[DomainApplication]:
"""return an array of pending requests if user has pending requests
and no approved requests"""
if self.approved_applications_exist() or self.approved_domains_exist():
return []
else:
return self.pending_applications()
def approved_applications_exist(self) -> bool:
"""Checks if user is creator of applications with APPROVED status"""
approved_application_count = DomainApplication.objects.filter(
creator=self.request.user,
status=DomainApplication.APPROVED
).count()
return approved_application_count > 0
def approved_domains_exist(self) -> bool:
"""Checks if user has permissions on approved domains
This additional check is necessary to account for domains which were migrated
and do not have an application"""
return self.request.user.permissions.count() > 0
def pending_applications(self) -> List[DomainApplication]:
"""Returns a List of user's applications with one of the following states:
SUBMITTED, IN_REVIEW, ACTION_NEEDED"""
# if the current application has ACTION_NEEDED status, this check should not be performed
if self.application.status == DomainApplication.ACTION_NEEDED:
return []
check_statuses = [DomainApplication.APPROVED, DomainApplication.IN_REVIEW, DomainApplication.ACTION_NEEDED]
filter_conditions = Q(creator=self.request.user) & Q(status__in=check_statuses)
return DomainApplication.objects.filter(filter_conditions)
def get_context_data(self):
"""Define context for access on all wizard pages."""
return {
@ -328,6 +381,11 @@ class ApplicationWizard(ApplicationWizardPermissionView, TemplateView):
if button == "save":
messages.success(request, "Your progress has been saved!")
return self.goto(self.steps.current)
# if user opted to save progress and return,
# return them to the home page
if button == "save_and_return":
messages.success(request, "Your progress has been saved!")
return HttpResponseRedirect(reverse("home"))
# otherwise, proceed as normal
return self.goto_next_step()