Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues POSTing to /api/search/iiif with trailing slashes in "id" #8

Open
donaldgray opened this issue Nov 18, 2020 · 0 comments
Open
Labels
bug Something isn't working

Comments

@donaldgray
Copy link
Member

We have a few object Id's that contain slashes (e.g. src:AB/1). These are used as-is in some URLs. If I use this URL as the "id" for psoting to /api/search/iiif, e.g.

{
  "contexts": [
    {
      "id": "https://mysite",
      "type": "Site"
    },
  ],
  "resource": { },
  "id": "http://mysite/objects/src:AB/1",
  "thumbnail": "https://my-thumbnails/full/!600,600/0/default.jpg"
}

I get a 400 back with body:

ImproperlyConfigured at /api/search/iiif
Could not resolve URL for hyperlinked relationship using view name "iiifresource-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.

Request Method: POST
Request URL: http://127.0.0.1:8000/api/search/iiif
Django Version: 3.1.3
Python Executable: /usr/local/bin/python3
Python Version: 3.8.6
Python Path: ['/app', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/site-packages']
Server time: Wed, 18 Nov 2020 16:32:45 +0000
Installed Applications:
['modeltranslation',
 'dal',
 'dal_select2',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_webserver',
 'django.contrib.postgres',
 'reversion',
 'rest_framework',
 'django_filters',
 'search']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/rest_framework/relations.py", line 399, in to_representation
    url = self.get_url(value, self.view_name, request, format)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/relations.py", line 337, in get_url
    return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/reverse.py", line 47, in reverse
    url = _reverse(viewname, args, kwargs, request, format, **extra)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/reverse.py", line 60, in _reverse
    url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
  File "/usr/local/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix
    raise NoReverseMatch(msg)

During handling of the above exception (Reverse for 'iiifresource-detail' with keyword arguments '{'pk': 'http://mysite/objects/src:AB/1'}' not found. 2 pattern(s) tried: ['api/search/iiif/(?P<pk>[^/]+)(?P<format>\\.[a-z0-9]+/?)$', 'api/search/iiif/(?P<pk>[^/]+)$']), another exception occurred:
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/generics.py", line 242, in post
    return self.create(request, *args, **kwargs)
  File "/app/search/views.py", line 260, in create
    manifest_data, manifest_headers = ingest_iiif(
  File "/app/search/views.py", line 233, in ingest_iiif
    return serializer.data, self.get_success_headers(serializer.data)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 548, in data
    ret = super().data
  File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 246, in data
    self._data = self.to_representation(self.instance)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 515, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/relations.py", line 414, in to_representation
    raise ImproperlyConfigured(msg % self.view_name)

Exception Type: ImproperlyConfigured at /api/search/iiif
Exception Value: Could not resolve URL for hyperlinked relationship using view name "iiifresource-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
Request information:

If I then POST the same request again I get a 400 with:

{
    "madoc_id": [
        "iiif resource with this Identifier (Madoc) already exists."
    ]
}

Also, if I GET /api/search/iiif I get the exception

ImproperlyConfigured at /api/search/iiif

Could not resolve URL for hyperlinked relationship using view name "iiifresource-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.

This is a fairly obscure one and easily avoided but thought I'd record.

@donaldgray donaldgray added the bug Something isn't working label Nov 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant