mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-13 04:59:59 +02:00
refactored domain_requests_json
This commit is contained in:
parent
be8a618791
commit
ba20e740b8
1 changed files with 86 additions and 69 deletions
|
@ -14,81 +14,21 @@ def get_domain_requests_json(request):
|
||||||
|
|
||||||
domain_request_ids = get_domain_requests_ids_from_request(request)
|
domain_request_ids = get_domain_requests_ids_from_request(request)
|
||||||
|
|
||||||
domain_requests = DomainRequest.objects.filter(id__in=domain_request_ids)
|
objects = DomainRequest.objects.filter(id__in=domain_request_ids)
|
||||||
unfiltered_total = domain_requests.count()
|
unfiltered_total = objects.count()
|
||||||
|
|
||||||
# Handle sorting
|
objects = apply_search(objects, request)
|
||||||
sort_by = request.GET.get("sort_by", "id") # Default to 'id'
|
objects = apply_sorting(objects, request)
|
||||||
order = request.GET.get("order", "asc") # Default to 'asc'
|
|
||||||
search_term = request.GET.get("search_term")
|
|
||||||
|
|
||||||
if search_term:
|
paginator = Paginator(objects, 10)
|
||||||
search_term_lower = search_term.lower()
|
|
||||||
new_domain_request_text = "new domain request"
|
|
||||||
|
|
||||||
# Check if the search term is a substring of 'New domain request'
|
|
||||||
# If yes, we should return domain requests that do not have a
|
|
||||||
# requested_domain (those display as New domain request in the UI)
|
|
||||||
if search_term_lower in new_domain_request_text:
|
|
||||||
domain_requests = domain_requests.filter(
|
|
||||||
Q(requested_domain__name__icontains=search_term) | Q(requested_domain__isnull=True)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
domain_requests = domain_requests.filter(Q(requested_domain__name__icontains=search_term))
|
|
||||||
|
|
||||||
if order == "desc":
|
|
||||||
sort_by = f"-{sort_by}"
|
|
||||||
domain_requests = domain_requests.order_by(sort_by)
|
|
||||||
page_number = request.GET.get("page", 1)
|
page_number = request.GET.get("page", 1)
|
||||||
paginator = Paginator(domain_requests, 10)
|
|
||||||
page_obj = paginator.get_page(page_number)
|
page_obj = paginator.get_page(page_number)
|
||||||
|
|
||||||
domain_requests_data = [
|
domain_requests = [serialize_domain_request(domain_request) for domain_request in page_obj.object_list]
|
||||||
{
|
|
||||||
"requested_domain": domain_request.requested_domain.name if domain_request.requested_domain else None,
|
|
||||||
"last_submitted_date": domain_request.last_submitted_date,
|
|
||||||
"status": domain_request.get_status_display(),
|
|
||||||
"created_at": format(domain_request.created_at, "c"), # Serialize to ISO 8601
|
|
||||||
"id": domain_request.id,
|
|
||||||
"is_deletable": domain_request.status
|
|
||||||
in [DomainRequest.DomainRequestStatus.STARTED, DomainRequest.DomainRequestStatus.WITHDRAWN],
|
|
||||||
"action_url": (
|
|
||||||
reverse("edit-domain-request", kwargs={"id": domain_request.id})
|
|
||||||
if domain_request.status
|
|
||||||
in [
|
|
||||||
DomainRequest.DomainRequestStatus.STARTED,
|
|
||||||
DomainRequest.DomainRequestStatus.ACTION_NEEDED,
|
|
||||||
DomainRequest.DomainRequestStatus.WITHDRAWN,
|
|
||||||
]
|
|
||||||
else reverse("domain-request-status", kwargs={"pk": domain_request.id})
|
|
||||||
),
|
|
||||||
"action_label": (
|
|
||||||
"Edit"
|
|
||||||
if domain_request.status
|
|
||||||
in [
|
|
||||||
DomainRequest.DomainRequestStatus.STARTED,
|
|
||||||
DomainRequest.DomainRequestStatus.ACTION_NEEDED,
|
|
||||||
DomainRequest.DomainRequestStatus.WITHDRAWN,
|
|
||||||
]
|
|
||||||
else "Manage"
|
|
||||||
),
|
|
||||||
"svg_icon": (
|
|
||||||
"edit"
|
|
||||||
if domain_request.status
|
|
||||||
in [
|
|
||||||
DomainRequest.DomainRequestStatus.STARTED,
|
|
||||||
DomainRequest.DomainRequestStatus.ACTION_NEEDED,
|
|
||||||
DomainRequest.DomainRequestStatus.WITHDRAWN,
|
|
||||||
]
|
|
||||||
else "settings"
|
|
||||||
),
|
|
||||||
}
|
|
||||||
for domain_request in page_obj
|
|
||||||
]
|
|
||||||
|
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{
|
{
|
||||||
"domain_requests": domain_requests_data,
|
"domain_requests": domain_requests,
|
||||||
"has_next": page_obj.has_next(),
|
"has_next": page_obj.has_next(),
|
||||||
"has_previous": page_obj.has_previous(),
|
"has_previous": page_obj.has_previous(),
|
||||||
"page": page_obj.number,
|
"page": page_obj.number,
|
||||||
|
@ -98,6 +38,7 @@ def get_domain_requests_json(request):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_domain_requests_ids_from_request(request):
|
def get_domain_requests_ids_from_request(request):
|
||||||
"""Get domain request ids from request.
|
"""Get domain request ids from request.
|
||||||
|
|
||||||
|
@ -115,3 +56,79 @@ def get_domain_requests_ids_from_request(request):
|
||||||
)
|
)
|
||||||
|
|
||||||
return domain_requests.values_list("id", flat=True)
|
return domain_requests.values_list("id", flat=True)
|
||||||
|
|
||||||
|
|
||||||
|
def apply_search(queryset, request):
|
||||||
|
search_term = request.GET.get("search_term")
|
||||||
|
|
||||||
|
if search_term:
|
||||||
|
search_term_lower = search_term.lower()
|
||||||
|
new_domain_request_text = "new domain request"
|
||||||
|
|
||||||
|
# Check if the search term is a substring of 'New domain request'
|
||||||
|
# If yes, we should return domain requests that do not have a
|
||||||
|
# requested_domain (those display as New domain request in the UI)
|
||||||
|
if search_term_lower in new_domain_request_text:
|
||||||
|
queryset = queryset.filter(
|
||||||
|
Q(requested_domain__name__icontains=search_term) | Q(requested_domain__isnull=True)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
queryset = queryset.filter(Q(requested_domain__name__icontains=search_term))
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
def apply_sorting(queryset, request):
|
||||||
|
sort_by = request.GET.get("sort_by", "id") # Default to 'id'
|
||||||
|
order = request.GET.get("order", "asc") # Default to 'asc'
|
||||||
|
|
||||||
|
if order == "desc":
|
||||||
|
sort_by = f"-{sort_by}"
|
||||||
|
return queryset.order_by(sort_by)
|
||||||
|
|
||||||
|
|
||||||
|
def serialize_domain_request(domain_request):
|
||||||
|
is_deletable = domain_request.status in [
|
||||||
|
DomainRequest.DomainRequestStatus.STARTED,
|
||||||
|
DomainRequest.DomainRequestStatus.WITHDRAWN,
|
||||||
|
]
|
||||||
|
action_url = (
|
||||||
|
reverse("edit-domain-request", kwargs={"id": domain_request.id})
|
||||||
|
if domain_request.status
|
||||||
|
in [
|
||||||
|
DomainRequest.DomainRequestStatus.STARTED,
|
||||||
|
DomainRequest.DomainRequestStatus.ACTION_NEEDED,
|
||||||
|
DomainRequest.DomainRequestStatus.WITHDRAWN,
|
||||||
|
]
|
||||||
|
else reverse("domain-request-status", kwargs={"pk": domain_request.id})
|
||||||
|
)
|
||||||
|
action_label = (
|
||||||
|
"Edit"
|
||||||
|
if domain_request.status
|
||||||
|
in [
|
||||||
|
DomainRequest.DomainRequestStatus.STARTED,
|
||||||
|
DomainRequest.DomainRequestStatus.ACTION_NEEDED,
|
||||||
|
DomainRequest.DomainRequestStatus.WITHDRAWN,
|
||||||
|
]
|
||||||
|
else "Manage"
|
||||||
|
)
|
||||||
|
svg_icon = (
|
||||||
|
"edit"
|
||||||
|
if domain_request.status
|
||||||
|
in [
|
||||||
|
DomainRequest.DomainRequestStatus.STARTED,
|
||||||
|
DomainRequest.DomainRequestStatus.ACTION_NEEDED,
|
||||||
|
DomainRequest.DomainRequestStatus.WITHDRAWN,
|
||||||
|
]
|
||||||
|
else "settings"
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"requested_domain": domain_request.requested_domain.name if domain_request.requested_domain else None,
|
||||||
|
"last_submitted_date": domain_request.last_submitted_date,
|
||||||
|
"status": domain_request.get_status_display(),
|
||||||
|
"created_at": format(domain_request.created_at, "c"), # Serialize to ISO 8601
|
||||||
|
"id": domain_request.id,
|
||||||
|
"is_deletable": is_deletable,
|
||||||
|
"action_url": action_url,
|
||||||
|
"action_label": action_label,
|
||||||
|
"svg_icon": svg_icon,
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue