Index.py refactor

This commit is contained in:
zandercymatics 2024-01-16 14:16:48 -07:00
parent 9fe1bbaac5
commit f623d00fc2
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -9,15 +9,44 @@ def index(request):
"""This page is available to anyone without logging in.""" """This page is available to anyone without logging in."""
context = {} context = {}
if request.user.is_authenticated: if request.user.is_authenticated:
# Get all domain applications the user has access to
applications, deletable_applications = _get_applications(request)
context["domain_applications"] = applications
# Get all domains the user has access to
domains = _get_domains(request)
context["domains"] = domains
# Determine if the user will see applications that they can delete
has_deletable_applications = deletable_applications.exists()
context["has_deletable_applications"] = has_deletable_applications
# If they can delete applications, add the delete button to the context
if has_deletable_applications:
# Add the delete modal button to the context
modal_button = (
'<button type="submit" '
'class="usa-button usa-button--secondary" '
'name="delete-application">Yes, delete request</button>'
)
context["modal_button"] = modal_button
return render(request, "home.html", context)
def _get_applications(request):
"""Given the current request,
get all DomainApplications that are associated with the UserDomainRole object.
Returns a tuple of all applications, and those that are deletable by the user.
"""
# Let's exclude the approved applications since our # Let's exclude the approved applications since our
# domain_applications context will be used to populate # domain_applications context will be used to populate
# the active applications table # the active applications table
applications = DomainApplication.objects.filter(creator=request.user).exclude(status="approved") applications = DomainApplication.objects.filter(creator=request.user).exclude(status="approved")
valid_statuses = [DomainApplication.ApplicationStatus.STARTED, DomainApplication.ApplicationStatus.WITHDRAWN]
# Create a placeholder DraftDomain for each incomplete draft # Create a placeholder DraftDomain for each incomplete draft
valid_statuses = [DomainApplication.ApplicationStatus.STARTED, DomainApplication.ApplicationStatus.WITHDRAWN]
deletable_applications = applications.filter(status__in=valid_statuses, requested_domain=None) deletable_applications = applications.filter(status__in=valid_statuses, requested_domain=None)
for application in applications: for application in applications:
if application in deletable_applications: if application in deletable_applications:
@ -30,26 +59,11 @@ def index(request):
application.requested_domain = default_draft_domain application.requested_domain = default_draft_domain
# Pass the final context to the application return (applications, deletable_applications)
context["domain_applications"] = applications
def _get_domains(request):
"""Given the current request,
get all domains that are associated with the UserDomainRole object"""
user_domain_roles = UserDomainRole.objects.filter(user=request.user) user_domain_roles = UserDomainRole.objects.filter(user=request.user)
domain_ids = user_domain_roles.values_list("domain_id", flat=True) domain_ids = user_domain_roles.values_list("domain_id", flat=True)
domains = Domain.objects.filter(id__in=domain_ids) return Domain.objects.filter(id__in=domain_ids)
context["domains"] = domains
# Determine if the user will see applications that they can delete
has_deletable_applications = deletable_applications.exists()
context["has_deletable_applications"] = has_deletable_applications
if has_deletable_applications:
# Add the delete modal button to the context
modal_button = (
'<button type="submit" '
'class="usa-button usa-button--secondary" '
'name="delete-application">Yes, delete request</button>'
)
context["modal_button"] = modal_button
return render(request, "home.html", context)