Fix issues with org_user_status global context and logo_clickable profile context

This commit is contained in:
Rachid Mrad 2024-07-08 18:57:46 -04:00
parent a7c29f4d21
commit dcb63d1947
No known key found for this signature in database
11 changed files with 132 additions and 127 deletions

View file

@ -429,6 +429,10 @@ class ViewsTest(TestCase):
# Create a mock request # Create a mock request
request = self.factory.get("/some-url") request = self.factory.get("/some-url")
request.session = {"acr_value": ""} request.session = {"acr_value": ""}
# Mock user and its attributes
mock_user = MagicMock()
mock_user.is_authenticated = True
request.user = mock_user
# Ensure that the CLIENT instance used in login_callback is the mock # Ensure that the CLIENT instance used in login_callback is the mock
# patch _requires_step_up_auth to return False # patch _requires_step_up_auth to return False
with patch("djangooidc.views._requires_step_up_auth", return_value=False), patch( with patch("djangooidc.views._requires_step_up_auth", return_value=False), patch(

View file

@ -7,4 +7,3 @@
.usa-identifier__logo { .usa-identifier__logo {
height: units(7); height: units(7);
} }

View file

@ -135,11 +135,7 @@
<div class="usa-overlay"></div> <div class="usa-overlay"></div>
{% block header %} {% block header %}
{% if not is_org_user %} {% include "includes/header_selector.html" with logo_clickable=True %}
{% include "includes/header_basic.html" %}
{% else %}
{% include "includes/header_extended.html" %}
{% endif %}
{% endblock header %} {% endblock header %}
{% block wrapper %} {% block wrapper %}

View file

@ -9,7 +9,6 @@
{% block content %} {% block content %}
{% block messages %} {% block messages %}
{% if messages %} {% if messages %}
<p>test</p>
<ul class="messages"> <ul class="messages">
{% for message in messages %} {% for message in messages %}
<li {% if message.tags %} class="{{ message.tags }}" {% endif %}> <li {% if message.tags %} class="{{ message.tags }}" {% endif %}>

View file

@ -4,8 +4,8 @@
{% block title %} Finish setting up your profile | {% endblock %} {% block title %} Finish setting up your profile | {% endblock %}
{# Disable the redirect #} {# Disable the redirect #}
{% block logo %} {% block header %}
{% include "includes/gov_extended_logo.html" with logo_clickable=user_finished_setup %} {% include "includes/header_selector.html" with logo_clickable=user_finished_setup %}
{% endblock %} {% endblock %}
{# Add the new form #} {# Add the new form #}

View file

@ -3,9 +3,7 @@
<header class="usa-header usa-header--basic"> <header class="usa-header usa-header--basic">
<div class="usa-nav-container"> <div class="usa-nav-container">
<div class="usa-navbar"> <div class="usa-navbar">
{% block logo %} {% include "includes/gov_extended_logo.html" with logo_clickable=logo_clickable %}
{% include "includes/gov_extended_logo.html" with logo_clickable=True %}
{% endblock %}
<button type="button" class="usa-menu-btn">Menu</button> <button type="button" class="usa-menu-btn">Menu</button>
</div> </div>
{% block usa_nav %} {% block usa_nav %}
@ -39,4 +37,3 @@
{% endblock %} {% endblock %}
</div> </div>
</header> </header>

View file

@ -2,9 +2,7 @@
<header class="usa-header usa-header--extended"> <header class="usa-header usa-header--extended">
<div class="usa-navbar"> <div class="usa-navbar">
{% block logo %} {% include "includes/gov_extended_logo.html" with logo_clickable=logo_clickable %}
{% include "includes/gov_extended_logo.html" with logo_clickable=True %}
{% endblock %}
<button type="button" class="usa-menu-btn">Menu</button> <button type="button" class="usa-menu-btn">Menu</button>
</div> </div>
{% block usa_nav %} {% block usa_nav %}

View file

@ -0,0 +1,5 @@
{% if not is_org_user %}
{% include "includes/header_basic.html" with logo_clickable=logo_clickable %}
{% else %}
{% include "includes/header_extended.html" with logo_clickable=logo_clickable %}
{% endif %}

View file

@ -36,6 +36,4 @@
{% block content_bottom %}{% endblock %} {% block content_bottom %}{% endblock %}
</div> </div>
{% endblock wrapper %} {% endblock wrapper %}

View file

@ -6,8 +6,8 @@ Edit your User Profile |
{% load static url_helpers %} {% load static url_helpers %}
{# Disable the redirect #} {# Disable the redirect #}
{% block logo %} {% block header %}
{% include "includes/gov_extended_logo.html" with logo_clickable=user_finished_setup %} {% include "includes/header_selector.html" with logo_clickable=user_finished_setup %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View file

@ -17,82 +17,15 @@ def get_domains_json(request):
objects = Domain.objects.filter(id__in=domain_ids) objects = Domain.objects.filter(id__in=domain_ids)
unfiltered_total = objects.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_state_filter(objects, request)
order = request.GET.get("order", "asc") # Default to 'asc' objects = apply_sorting(objects, request)
# Handle search term
search_term = request.GET.get("search_term")
if search_term:
objects = objects.filter(Q(name__icontains=search_term))
# Handle state
status_param = request.GET.get("status")
if status_param:
status_list = status_param.split(",")
# if unknown is in status_list, append 'dns needed' since both
# unknown and dns needed display as DNS Needed, and both are
# searchable via state parameter of 'unknown'
if "unknown" in status_list:
status_list.append("dns needed")
# Split the status list into normal states and custom states
normal_states = [state for state in status_list if state in Domain.State.values]
custom_states = [state for state in status_list if state == "expired"]
# Construct Q objects for normal states that can be queried through ORM
state_query = Q()
if normal_states:
state_query |= Q(state__in=normal_states)
# Handle custom states in Python, as expired can not be queried through ORM
if "expired" in custom_states:
expired_domain_ids = [domain.id for domain in objects if domain.state_display() == "Expired"]
state_query |= Q(id__in=expired_domain_ids)
# Apply the combined query
objects = objects.filter(state_query)
# If there are filtered states, and expired is not one of them, domains with
# state_display of 'Expired' must be removed
if "expired" not in custom_states:
expired_domain_ids = [domain.id for domain in objects if domain.state_display() == "Expired"]
objects = objects.exclude(id__in=expired_domain_ids)
if sort_by == "state_display":
# Fetch the objects and sort them in Python
objects = list(objects) # Evaluate queryset to a list
objects.sort(key=lambda domain: domain.state_display(), reverse=(order == "desc"))
else:
if order == "desc":
sort_by = f"-{sort_by}"
objects = objects.order_by(sort_by)
paginator = Paginator(objects, 10) paginator = Paginator(objects, 10)
page_number = request.GET.get("page") page_number = request.GET.get("page")
page_obj = paginator.get_page(page_number) page_obj = paginator.get_page(page_number)
# Convert objects to JSON-serializable format domains = [serialize_domain(domain) for domain in page_obj.object_list]
domains = [
{
"id": domain.id,
"name": domain.name,
"expiration_date": domain.expiration_date,
"state": domain.state,
"state_display": domain.state_display(),
"get_state_help_text": domain.get_state_help_text(),
"action_url": reverse("domain", kwargs={"pk": domain.id}),
"action_label": ("View" if domain.state in [Domain.State.DELETED, Domain.State.ON_HOLD] else "Manage"),
"svg_icon": ("visibility" if domain.state in [Domain.State.DELETED, Domain.State.ON_HOLD] else "settings"),
"suborganization": (
domain.domain_info.sub_organization.name
if domain.domain_info and domain.domain_info.sub_organization
else None
),
}
for domain in page_obj.object_list
]
return JsonResponse( return JsonResponse(
{ {
@ -105,3 +38,79 @@ def get_domains_json(request):
"unfiltered_total": unfiltered_total, "unfiltered_total": unfiltered_total,
} }
) )
def apply_search(queryset, request):
search_term = request.GET.get("search_term")
if search_term:
queryset = queryset.filter(Q(name__icontains=search_term))
return queryset
def apply_state_filter(queryset, request):
status_param = request.GET.get("status")
if status_param:
status_list = status_param.split(",")
# if unknown is in status_list, append 'dns needed' since both
# unknown and dns needed display as DNS Needed, and both are
# searchable via state parameter of 'unknown'
if "unknown" in status_list:
status_list.append("dns needed")
# Split the status list into normal states and custom states
normal_states = [state for state in status_list if state in Domain.State.values]
custom_states = [state for state in status_list if state == "expired"]
# Construct Q objects for normal states that can be queried through ORM
state_query = Q()
if normal_states:
state_query |= Q(state__in=normal_states)
# Handle custom states in Python, as expired can not be queried through ORM
if "expired" in custom_states:
expired_domain_ids = [domain.id for domain in queryset if domain.state_display() == "Expired"]
state_query |= Q(id__in=expired_domain_ids)
# Apply the combined query
queryset = queryset.filter(state_query)
# If there are filtered states, and expired is not one of them, domains with
# state_display of 'Expired' must be removed
if "expired" not in custom_states:
expired_domain_ids = [domain.id for domain in queryset if domain.state_display() == "Expired"]
queryset = queryset.exclude(id__in=expired_domain_ids)
return queryset
def apply_sorting(queryset, request):
sort_by = request.GET.get("sort_by", "id")
order = request.GET.get("order", "asc")
if sort_by == "state_display":
objects = list(queryset)
objects.sort(key=lambda domain: domain.state_display(), reverse=(order == "desc"))
return objects
else:
if order == "desc":
sort_by = f"-{sort_by}"
return queryset.order_by(sort_by)
def serialize_domain(domain):
suborganization_name = None
try:
domain_info = domain.domain_info
if domain_info:
suborganization = domain_info.sub_organization
if suborganization:
suborganization_name = suborganization.name
except Domain.domain_info.RelatedObjectDoesNotExist:
domain_info = None
return {
"id": domain.id,
"name": domain.name,
"expiration_date": domain.expiration_date,
"state": domain.state,
"state_display": domain.state_display(),
"get_state_help_text": domain.get_state_help_text(),
"action_url": reverse("domain", kwargs={"pk": domain.id}),
"action_label": ("View" if domain.state in [Domain.State.DELETED, Domain.State.ON_HOLD] else "Manage"),
"svg_icon": ("visibility" if domain.state in [Domain.State.DELETED, Domain.State.ON_HOLD] else "settings"),
"suborganization": suborganization_name,
}