Skip to content
Snippets Groups Projects
Commit 6277934d authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'handle-path-updates' into 'master'

Handle possible path updates when checking an image in CreateIIIFImage

See merge request !700
parents f0d1d3f6 2dc9b37c
No related branches found
No related tags found
1 merge request!700Handle possible path updates when checking an image in CreateIIIFImage
......@@ -117,12 +117,26 @@ class IIIFImageSerializer(ImageSerializer):
existing_image = Image.objects.filter(server=data['server'], path=data['path']).first()
if existing_image:
raise DuplicatedImage(existing_image.id)
return data
def create(self, validated_data):
image = Image.objects.create(**validated_data)
image.perform_check()
return image
image = Image(**data)
try:
image.perform_check(save=False)
except Exception as e:
raise ValidationError({
'detail': str(e)
})
# perform_check can alter the image's path since it updates it from info.json.
# This can once more cause duplicated images, so we re-check for an existing image.
existing_image = image.server.images.filter(path=image.path).first()
if existing_image:
raise DuplicatedImage(existing_image.id)
# Cannot return an Image instance from validate() so we update the data dict
for prop in ('width', 'height', 'path', 'status'):
data[prop] = getattr(image, prop)
return data
class ImagePathDefault(object):
......
......@@ -11,12 +11,13 @@ import responses
class ImageMethods:
def check_mock(self):
def check_mock(self, save=True):
self.path = 'image_path'
self.height = 1337
self.width = 4200
self.status = S3FileStatus.Checked
self.save()
if save:
self.save()
@override_settings(LOCAL_IMAGESERVER_ID=1)
......@@ -351,3 +352,24 @@ class TestImageApi(FixtureAPITestCase):
'__all__': ['Image with this path already exists'],
'id': created_image_id
})
@patch('arkindex.images.serializers.Image.perform_check', new=ImageMethods.check_mock)
def test_create_existing_iiif_image_check(self):
"""
Special case when an image does not look duplicated,
except the check algorithm updates its path and turns it into a duplicate
"""
self.client.force_login(self.admin)
img = self.server.images.create(
status=S3FileStatus.Unchecked,
path='image_path',
)
response = self.client.post(
reverse('api:iiif-image-create'),
{'url': 'https://test-server.eu/images/image_path2'},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(response.json(), {
'__all__': ['Image with this path already exists'],
'id': str(img.id),
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment