File tree 2 files changed +13
-3
lines changed
2 files changed +13
-3
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import django
4
4
from django .core .exceptions import ImproperlyConfigured
5
- from .utils import get_callable , parse_url
5
+ from .utils import get_callable , parse_url , path_startswith
6
6
7
7
if django .VERSION >= (1 , 10 ): # pragma: no cover
8
8
from django .urls import reverse , NoReverseMatch
@@ -91,14 +91,14 @@ def _is_selected(self, item_dict):
91
91
If related URLS are given, it also returns true if one of the related URLS is part of path.
92
92
"""
93
93
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 ) :
95
95
return True
96
96
elif url == self .path :
97
97
return True
98
98
else :
99
99
# Go through all related URLs and test
100
100
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 ) :
102
102
return True
103
103
return False
104
104
Original file line number Diff line number Diff line change @@ -52,3 +52,13 @@ def parse_url(url):
52
52
except NoReverseMatch :
53
53
final_url = url
54
54
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
You can’t perform that action at this time.
0 commit comments