Here is an example of how to solve your problem:
First, create a decorator shell to use instead of resolved_required:
from django.contrib.auth.decorators import login_required, permission_required, user_passes_test from django.core.exceptions import PermissionDenied from functools import wraps from django.utils.decorators import available_attrs def require_perms(*perms): def decorator(view_func): view_func.permissions = perms @wraps(view_func, assigned=available_attrs(view_func)) def _wrapped_view(request, *args, **kwargs): for perm in perms: return view_func(request, *args, **kwargs) raise PermissionDenied() return _wrapped_view return decorator
Then use it to decorate your views:
@require_perms('my_perm',) def home(request): .....
Then add the tag that will be used for your menu items:
from django.core.urlresolvers import resolve def check_menu_permissions(menu_path, user): view = resolve(menu_path) if hasattr(view.func, "permissions"): permissions = view.func.permissions for perm in permissions: if user.has_perm(perm): return True
And finally, in your templates when creating the menu tree:
<button href="{{ some_path }} {% if not check_menu_permissions some_path request.user %}disabled="disabled"{% endif %} />
NB I have not tested the last part with the tag, but hope you have an idea. The magic here is to add permissions to the view_func in the decorator, and then you can access it with a permission (path). I'm not sure how this will behave in terms of performance, but this is just an idea.
EDIT : fixed bug in example.
Tisho
source share