Skip to content

Commit 541317a

Browse files
NavGroup now marked as active if any item URLs are active (#105)
1 parent 7ce51e3 commit 541317a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
3434
- `Nav.get_items` now returns a list of `NavGroup` or `NavItem`, instead of a list of `RenderedNavItem`.
3535
- Check for the existence of a user attached to the `request` object passed in to `django_simple_nav.permissions.check_item_permissions` has been moved to allow for an early return if there is no user. There are instances where the `django.contrib.auth` app can be installed, but no user is attached to the request object. This change will allow this function to correctly be used in those instances.
3636
- Now using v2024.20 of `django-twc-package`.
37+
- `NavGroup` is now marked as active if the request path matches it's URL (if set) **or** and of its items' URLs.
3738

3839
### Removed
3940

src/django_simple_nav/nav.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ def get_url(self) -> str:
199199
return ""
200200
return url
201201

202+
@override
203+
def get_active(self, request: HttpRequest) -> bool:
204+
is_active = super().get_active(request)
205+
206+
items = self.get_items(request)
207+
item_is_active = any([item.get_active(request) for item in items])
208+
209+
return is_active or item_is_active
210+
202211
@override
203212
def check_permissions(self, request: HttpRequest) -> bool:
204213
has_perm = super().check_permissions(request)

tests/test_navgroup.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,29 @@ def get_url(self):
192192
group.get_url()
193193

194194

195+
@pytest.mark.parametrize(
196+
"url,expected",
197+
[
198+
("/test/", True),
199+
("/test/active/", True),
200+
("/foo/", False),
201+
],
202+
)
203+
def test_get_active(url, expected, req):
204+
group = NavGroup(
205+
title=...,
206+
url="/test/",
207+
items=[
208+
NavItem(title=..., url="/test/active/"),
209+
NavItem(title=..., url="/test/not-active/"),
210+
],
211+
)
212+
213+
req.path = url
214+
215+
assert group.get_active(req) is expected
216+
217+
195218
@pytest.mark.parametrize(
196219
"url,items,expected",
197220
[

0 commit comments

Comments
 (0)