mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-12 20:49:41 +02:00
Index.py refactor
This commit is contained in:
parent
9fe1bbaac5
commit
f623d00fc2
1 changed files with 40 additions and 26 deletions
|
@ -9,41 +9,21 @@ 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:
|
||||||
# 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]
|
# Get all domain applications the user has access to
|
||||||
|
applications, deletable_applications = _get_applications(request)
|
||||||
# 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
|
|
||||||
context["domain_applications"] = applications
|
context["domain_applications"] = applications
|
||||||
|
|
||||||
user_domain_roles = UserDomainRole.objects.filter(user=request.user)
|
# Get all domains the user has access to
|
||||||
domain_ids = user_domain_roles.values_list("domain_id", flat=True)
|
domains = _get_domains(request)
|
||||||
domains = Domain.objects.filter(id__in=domain_ids)
|
|
||||||
|
|
||||||
context["domains"] = domains
|
context["domains"] = domains
|
||||||
|
|
||||||
# Determine if the user will see applications that they can delete
|
# Determine if the user will see applications that they can delete
|
||||||
has_deletable_applications = deletable_applications.exists()
|
has_deletable_applications = deletable_applications.exists()
|
||||||
context["has_deletable_applications"] = has_deletable_applications
|
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
|
# Add the delete modal button to the context
|
||||||
modal_button = (
|
modal_button = (
|
||||||
'<button type="submit" '
|
'<button type="submit" '
|
||||||
|
@ -53,3 +33,37 @@ def index(request):
|
||||||
context["modal_button"] = modal_button
|
context["modal_button"] = modal_button
|
||||||
|
|
||||||
return render(request, "home.html", context)
|
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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue