Skip to content
Snippets Groups Projects
Verified Commit f77cc68f authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Anchor related_url to beginning of request path and match components

Closes #22.
parent ccc3ef9b
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ import copy ...@@ -2,7 +2,7 @@ import copy
import django import django
from django.core.exceptions import ImproperlyConfigured 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 if django.VERSION >= (1, 10): # pragma: no cover
from django.urls import reverse, NoReverseMatch from django.urls import reverse, NoReverseMatch
...@@ -91,14 +91,14 @@ class MenuBase(object): ...@@ -91,14 +91,14 @@ class MenuBase(object):
If related URLS are given, it also returns true if one of the related URLS is part of path. 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) 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 return True
elif url == self.path: elif url == self.path:
return True return True
else: else:
# Go through all related URLs and test # Go through all related URLs and test
for related_url in self._get_related_urls(item_dict): 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 True
return False return False
......
...@@ -52,3 +52,13 @@ def parse_url(url): ...@@ -52,3 +52,13 @@ def parse_url(url):
except NoReverseMatch: except NoReverseMatch:
final_url = url final_url = url
return final_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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment