简体   繁体   中英

Removing the list endpoint from a resource on tastypie

I have a Resource on my api that always return the logged-in user. The resource is read-only. I wanted the list uri to act as the detail uri, and remove the detail urls.

So, /api/v1/user/ would return the logged user, and any other url would fail. This is what I did to achieve this:

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        fields = ['email', 'name']
        authentication = MultiAuthentication(SessionAuthentication(), BasicAuthentication())
        authorization = Authorization()
        list_allowed_methods = []
        detail_allowed_methods = ['get']

    def base_urls(self):
        '''
        The list endpoint behaves as the list endpoint.
        '''
        return [
            url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
            url(r"^(?P<resource_name>%s)/schema%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_schema'), name="api_get_schema")
        ]

    def obj_get(self, bundle, **kwargs):
        '''
        Always returns the logged in user.
        '''
        return bundle.request.user

    def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_detail'):
        bundle_or_obj = None
        try:
            return self._build_reverse_url(url_name, kwargs=self.resource_uri_kwargs(bundle_or_obj))
        except NoReverseMatch:
            return ''

I used base_urls() instead of prepend_urls() because I wanted to remove the other urls.

It works fine, but when I hit the /api/v1/ url, I get this error:

Traceback:
File "/home/vagrant/workspace/expenses/venv/local/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/vagrant/workspace/expenses/venv/local/lib/python2.7/site-packages/django_tastypie-0.9.15-py2.7.egg/tastypie/api.py" in wrapper
  80.                 return getattr(self, view)(request, *args, **kwargs)
File "/home/vagrant/workspace/expenses/venv/local/lib/python2.7/site-packages/django_tastypie-0.9.15-py2.7.egg/tastypie/api.py" in top_level
  137.                     'resource_name': name,
File "/home/vagrant/workspace/expenses/venv/local/lib/python2.7/site-packages/django_tastypie-0.9.15-py2.7.egg/tastypie/api.py" in _build_reverse_url
  166.         return reverse(name, args=args, kwargs=kwargs)
File "/home/vagrant/workspace/expenses/venv/local/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/core/urlresolvers.py" in reverse
  496.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/vagrant/workspace/expenses/venv/local/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/core/urlresolvers.py" in _reverse_with_prefix
  416.                 "arguments '%s' not found." % (lookup_view_s, args, kwargs))

Exception Type: NoReverseMatch at /api/v1/
Exception Value: Reverse for 'api_dispatch_list' with arguments '()' and keyword arguments '{'api_name': u'v1', 'resource_name': 'user'}' not found.

It's trying to reach the missing list endpoint. How to I get rid of this?

Thanks.


Thanks to Rudy's guidance, I ended up with the following:

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        fields = ['email', 'name']
        authentication = MultiAuthentication(SessionAuthentication(), BasicAuthentication())
        authorization = Authorization()
        list_allowed_methods = []
        detail_allowed_methods = ['get']

    def dispatch_list(self, request, **kwargs):
        return self.dispatch_detail(request, **kwargs)

    def obj_get(self, bundle, **kwargs):
        '''
        Always returns the logged in user.
        '''
        return bundle.request.user

    def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_list'):
        bundle_or_obj = None
        try:
            return self._build_reverse_url(url_name, kwargs=self.resource_uri_kwargs(bundle_or_obj))
        except NoReverseMatch:
            return ''

You should use a custom Authorization class that blocks the list endpoints and gracefully raises an error instead of just removing the URL all together so it still plays nicely with Tastypie.

class UserObjectsOnlyAuthorization(Authorization):
def read_list(self, object_list, bundle):
    raise Unauthorized("Sorry, no list reads.")

def read_detail(self, object_list, bundle):
    # Is the requested object the user?
    return bundle.obj == bundle.request.user

def create_list(self, object_list, bundle):
    raise Unauthorized("Sorry, no creates.")

def create_detail(self, object_list, bundle):
    raise Unauthorized("Sorry, no creates.")

def update_list(self, object_list, bundle):
    raise Unauthorized("Sorry, no updates.")

def update_detail(self, object_list, bundle):
    raise Unauthorized("Sorry, no updates.")

def delete_list(self, object_list, bundle):
    # Sorry user, no deletes for you!
    raise Unauthorized("Sorry, no deletes.")

def delete_detail(self, object_list, bundle):
    raise Unauthorized("Sorry, no deletes.")

EDIT:

If you'd like to force this API always to be a 'Detail' request then you can override Tastypie's built in functions. Basically if you specify an ID in the URL then tastypie routes it to be a _detail request and if you don't then it routes it to be a _list request. If you override the dispatch functions which detect this, you can change all requests to this resource to be _detail and specify what the primary key is to look up your user. This may be a bit more hacky, but will accomplish what you want:

def dispatch(self, request_type, request, **kwargs):
    # Force this to be a single User object
    return super(UserResource, self).dispatch('detail', request, **kwargs)

def get_detail(self, request, **kwargs):
    # Place the authenticated user's id in the get detail request
    kwargs['id'] = request.user.pk
    return super(UserResource, self).get_detail(request, **kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM