Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/

## [Unreleased]

### Fixed

- Checking whether a `NavItem` or `NavGroup` is active now takes into account the URL scheme and domain name for both the nav item and request.

## [0.10.0]

### Added
Expand Down
6 changes: 6 additions & 0 deletions src/django_simple_nav/nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ def get_active(self, request: HttpRequest) -> bool:
parsed_url = urlparse(url)
parsed_request = urlparse(request.build_absolute_uri())

if (
parsed_url.scheme != parsed_request.scheme
or parsed_url.netloc != parsed_request.netloc
):
return False

url_path = parsed_url.path
request_path = parsed_request.path

Expand Down
10 changes: 5 additions & 5 deletions tests/test_navgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,17 @@ def get_url(self):
("/foo/", False),
],
)
def test_get_active(url, expected, req):
def test_get_active(url, expected, rf):
group = NavGroup(
title=...,
url="/test/",
url="http://testserver/test/",
items=[
NavItem(title=..., url="/test/active/"),
NavItem(title=..., url="/test/not-active/"),
NavItem(title=..., url="http://testserver/test/active/"),
NavItem(title=..., url="http://testserver/test/not-active/"),
],
)

req.path = url
req = rf.get(url)

assert group.get_active(req) is expected

Expand Down
25 changes: 20 additions & 5 deletions tests/test_navitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,34 @@ def get_url(self):
],
)
def test_active(url, req_path, req_params, expected, rf):
item = NavItem(title=..., url=url)
item = NavItem(title=..., url=f"http://testserver/{url.lstrip('/')}")

req = rf.get(req_path, req_params)
print(f"{req.path=}")

assert item.get_active(req) == expected


def test_active_different_scheme(rf):
item = NavItem(title=..., url="https://testserver/")

req = rf.get("/")

assert item.get_active(req) is False


def test_active_different_domain(rf):
item = NavItem(title=..., url="http://different-domain/")

req = rf.get("/")

assert item.get_active(req) is False


@pytest.mark.parametrize("append_slash", [True, False])
def test_active_append_slash_setting(append_slash, req):
item = NavItem(title=..., url="/test")
def test_active_append_slash_setting(append_slash, rf):
item = NavItem(title=..., url="http://testserver/test")

req.path = "/test"
req = rf.get("/test")

with override_settings(APPEND_SLASH=append_slash):
assert item.get_active(req) is True
Expand Down