Fix url path bug

This commit is contained in:
zandercymatics 2024-09-16 10:11:22 -06:00
parent b5b2d69344
commit eb3850458b
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 55 additions and 6 deletions

View file

@ -598,9 +598,11 @@ class DomainRequest(TimeStampedModel):
def get_first_status_set_date(self, status):
"""Returns the date when the domain request was first set to the given status."""
log_entry = LogEntry.objects.filter(
content_type__model="domainrequest", object_pk=self.pk, changes__status__1=status
).order_by("-timestamp").first()
log_entry = (
LogEntry.objects.filter(content_type__model="domainrequest", object_pk=self.pk, changes__status__1=status)
.order_by("-timestamp")
.first()
)
return log_entry.timestamp.date() if log_entry else None
def get_first_status_started_date(self):

View file

@ -3,6 +3,7 @@
import time
import logging
from urllib.parse import urlparse, urlunparse, urlencode
from django.urls import resolve, Resolver404
logger = logging.getLogger(__name__)
@ -315,3 +316,21 @@ def convert_queryset_to_dict(queryset, is_model=True, key="id"):
request_dict = {value[key]: value for value in queryset}
return request_dict
def get_url_name(path):
"""
Given a URL path, returns the corresponding URL name defined in urls.py.
Args:
path (str): The URL path to resolve.
Returns:
str or None: The URL name if it exists, otherwise None.
"""
try:
match = resolve(path)
return match.url_name
except Resolver404:
logger.error(f"No matching URL name found for path: {path}")
return None

View file

@ -59,7 +59,7 @@
{% url 'domain-requests' as url %}
<button
type="button"
class="usa-accordion__button usa-nav__link{% if 'request'|in_path:request.path %} usa-current{% endif %}"
class="usa-accordion__button usa-nav__link{% if path|is_domain_request_subpage %} usa-current{% endif %}"
aria-expanded="false"
aria-controls="basic-nav-section-two"
>
@ -80,13 +80,13 @@
<!-- user has view but no edit permissions -->
{% elif has_any_requests_portfolio_permission %}
{% url 'domain-requests' as url %}
<a href="{{ url }}" class="usa-nav-link{% if 'request'|in_path:request.path %} usa-current{% endif %}">
<a href="{{ url }}" class="usa-nav-link{% if path|is_domain_request_subpage %} usa-current{% endif %}">
Domain requests
</a>
<!-- user does not have permissions -->
{% else %}
{% url 'no-portfolio-requests' as url %}
<a href="{{ url }}" class="usa-nav-link{% if 'request'|in_path:request.path %} usa-current{% endif %}">
<a href="{{ url }}" class="usa-nav-link{% if path|is_domain_request_subpage %} usa-current{% endif %}">
Domain requests
</a>
{% endif %}

View file

@ -3,6 +3,9 @@ from django import template
import re
from registrar.models.domain_request import DomainRequest
from phonenumber_field.phonenumber import PhoneNumber
from registrar.views.domain_request import DomainRequestWizard
from registrar.models.utility.generic_helper import get_url_name
register = template.Library()
logger = logging.getLogger(__name__)
@ -174,3 +177,28 @@ def has_contact_info(user):
@register.filter
def model_name_lowercase(instance):
return instance.__class__.__name__.lower()
@register.filter(name="is_domain_request_subpage")
def is_domain_request_subpage(path):
"""Checks if the given page is a subpage of domain requests.
Takes a path name, like '/requests/'."""
# Since our pages aren't unified under a common path, we need this approach for now.
url_names = [
"domain-requests",
"no-portfolio-requests",
"domain-request-status",
"domain-request-withdraw-confirmation",
"domain-request-withdrawn",
"domain-request-delete",
]
# The domain request wizard pages don't have a defined path,
# so we need to check directly on it.
wizard_paths = [
DomainRequestWizard.EDIT_URL_NAME,
DomainRequestWizard.URL_NAMESPACE,
DomainRequestWizard.NEW_URL_NAME,
]
return get_url_name(path) in url_names or any(wizard in path for wizard in wizard_paths)