基于Django的角色的看法? [英] Django role based views?

查看:142
本文介绍了基于Django的角色的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找一些投入上别人怎么会这样的建筑师。我将提供基于类(Django的群体)的意见。

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 what views/templates he or she will have access to. I'm thinking of perhaps storing paths to view functions in a table to determine what a user's link bar will consist of. Filter specifications can also be stored to determine what rows will fill these templates.

一个很好的例子是医院护理单位。在一个单位的护士不需要看到整个医院的病人。他们只需要看到他们的病人。同一单位的医生只需要看到那些患者为好,但他们应该有更大的功能的访问。

A good example is a hospital nursing units. Nurses at one unit need not see the entire hospital's patients. They only need to see their patients. Doctors on the same unit need only to see those patients as well, but they should have access to much greater functionality.

这种情形已经通过某些第三方应用程序做了什么?而且你会怎么处理这个问题?

Has this been done via some third party application? And how would you approach this problem?

谢谢,
皮特

推荐答案

Django中已经有一个组和权限系统,这可能足以满足您的目的。

Django already has a groups and permissions system, which may be sufficient for your purpose.

http://docs.djangoproject.com/en/dev/topics/auth /

一般在code您检查用户是否有权限。一个用户都有自己的权限和那些组的他属于。您可以从管理控制台轻松管理本pretty。

Generally in your code you check if a user has a permission. A user has his own permissions and those of the groups he belongs to. You can administer this pretty easily from the admin console.

有两个部分,你需要看一下。

There are two parts you need to look at.


  1. 检查一个用户请求一个页面
    有这样的权限。

  2. 只显示链接到用户,如果他
    具有的权限。

有关1.您可以检查的权限在一个装饰这样:

For 1. you can check permissions in a decorator as such:

from django.contrib.auth.decorators import permission_required

@permission_required('polls.can_vote')
def some_view(request):

有关2.当前登录用户的权限存储在模板变量{{烫发}}。这code盘与上述相同的权限。

For 2. the currently logged-in user's permissions are stored in the template variable {{ perms }}. This code checks the same permission as above.

{% if perms.polls.can_vote %}
    <a href="/vote">vote</a>
{% endif %}

要生成)的链接列表,你可以遍历user.get_all_permissions(并获取链接(或生成链接功能)从字典:

To generate a list of links you can iterate over user.get_all_permissions() and fetch the links (or function that generates the link) from a dict:

def more_elaborate_list_of_links_for_a_perm(user):
    return ["/link1", ...]

_LINKS = {
    'polls.can_vote' : lambda u: ["/user/specific/link/" + u.id],
    'polls.can_close': lambda u: ['/static/link/1', 'static/link/2'],
    'polls.can_open' : more_elaborate_list_of_links_for_a_perm
}

def gen_links(user):
    # get_all_permissions also gets permissions for users groups
    perms = user.get_all_permissions()
    return sum((_LINKS[p](user) for p in perms if p in _LINKS), [])

有可能很多其他的方法。

There are probably many other approaches.

这篇关于基于Django的角色的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆