Skip to content

Commit 85433f8

Browse files
fix active NavItem check for urls with query params (#114)
1 parent 19dc974 commit 85433f8

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Changed
22+
23+
- Updated `NavItem.get_active` to allow for using URLs that contain query strings.
24+
2125
## [0.8.0]
2226

2327
### Changed

src/django_simple_nav/nav.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from dataclasses import field
66
from typing import Callable
77
from typing import cast
8+
from urllib.parse import parse_qs
89
from urllib.parse import urlparse
910
from urllib.parse import urlunparse
1011

@@ -136,10 +137,20 @@ def get_active(self, request: HttpRequest) -> bool:
136137
if not url:
137138
return False
138139

139-
if settings.APPEND_SLASH and not request.path.endswith("/"): # pyright: ignore[reportAny]
140-
request.path += "/"
140+
parsed_url = urlparse(url)
141+
parsed_request = urlparse(request.build_absolute_uri())
141142

142-
return request.path == url
143+
url_path = parsed_url.path
144+
request_path = parsed_request.path
145+
146+
if settings.APPEND_SLASH:
147+
url_path = url_path.rstrip("/") + "/"
148+
request_path = request_path.rstrip("/") + "/"
149+
150+
url_query = parse_qs(parsed_url.query)
151+
request_query = parse_qs(parsed_request.query)
152+
153+
return url_path == request_path and url_query == request_query
143154

144155
def get_items(self, request: HttpRequest) -> list[NavGroup | NavItem] | None:
145156
# this needs to be set to shadow the built-in `items()` of the dict

tests/conftest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ def pytest_configure(config):
3838

3939
@pytest.fixture
4040
def req():
41-
return HttpRequest()
41+
# adding a HTTP_HOST header for now to fix tests, but
42+
# we really should switch to using a RequestFactory
43+
# instead of this fixture
44+
request = HttpRequest()
45+
request.META = {"HTTP_HOST": "test"}
46+
return request

tests/test_navitem.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,23 @@ def get_url(self):
120120

121121

122122
@pytest.mark.parametrize(
123-
"url,req_path,expected",
123+
"url,req_path,req_params,expected",
124124
[
125-
("/test/", "/test/", True),
126-
("/test/", "/other/", False),
127-
("fake-view", "/fake-view/", True),
128-
("/test", "/test/", True),
129-
("/test/", "/test", True),
130-
("/test/nested/", "/test/", False),
125+
("/test/", "/test/", None, True),
126+
("/test/", "/other/", None, False),
127+
("fake-view", "/fake-view/", None, True),
128+
("/test", "/test/", None, True),
129+
("/test/", "/test", None, True),
130+
("/test/nested/", "/test/", None, False),
131+
("/test/?query=param", "/test/", {"query": "param"}, True),
132+
("/test/?query=param", "/test/", None, False),
131133
],
132134
)
133-
def test_active(url, req_path, expected, req):
135+
def test_active(url, req_path, req_params, expected, rf):
134136
item = NavItem(title=..., url=url)
135137

136-
req.path = req_path
138+
req = rf.get(req_path, req_params)
139+
print(f"{req.path=}")
137140

138141
assert item.get_active(req) == expected
139142

0 commit comments

Comments
 (0)