Thumbnails for images and videos #87
@ -34,12 +34,13 @@ def make_thumbnails(file_id: int) -> None:
|
||||
"""Generate thumbnails for a given file, store them in thumbnail and metadata columns."""
|
||||
file = files.models.File.objects.get(pk=file_id)
|
||||
args = {'pk': file_id, 'type': file.get_type_display()}
|
||||
if not file.is_image and not file.is_video:
|
||||
logger.warning('File pk=%(pk)s is neither an image nor a video ("%(type)s")', args)
|
||||
return
|
||||
|
||||
assert file.validation.is_ok, f'File pk={file_id} is flagged'
|
||||
assert file.is_image or file.is_video, f'File pk={file_id} is neither image nor video'
|
||||
if not file.is_image and not file.is_video:
|
||||
logger.error('File pk=%(pk)s of type "%(type)s" is neither an image nor a video', args)
|
||||
return
|
||||
if not file.validation.is_ok:
|
||||
logger.error("File pk={pk} is flagged, won't make thumbnails".format(**args))
|
||||
return
|
||||
|
||||
# For an image, source of the thumbnails is the original image
|
||||
source_path = file.source.path
|
||||
@ -65,4 +66,6 @@ def make_thumbnails(file_id: int) -> None:
|
||||
file.metadata.update({'thumbnails': thumbnails})
|
||||
update_fields.add('metadata')
|
||||
if update_fields:
|
||||
args['update_fields'] = update_fields
|
||||
logger.info('Made thumbnails for file pk=%(pk)s, updating %(update_fields)s', args)
|
||||
file.save(update_fields=update_fields)
|
||||
|
@ -1,5 +1,6 @@
|
||||
from unittest.mock import patch
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
import logging
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
@ -18,19 +19,37 @@ class TasksTest(TestCase):
|
||||
with self.assertRaises(files.models.File.validation.RelatedObjectDoesNotExist):
|
||||
make_thumbnails.task_function(file_id=file.pk)
|
||||
|
||||
def test_make_thumbnails_fails_when_validation_not_ok(self):
|
||||
@patch('files.utils.make_thumbnails')
|
||||
def test_make_thumbnails_fails_when_validation_not_ok(self, mock_make_thumbnails):
|
||||
file = FileFactory(original_hash='foobar', source='file/original_image_source.jpg')
|
||||
files.models.FileValidation.objects.create(file=file, is_ok=False, results={})
|
||||
|
||||
with self.assertRaises(AssertionError):
|
||||
with self.assertLogs(level=logging.ERROR) as logs:
|
||||
make_thumbnails.task_function(file_id=file.pk)
|
||||
|
||||
def test_make_thumbnails_fails_when_not_image_or_video(self):
|
||||
self.maxDiff = None
|
||||
self.assertEqual(
|
||||
logs.output[0], f"ERROR:files.tasks:File pk={file.pk} is flagged, won't make thumbnails"
|
||||
)
|
||||
|
||||
mock_make_thumbnails.assert_not_called()
|
||||
|
||||
@patch('files.utils.make_thumbnails')
|
||||
def test_make_thumbnails_fails_when_not_image_or_video(self, mock_make_thumbnails):
|
||||
file = FileFactory(
|
||||
original_hash='foobar', source='file/source.zip', type=files.models.File.TYPES.THEME
|
||||
)
|
||||
|
||||
make_thumbnails.task_function(file_id=file.pk)
|
||||
with self.assertLogs(level=logging.ERROR) as logs:
|
||||
make_thumbnails.task_function(file_id=file.pk)
|
||||
|
||||
self.maxDiff = None
|
||||
self.assertEqual(
|
||||
logs.output[0],
|
||||
f'ERROR:files.tasks:File pk={file.pk} of type "Theme" is neither an image nor a video',
|
||||
)
|
||||
|
||||
mock_make_thumbnails.assert_not_called()
|
||||
|
||||
@patch('files.utils.resize_image')
|
||||
@patch('files.utils.Image')
|
||||
|
Loading…
Reference in New Issue
Block a user