【问题标题】:python/tastypie: authorization on a non-resource?python/tastypie:非资源授权?
【发布时间】:2015-10-20 15:09:48
【问题描述】:

为我们这里提供的 API 使用 sweetpie。

所以主要的API类是:

from tastypie.api import Api

class TheAPI(Api):
  def prepend_urls(self):
    return [
     url(r"^(?P<api_name>%s)/reset/(?P<pk>\w+)%s$" % (self.api_name, trailing_slash()), self.wrap_view('reset'), name="api_reset"),
    ]
  def reset(self, request, **kwargs):
      #do the work

到目前为止一切顺利。 我的问题在于我想在此调用中使用 ApiAuthentication。我不希望任何人能够使用重置功能(在 URL 中有一个唯一的代码,但仍然存在)。

但由于这不是资源,我不知道该怎么做。我尝试在这个类中添加一个 Meta 类,但它似乎被忽略了。

我能想到的唯一其他 hack 是发明了一些 FakeResource 来封装此功能,但这感觉很奇怪,因为它不是资源。 有什么想法吗?

【问题讨论】:

  • 嘿,不确定该代码是否正确。 prepend_urls 应该返回一个列表,并且在您的代码中您没有返回任何内容。

标签: python api tastypie


【解决方案1】:

在这里扩展常规资源并将authorizationauthentication 添加到Meta 类中没有什么坏处。

class BaseNonORMResource(Resource):

    class Meta:
        max_limit = None

        """
        (Using Tastypie's DjangoAuthorization which checks the permission a user has granted to them)
        and Tastypie's token based authentication approach
        """
        authorization = DjangoAuthorization()
        authentication = ApiKeyAuthentication()

        '''
        Specify allowed_methods, list_allowed_methods or detail_allowed_methods in each SubResource
        e.g. allowed_methods = ['get']
        '''
        allowed_methods = ['get', 'post', 'delete', 'patch', 'put']

然后用它来定义你的端点

class TheAPI(BaseNonORMResource):
  def prepend_urls(self):
    return [
         url(r"^(?P<api_name>%s)/reset/(?P<pk>\w+)%s$" % (self.api_name, trailing_slash()), self.wrap_view('reset'), name="api_reset"),
    ]

  def reset(self, request, **kwargs):
      #do the work

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2013-10-17
    • 2012-11-18
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多