Django 1.6 compatibility issue: quoting issues in resource URIs #1040

Open
jleclanche opened this Issue Sep 27, 2013 · 0 comments

2 participants

@jleclanche

I have a user resource where detail_uri_name = "username", and username is an email address. This looks something like /api/v1/user/john@example.com.

Django 1.6 release notes mention the following:

When reversing URLs, Django didn’t apply urlquote() to arguments before interpolating them in URL patterns. This bug is fixed in Django 1.6. If you worked around this bug by applying URL quoting before passing arguments to reverse(), this may result in double-quoting. If this happens, simply remove the URL quoting from your code.

Now, user resources are like so: resource_uri: "/api/v1/user/john%40example.com/",.

Admittedly, I don't know which is the correct form. I don't think it should be encoding things in the output, but it causes a graver issue elsewhere.
When modifying resources that have a user foreignkey, we get the following crash:

Traceback (most recent call last):
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/resources.py", line 195, in wrapper
    response = callback(request, *args, **kwargs)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/resources.py", line 435, in dispatch_detail
    return self.dispatch('detail', request, **kwargs)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/resources.py", line 458, in dispatch
    response = method(request, **kwargs)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/resources.py", line 1603, in patch_detail
    self.update_in_place(request, bundle, deserialized)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/resources.py", line 1626, in update_in_place
    return self.obj_update(bundle=original_bundle, **kwargs)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/resources.py", line 2143, in obj_update
    bundle = self.full_hydrate(bundle)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/resources.py", line 876, in full_hydrate
    value = field_object.hydrate(bundle)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/fields.py", line 739, in hydrate
    return self.build_related_resource(value, request=bundle.request)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/fields.py", line 655, in build_related_resource
    return self.resource_from_uri(self.fk_resource, value, **kwargs)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/fields.py", line 574, in resource_from_uri
    obj = fk_resource.get_via_uri(uri, request=request)
  File "/home/adys/.local/share/virtualenvs/anthill/lib/python2.7/site-packages/tastypie/resources.py", line 802, in get_via_uri
    raise NotFound("The URL provided '%s' was not a link to a valid resource." % uri)
NotFound: The URL provided '/api/v1/user/john%40example.com/' was not a link to a valid resource.

Oops.
For now, a workaround I'm using is to add the following in get_via_uri in the user resource:

    def get_via_uri(self, uri, request=None):
        from urllib import unquote
        uri = unquote(uri)
        return super(UserResource, self).get_via_uri(uri, request)

But this needs to be taken care of for Django 1.6 compatibility.

@SeanHayes SeanHayes added this to the v0.13.4 milestone Feb 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment