Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Tab Functionality for URL Query-Based Field Filtering #811

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/unfold/sites.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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
Expand Down Expand Up @@ -394,10 +395,25 @@ def _process_colors(
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"):
request_path = request.get_full_path()
# Special case for the index page
if link == request.path == reverse_lazy(f"{self.name}:index"):
return True

return False
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()
)