Skip to content

Commit

Permalink
fix: fix _get_is_active when url contains query
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Oct 18, 2024
1 parent ba0c335 commit 8d956c0
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/unfold/sites.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from http import HTTPStatus
from typing import Any, Callable, Dict, List, Optional, Union
from urllib.parse import parse_qs, urlparse

from django.contrib.admin import AdminSite
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.validators import EMPTY_VALUES
from django.http import HttpRequest, HttpResponse
from django.template.response import TemplateResponse
from django.urls import URLPattern, path, reverse, reverse_lazy
from django.urls import URLPattern, path, reverse
from django.utils.decorators import method_decorator
from django.utils.functional import lazy
from django.utils.module_loading import import_string
Expand Down Expand Up @@ -395,9 +396,26 @@ def _get_is_active(self, request: HttpRequest, link: str) -> bool:
if not isinstance(link, str):
link = str(link)

if link in request.path and link != reverse_lazy(f"{self.name}:index"):
return True
elif link == request.path == reverse_lazy(f"{self.name}:index"):
return True
request_path = request.get_full_path()

return False
# Parse the URLs
link_parts = urlparse(link)
request_parts = urlparse(request_path)

# Check if paths match
if link_parts.path != request_parts.path:
return False

# If there are no query parameters in the link, it's active only if the request also has no query parameters
if not link_parts.query:
return not request_parts.query

# Parse query parameters
link_params = parse_qs(link_parts.query)
request_params = parse_qs(request_parts.query)

# Check if all link parameters are present in the request with the same values
return all(
key in request_params and request_params[key] == value
for key, value in link_params.items()
)

0 comments on commit 8d956c0

Please sign in to comment.