5

我尝试在我的 Django 应用程序中获取有关经过身份验证的用户的详细信息。

为此,我创建了一个新资源:

class MyUserResource(ModelResource):
    class Meta:
        queryset = ReaderUser.objects.all()
        resource_name = 'me'
        list_allowed_methods = []
        detail_allowed_methods = ['get']
        authorization = Authorization()
        authentication = SessionAuthentication()
        excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login')

    def apply_authorization_limits(self, request, object_list):
        print request.user
        return object_list.filter(pk=request.user.pk)

    def prepend_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
        ]

当我调用我的 API 时,/api/me/?format=json 我得到了以下信息:More than one resource is found at this URI.

我也尝试过不使用 prepend_urls。我不明白的是该print语句从未在方法中执行apply_authorization_limits

关于我做错了什么的任何提示?

4

2 回答 2

4

我找到了两种方法来解决我的问题:

第一个是两个创建我自己的授权。

就我而言,以下内容:

from tastypie.authorization import Authorization

class SimpleReaderAuthorization(Authorization):
    def read_list(self, object_list, bundle):
        return object_list.filter(email=bundle.request.user.email)

我只需要更新我的资源:

class MyUserResource(ModelResource):
    class Meta:
        queryset = ReaderUser.objects.all()
        resource_name = 'me'
        list_allowed_methods = ['get']
        authorization = SimpleReaderAuthorization()
        authentication = SessionAuthentication()
        excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login')

另一种简单的方法是按照文档中的说明执行以下操作。

def get_object_list(self, request): 
        return super(YourResource, self).get_object_list(request).filter(pk=request.user.pk)

结论:我选择了第二个,因为它更干净,更简单。

于 2013-09-24T17:46:04.577 回答
0

文档未显示,但 apply_authorization_limits 设置为已弃用。它不仅被弃用,而且在 2013 年 2 月被本次提交从资源生命周期中删除。这就是它不再被调用的原因。

当时更新的文档在这里,从那时起并没有太大变化。

检查您的美味派版本,因为更新的文档建议使用“read_list” 。

def read_list(self, object_list, bundle):
    """
    Returns a list of all the objects a user is allowed to read.

    Should return an empty list if none are allowed.

    Returns the entire list by default.
    """
    return object_list

您更改的代码如下所示:

from tastypie.authorization import Authorization
from tastypie.exceptions import Unauthorized


class MyAuthorization(Authorization):
    def read_list(self, object_list, bundle):
        print request.user
        return object_list.filter(pk=bundle.request.user.pk)


class MyUserResource(ModelResource):
    class Meta:
        queryset = ReaderUser.objects.all()
        resource_name = 'me'
        list_allowed_methods = []
        detail_allowed_methods = ['get']
        authorization = MyAuthorization()
        authentication = SessionAuthentication()
        excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login')

    def prepend_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
        ]

或者,您可以使用资源级别的授权(在您的情况下要简单得多):

class MyUserResource(ModelResource):
    class Meta:
        queryset = ReaderUser.objects.all()
        resource_name = 'me'
        list_allowed_methods = []
        detail_allowed_methods = ['get']
        authentication = SessionAuthentication()
        excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login')

    def authorized_read_list(self, object_list, bundle):
        print request.user
        return object_list.filter(pk=bundle.request.user.pk)

    def prepend_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
        ]

请注意,在这种情况下不需要“授权”元数据。

如果您有一种通用且通用的方式来对共享相同授权类的不同资源应用权限,则使用“授权”元是很好的。

于 2015-12-27T12:07:58.253 回答