Skip to content

Commit f77cc68

Browse files
committed
Anchor related_url to beginning of request path and match components
Closes #22.
1 parent ccc3ef9 commit f77cc68

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Diff for: menu_generator/menu.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import django
44
from django.core.exceptions import ImproperlyConfigured
5-
from .utils import get_callable, parse_url
5+
from .utils import get_callable, parse_url, path_startswith
66

77
if django.VERSION >= (1, 10): # pragma: no cover
88
from django.urls import reverse, NoReverseMatch
@@ -91,14 +91,14 @@ def _is_selected(self, item_dict):
9191
If related URLS are given, it also returns true if one of the related URLS is part of path.
9292
"""
9393
url = self._get_url(item_dict)
94-
if self._is_root(item_dict) and url in self.path:
94+
if self._is_root(item_dict) and path_startswith(self.path, url):
9595
return True
9696
elif url == self.path:
9797
return True
9898
else:
9999
# Go through all related URLs and test
100100
for related_url in self._get_related_urls(item_dict):
101-
if related_url in self.path:
101+
if path_startswith(self.path, related_url):
102102
return True
103103
return False
104104

Diff for: menu_generator/utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ def parse_url(url):
5252
except NoReverseMatch:
5353
final_url = url
5454
return final_url
55+
56+
57+
def path_startswith(path, prefix):
58+
"""
59+
Returns True if the leftmost path components are the same as prefix.
60+
"""
61+
path_components = path.strip("/").split("/")
62+
prefix_components = prefix.strip("/").split("/")
63+
64+
return path_components[:len(prefix_components)] == prefix_components

0 commit comments

Comments
 (0)