Django role based views?

后端 未结 9 1998
既然无缘
既然无缘 2020-12-04 06:43

I\'m looking for some input on how others would architect this. I\'m going to provide class (django group) based views.

For example, a user\'s group will determine

9条回答
  •  暖寄归人
    2020-12-04 07:24

    We used a role base system for a similar problem. Basically users have permissions to assume different roles.

    View functions got decorated:

    def needs_capability(capability,redirect_to="/cms/"):
       def view_func_wrapper(view_func):
           def wrapped_view_func(request,*args,**kwargs):
               if not request.role._can(capability):
                  return HttpResponseRedirect(redirect_to)
               return view_func(request,*args,**kwargs)
           return wrapped_view_func
       return view_func_wrapper
    

    The rest of the magic is inside the request.role attribute which got set inside a context processor. Authenticated users got a Role, for the unwashed masses a DummyRole.

    Access to information was restricted further inside the templates:

     {% if not request.role.can.view_all_products %}
              Lots of products, yeah!
     {% endif %}
    

    Not the cleanest solution in my opinion, but worked as expected.

提交回复
热议问题