| 1 |
import copy |
| 2 |
|
| 3 |
import django |
| 4 |
from django.core.exceptions import ImproperlyConfigured |
| 5 |
from django.urls import Resolver404 |
| 6 |
from .utils import get_callable, parse_url, path_startswith |
| 7 |
|
| 8 |
if django.VERSION >= (1, 10): |
| 9 |
from django.urls import resolve, reverse, NoReverseMatch |
| 10 |
else: |
| 11 |
from django.core.urlresolvers import resolve, reverse, NoReverseMatch |
| 12 |
|
| 13 |
|
| 14 |
class MenuBase(object): |
| 15 |
""" |
| 16 |
Base class that generates menu list. |
| 17 |
""" |
| 18 |
|
| 19 |
def __init__(self): |
| 20 |
self.path = '' |
| 21 |
self.request = None |
| 22 |
|
| 23 |
def save_user_state(self, request): |
| 24 |
""" |
| 25 |
Given a request object, store the current user attributes |
| 26 |
:param request: HttpRequest |
| 27 |
""" |
| 28 |
self.request = request |
| 29 |
self.path = request.path |
| 30 |
|
| 31 |
def _is_validated(self, item_dict): |
| 32 |
""" |
| 33 |
Given a menu item dictionary, it returns true if the user passes all the validator's conditions, it means, |
| 34 |
if the user passes all the conditions, the user can see the menu |
| 35 |
""" |
| 36 |
validators = item_dict.get('validators') |
| 37 |
if not validators: |
| 38 |
return True |
| 39 |
|
| 40 |
if not isinstance(validators, (list, tuple)): |
| 41 |
raise ImproperlyConfigured("validators must be a list") |
| 42 |
|
| 43 |
result_validations = [] |
| 44 |
for validator in validators: |
| 45 |
if isinstance(validator, tuple): |
| 46 |
if len(validator) <= 1: |
| 47 |
raise ImproperlyConfigured("You are passing a tuple validator without args %s" % str(validator)) |
| 48 |
func = get_callable(validator[0]) |
| 49 |
|
| 50 |
args = validator[1:] |
| 51 |
|
| 52 |
result_validations.append(func(self.request, *args)) |
| 53 |
else: |
| 54 |
func = get_callable(validator) |
| 55 |
result_validations.append(func(self.request)) |
| 56 |
return all(result_validations) |
| 57 |
|
| 58 |
def _has_attr(self, item_dict, attr): |
| 59 |
""" |
| 60 |
Given a menu item dictionary, it returns true if an attr is set. |
| 61 |
""" |
| 62 |
if item_dict.get(attr, False): |
| 63 |
return True |
| 64 |
return False |
| 65 |
|
| 66 |
def _get_icon(self, parent_dict): |
| 67 |
""" |
| 68 |
Given a menu item dictionary, this returns an icon class if one exist, or |
| 69 |
returns an empty string. |
| 70 |
""" |
| 71 |
return parent_dict.get('icon_class', '') |
| 72 |
|
| 73 |
def _get_url(self, item_dict): |
| 74 |
""" |
| 75 |
Given a menu item dictionary, it returns the URL or an empty string. |
| 76 |
""" |
| 77 |
url = item_dict.get('url', '') |
| 78 |
return parse_url(url) |
| 79 |
|
| 80 |
def _get_related_urls(self, item_dict): |
| 81 |
""" |
| 82 |
Given a menu item dictionary, it returns the relateds URLs or an empty list. |
| 83 |
""" |
| 84 |
related_urls = item_dict.get('related_urls', []) |
| 85 |
return [parse_url(url) for url in related_urls] |
| 86 |
|
| 87 |
def _get_related_views(self, item_dict): |
| 88 |
""" |
| 89 |
Given a menu item dictionary, it returns the relateds viewss or an empty list. |
| 90 |
""" |
| 91 |
related_views = item_dict.get('related_views', []) |
| 92 |
return related_views |
| 93 |
|
| 94 |
def _is_selected(self, item_dict): |
| 95 |
""" |
| 96 |
Given a menu item dictionary, it returns true if `url` is on path, |
| 97 |
unless the item is marked as a root, in which case returns true if `url` is part of path. |
| 98 |
|
| 99 |
If related URLS are given, it also returns true if one of the related URLS is part of path. |
| 100 |
If related views are given, it also returns true if the path maps to one of these views. |
| 101 |
""" |
| 102 |
url = self._get_url(item_dict) |
| 103 |
if self._is_root(item_dict) and path_startswith(self.path, url): |
| 104 |
return True |
| 105 |
elif url == self.path: |
| 106 |
return True |
| 107 |
else: |
| 108 |
|
| 109 |
for related_url in self._get_related_urls(item_dict): |
| 110 |
if path_startswith(self.path, related_url): |
| 111 |
return True |
| 112 |
|
| 113 |
try: |
| 114 |
if resolve(self.path).func in self._get_related_views(item_dict): |
| 115 |
return True |
| 116 |
except Resolver404: |
| 117 |
return False |
| 118 |
return False |
| 119 |
|
| 120 |
def _is_root(self, item_dict): |
| 121 |
""" |
| 122 |
Given a menu item dictionary, it returns true if item is marked as a `root`. |
| 123 |
""" |
| 124 |
return item_dict.get('root', False) |
| 125 |
|
| 126 |
def _process_breadcrums(self, menu_list): |
| 127 |
""" |
| 128 |
Given a menu list, it marks the items on the current path as selected, which |
| 129 |
can be used as breadcrumbs |
| 130 |
""" |
| 131 |
for item in menu_list: |
| 132 |
if item['submenu']: |
| 133 |
item['selected'] = self._process_breadcrums(item['submenu']) |
| 134 |
if item['selected']: |
| 135 |
return True |
| 136 |
return False |
| 137 |
|
| 138 |
def _get_submenu_list(self, parent_dict): |
| 139 |
""" |
| 140 |
Given a menu item dictionary, it returns a submenu if one exist, or |
| 141 |
returns None. |
| 142 |
""" |
| 143 |
submenu = parent_dict.get('submenu', None) |
| 144 |
if submenu: |
| 145 |
for child_dict in submenu: |
| 146 |
|
| 147 |
|
| 148 |
child_dict['validators'] = list( |
| 149 |
set(list(parent_dict.get('validators', [])) + list(child_dict.get('validators', []))) |
| 150 |
) |
| 151 |
submenu = self.generate_menu(submenu) |
| 152 |
if not submenu: |
| 153 |
submenu = None |
| 154 |
return submenu |
| 155 |
|
| 156 |
def _get_menu_list(self, list_dict): |
| 157 |
""" |
| 158 |
A generator that returns only the visible menu items. |
| 159 |
""" |
| 160 |
for item in list_dict: |
| 161 |
if self._has_attr(item, 'name') and self._has_attr(item, 'url'): |
| 162 |
if self._is_validated(item): |
| 163 |
yield copy.copy(item) |
| 164 |
|
| 165 |
def generate_menu(self, list_dict): |
| 166 |
""" |
| 167 |
Given a list of dictionaries, returns a menu list. |
| 168 |
""" |
| 169 |
visible_menu = [] |
| 170 |
for item in self._get_menu_list(list_dict): |
| 171 |
item['url'] = self._get_url(item) |
| 172 |
item['selected'] = self._is_selected(item) |
| 173 |
item['submenu'] = self._get_submenu_list(item) |
| 174 |
item['icon_class'] = self._get_icon(item) |
| 175 |
visible_menu.append(item) |
| 176 |
self._process_breadcrums(visible_menu) |
| 177 |
|
| 178 |
return visible_menu |
| 179 |
|
| 180 |
|
| 181 |
class Menu(MenuBase): |
| 182 |
""" |
| 183 |
Class that generates menu list. |
| 184 |
""" |
| 185 |
|
| 186 |
def __call__(self, request, list_dict): |
| 187 |
self.save_user_state(request) |
| 188 |
return self.generate_menu(list_dict) |
| 189 |
|
| 190 |
|
| 191 |
generate_menu = Menu() |