English 中文(简体)
我中继器械路径的排序 符合要求?
原标题:In which order to sort my middleware path match requirements?

我创建了一个中间软件, 允许我使用一个词典列表, 来为我的观点指定一些访问规则。

REQUIREMENTS=(
    { viewname : addtag ,
      permissions : can_add_tags },
    { regex :re.compile(r ^somestart ),
     user_check :lambda request:request.user.username== sam }
)

在我的中继器件中, 我试图找出其中哪些要求符合当前要求 。 要做到这一点, 我过滤完整要求, 在过滤功能中, i 使用此代码检查路径是否匹配 :

def process_request(self,request):

    def path_matches(self,req):
        path_matches = False

        if  (req.has_key( url ) and req[ url ] == request.path ) or
            (req.has_key( regex ) and req[ regex ].search(request.path)) or
            (req.has_key( viewname ) and resolve(request.path).url_name==req[ viewname ]):
            path_matches=True

        return path_matches

    requirements = filter(path_matches,REQUIREMENTS)
    # now use the returned requirements to determine if a user
    # matches the requirement and

现在我的问题是:我应该用什么顺序来使用检查?非常清楚的是,URL的检查是最快的,所以这必须先进行。 但问题是,是否应该先进行regex搜索或对齐的URL解析功能。

由于我目前没有任何业绩问题,这更是一个学术问题。 如果有人有更好的解决办法解决这个问题,那会更好。


<强强>编辑:

要对给定的答案作出反应: im 试图做的是创造一种可能性, 在一个文件中限制对多个外部应用程序的视图。 因此, 只要我不想做这样的事情, 装饰器不是一个选项 :

from ext_app1 import view1,view2
from ext_app2 import view3

@permission_required( can_do_stuff )
def view1_ext(*args,**kwargs):
    return view1(args,kwargs)

这将导致每次我更改权限时重写 URL 规格。 我希望避免这种情况。 此外, 我的解决方案允许用户_ check 函数对这样的用户进行检查 :

def check_user(user):
    if len(Item.objects.get(creator=user,datetime=today)) > 3:
        return False
    return True

这样可以简单限制用户每天能够上传的项目数量。 ( Ok, 如果使用用户_ passes_stest 也可以这样做 ) 。

还有一点是,我有时只想要在请求为 POST 的情况下检查许可,或者如果请求包含一定的密钥:价值对(如 动作:删除 需要许可,而 动作:允许任何人更改 。这也可以使用自定义的装饰器进行,但只要我需要新检查,我就需要一个新的装饰器。

问题回答

如果您重新使用在 用户认证和许可系统 (django.contrib.auth )中构建的“Djangos”系统,那么您应该考虑使用它提供的观点设计器,而不是中间软件。 这给了您几个好处 :

  • Code that changes together is in the same place, i.e. it s more likely that you ll need to change permissions for a specific view when you are changing the view s code than when you are changing other permissions.
  • You don t have to write much of the code yourself, which makes your project smaller and easier to maintain.

对于简单的情形,您可以使用 < a href="https://docs.djangoproject.com/en/dev/topics/#docs.contrib.autors.permission_required" rel="nofollows"_acolents_required_required <\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

代码看起来像这个功能的视图函数 ( 示例从文件 中删除 :

from django.contrib.auth.decorators import permission_required

@permission_required( polls.can_vote )
def my_view(request):
    ...

如果你再使用"https://docs.djangoproject.com/en/dev/topics/glass-based-views/"rel="nofollow"/"rel="nofollow"/"class-class-based-views/"rel="nofollows"/"rel="nofollow" example从文件 中删除:

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView

class ProtectedView(TemplateView):
    template_name =  secret.html 

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(ProtectedView, self).dispatch(*args, **kwargs)

如果您有充分理由不使用Djangos许可系统,那么您仍然可以采取类似的做法。 代码的django.contrib.auth 装饰者 可以很容易地用作您自己设计师的基础。

你可能在找"https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.decorators.user_passes_st" rel=“nofollow”>user_passes_test cutorator

您可以装饰您需要的视图, 而不是使用中间软件 。

代码会是这样的:

from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda user: user.has_perm( model.can_add_tags ) 
                                and user.username ==  sam )
def my_view(request):
    ...  




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签