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,41 +9,21 @@ def index(request):
"""This page is available to anyone without logging in."""
context = {}
if request.user.is_authenticated:
# Let's exclude the approved applications since our
# domain_applications context will be used to populate
# the active applications table
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
deletable_applications = applications.filter(status__in=valid_statuses, requested_domain=None)
for application in applications:
if application in deletable_applications:
created_at = application.created_at.strftime("%b. %d, %Y, %I:%M %p UTC")
_name = f"New domain request ({created_at})"
default_draft_domain = DraftDomain(
name=_name,
is_complete=False
)
application.requested_domain = default_draft_domain
# Pass the final context to the application
# Get all domain applications the user has access to
applications, deletable_applications = _get_applications(request)
context["domain_applications"] = applications
user_domain_roles = UserDomainRole.objects.filter(user=request.user)
domain_ids = user_domain_roles.values_list("domain_id", flat=True)
domains = Domain.objects.filter(id__in=domain_ids)
# 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 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" '
@ -53,3 +33,37 @@ def index(request):
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
# domain_applications context will be used to populate
# the active applications table
applications = DomainApplication.objects.filter(creator=request.user).exclude(status="approved")
# 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)
for application in applications:
if application in deletable_applications:
created_at = application.created_at.strftime("%b. %d, %Y, %I:%M %p UTC")
_name = f"New domain request ({created_at})"
default_draft_domain = DraftDomain(
name=_name,
is_complete=False
)
application.requested_domain = default_draft_domain
return (applications, deletable_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)
domain_ids = user_domain_roles.values_list("domain_id", flat=True)
return Domain.objects.filter(id__in=domain_ids)