Skip to content

Commit

Permalink
Anchor related_url to beginning of request path and match components
Browse files Browse the repository at this point in the history
  • Loading branch information
Natureshadow committed Nov 17, 2020
1 parent ccc3ef9 commit f77cc68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions menu_generator/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import django
from django.core.exceptions import ImproperlyConfigured
from .utils import get_callable, parse_url
from .utils import get_callable, parse_url, path_startswith

if django.VERSION >= (1, 10): # pragma: no cover
from django.urls import reverse, NoReverseMatch
Expand Down Expand Up @@ -91,14 +91,14 @@ def _is_selected(self, item_dict):
If related URLS are given, it also returns true if one of the related URLS is part of path.
"""
url = self._get_url(item_dict)
if self._is_root(item_dict) and url in self.path:
if self._is_root(item_dict) and path_startswith(self.path, url):
return True
elif url == self.path:
return True
else:
# Go through all related URLs and test
for related_url in self._get_related_urls(item_dict):
if related_url in self.path:
if path_startswith(self.path, related_url):
return True
return False

Expand Down
10 changes: 10 additions & 0 deletions menu_generator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ def parse_url(url):
except NoReverseMatch:
final_url = url
return final_url


def path_startswith(path, prefix):
"""
Returns True if the leftmost path components are the same as prefix.
"""
path_components = path.strip("/").split("/")
prefix_components = prefix.strip("/").split("/")

return path_components[:len(prefix_components)] == prefix_components

0 comments on commit f77cc68

Please sign in to comment.