Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anchor related_url to beginning of request path #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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