1đ
something like (maybe reinventing the wheel)
class require_permission:
def __init__(self,permission,redirect="index/denied_access"):
self.perm = permission
self.redirect = redirect
@require_login
def __call__(self,f):
def fn(self,request,*args,**kwargs):
if not request.user.has_perm(self.perm):
redirect(self.redirect)
return f(request,*args,**kwargs)
return fn
...
@require_permission(PERMISSION.CAN_VIEW_ADMIN,'index/capture_info')
def admin_view(request,*args,**kw):
return HttpResponse(...)
@require_permission(PERMISSION.CAN_CAPTURE_INFO,'index/basic_user')
def capture_view(request,*args,**kw):
return HttpRespone(...)
@require_login
def basic_view(request,*args,**kw):
return HttpResponse(...)
1đ
What do I need to give permissions to each user?
What youâre going to want here is a Group for each type of user (https://docs.djangoproject.com/en/dev/topics/auth/default/#groups). E.G. âSuperuserâ, âNormal Userâ, and âRestricted Userâ. These Groups can be created in the Django Admin Console and then each user can be assigned to their respective groups using the same Django Admin Console (https://docs.djangoproject.com/en/dev/topics/auth/default/#managing-users-in-the-admin).
How can I redirect each panel to their respective user?
You will want to define a RedirectView that forwards to userâs request to the expected âpanelâ view based on the userâs assigned group (https://docs.djangoproject.com/en/dev/ref/class-based-views/base/#redirectview).
from django.core.urlresolvers import reverse
from django.views.generic.base import RedirectView
from articles.models import Article
class PanelRedirectView(RedirectView):
def get_redirect_url(self, *args, **kwargs):
user = self.request.user
if user.groups.filter(name='superuser').count():
return reverse('superuser-panel')
elif user.groups.filter(name='regular_user').count():
return reverse('regular-user-panel')
else:
return reverse('restricted-user-panel')
You can then use the annotations described by Joran to restricted access to the particular views being redirected to. The example I described uses restrictions based User Groups, whereas his answer uses restrictions based on User Permissions. This is more of a personal preference when dealing with a simple permission scheme like the one you are describing.
* More Details *
To redirect the user on loginâŠ
-
Create a named url for your redirect view in urls.py
url(râ^panel-redirect/$â , PanelRedirectView.as_view() , name=âpanel-redirectâ),
-
change LOGIN_REDIRECT_URL in settings.py to point to this redirect view on login
LOGIN_REDIRECT_URL = reverse(âpanel-redirectâ)
When the person logs in they will be redirected to the RedirectView, which in turn will redirect the person to the correct panel based on his/her assigned group
- [Answered ]-Javascript modules not available templates not inherited from base Django tempate
- [Answered ]-How to display new line input text in new line?