Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django相当于Rails cancan和Desive_Python_Ruby On Rails_Django - Fatal编程技术网

Python Django相当于Rails cancan和Desive

Python Django相当于Rails cancan和Desive,python,ruby-on-rails,django,Python,Ruby On Rails,Django,不久前,我和django一起做了一些项目,然后转向rails。我在rails中发现了很多很酷的东西。我需要向django项目添加一些特性 Are there Django equivalent of Rails cancan and devise ? Is there Django equivalent of Rails scheduler gem? 更新 对于django权限框架,我必须在每个视图中指定如下内容 @permission_required('polls.can_vote')

不久前,我和django一起做了一些项目,然后转向rails。我在rails中发现了很多很酷的东西。我需要向django项目添加一些特性

Are there  Django equivalent of Rails cancan and devise ?
Is there  Django equivalent of Rails scheduler gem?
更新

对于django权限框架,我必须在每个视图中指定如下内容

@permission_required('polls.can_vote')
def my_view
我更喜欢cancan的方式,我可以在一个地方管理所有权限

  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
    if user.has_role? :admin
      can :manage, :all
    else
      can :manage, :all      
      cannot :users, Swimming::Student
    end   
  end

您可以尝试使用Django的内置程序,而不是
cancan
designe
。它适合你的需要吗


在使用Django时,延迟和安排时间可能是最好的选择。有一个包装将芹菜和Django结合在一起。

像cancan一样重新授权

我正在使用,最终结果看起来/功能非常像cancan

models/user.py models/rules.py 示例_view.py 是处理身份验证和密码重置的好软件包。它是在Django中设计的另一个选择

是Django的授权库,灵感来自Rails cancan

首先,定义每个用户的能力:

def定义访问规则(用户、规则):
#任何人都可以查看已发表的文章
rules.allow('view',Article,published=True)
如果未对user.u进行身份验证:
返回
# ... 向登录用户授予其他功能
然后您可以在视图中使用它:

class ArticleDetailView(PermissionRequiredMixin,DetailView):
def get_queryset():
#这就是如何检索当前用户可以访问的所有对象
qs=self.request.ability.queryset_for('view',Article)
返回qs
def具有_权限(自我):
article=self.get_object()
#这是检查用户是否可以访问对象的方法
返回self.request.ability.can('view',article)
或在模板中:

{% if ability|can:"change"|subject:article %}
    <a href="{% url 'article_edit' pk=article.id %}">Edit article</a>
{% endif %}
{%if能力|可以:“改变”|主题:文章%}
{%endif%}

像Rufus scheduler这样的rails调度器gem的优点是,只需安装gem就可以开始运行(而不是像db设置、队列设置等其他设置),这些东西使部署方式变得复杂。芹菜非常容易使用。但是,是的,您需要为它安装一些后端。实际上这只需要5-10分钟。但如果你想要一些简单的东西,我相信还有其他选择。例如,您可以使用uWSGI装饰器(如果您使用uWSGI),它提供非常轻量级的延迟和调度功能。
from rules import RuleSet


class InvalidUser(Exception):
    pass


class BaseRuleSet(RuleSet):
    def __init__(self, user):
        super()


class ManagerRuleSet(BaseRuleSet):
    def __init__(self, user):
        super()
        if user and not user.group.is_owner:
            raise InvalidUser("instantiated OwnerRuleSet with user in {} group",
                                    user.group.name)

        # Calendar permissions (pass appointment or schedule event)
        self.add_rule('calendar.can_view_calendar', rules.always_true)
        self.add_rule('calendar.can_manage_schedule',
                      is_own | has_accepted_invite)
def index(request, id):
    calendar = Calendar.objects.get(id=id)

    if not request.user.has_perm('calendar.can_manage_schedule', calendar):
        return HttpResponseForbidden()
    # ...
{% if ability|can:"change"|subject:article %}
    <a href="{% url 'article_edit' pk=article.id %}">Edit article</a>
{% endif %}