|
2 | 2 |
|
3 | 3 | import django |
4 | 4 | from django.core.exceptions import ImproperlyConfigured |
5 | | -from .utils import get_callable |
| 5 | +from .utils import get_callable, parse_url |
6 | 6 |
|
7 | 7 | if django.VERSION >= (1, 10): # pragma: no cover |
8 | 8 | from django.urls import reverse, NoReverseMatch |
@@ -74,22 +74,33 @@ def _get_url(self, item_dict): |
74 | 74 | Given a menu item dictionary, it returns the URL or an empty string. |
75 | 75 | """ |
76 | 76 | url = item_dict.get('url', '') |
77 | | - try: |
78 | | - final_url = reverse(**url) if type(url) is dict else reverse(url) |
79 | | - except NoReverseMatch: |
80 | | - final_url = url |
81 | | - return final_url |
| 77 | + return parse_url(url) |
| 78 | + |
| 79 | + def _get_related_urls(self, item_dict): |
| 80 | + """ |
| 81 | + Given a menu item dictionary, it returns the relateds URLs or an empty list. |
| 82 | + """ |
| 83 | + related_urls = item_dict.get('related_urls', []) |
| 84 | + return [parse_url(url) for url in related_urls] |
82 | 85 |
|
83 | 86 | def _is_selected(self, item_dict): |
84 | 87 | """ |
85 | 88 | Given a menu item dictionary, it returns true if `url` is on path, |
86 | 89 | unless the item is marked as a root, in which case returns true if `url` is part of path. |
| 90 | +
|
| 91 | + If related URLS are given, it also returns true if one of the related URLS is part of path. |
87 | 92 | """ |
88 | 93 | url = self._get_url(item_dict) |
89 | | - if self._is_root(item_dict): |
90 | | - return url in self.path |
| 94 | + if self._is_root(item_dict) and url in self.path: |
| 95 | + return True |
| 96 | + elif url == self.path: |
| 97 | + return True |
91 | 98 | else: |
92 | | - return url == self.path |
| 99 | + # Go through all related URLs and test |
| 100 | + for related_url in self._get_related_urls(item_dict): |
| 101 | + if related_url in self.path: |
| 102 | + return True |
| 103 | + return False |
93 | 104 |
|
94 | 105 | def _is_root(self, item_dict): |
95 | 106 | """ |
|
0 commit comments