Try another way

This commit is contained in:
Joachim 2021-05-29 12:33:04 +02:00
parent 022182e114
commit cc0881b11c
2 changed files with 39 additions and 42 deletions

View file

@ -309,33 +309,33 @@ def save_and_cleanup(image, instance=None):
file_name = "%s-%s.jpg" % (str(instance.id), str(uuid4())) file_name = "%s-%s.jpg" % (str(instance.id), str(uuid4()))
image_buffer = BytesIO() image_buffer = BytesIO()
# try:
try: try:
old_path = instance.preview_image.path try:
except ValueError: old_path = instance.preview_image.path
old_path = "" except ValueError:
old_path = ""
# Save # Save
image.save(image_buffer, format="jpeg", quality=75) image.save(image_buffer, format="jpeg", quality=75)
instance.preview_image = InMemoryUploadedFile( instance.preview_image = InMemoryUploadedFile(
ContentFile(image_buffer.getvalue()), ContentFile(image_buffer.getvalue()),
"preview_image", "preview_image",
file_name, file_name,
"image/jpg", "image/jpg",
image_buffer.tell(), image_buffer.tell(),
None, None,
) )
save_without_broadcast = isinstance(instance, (models.Book, models.User)) save_without_broadcast = isinstance(instance, (models.Book, models.User))
if save_without_broadcast: if save_without_broadcast:
instance.save(update_fields=["preview_image"], broadcast=False) instance.save(update_fields=["preview_image"], broadcast=False)
else: else:
instance.save(update_fields=["preview_image"]) instance.save(update_fields=["preview_image"])
# Clean up old file after saving # Clean up old file after saving
if os.path.exists(old_path): if os.path.exists(old_path):
os.remove(old_path) os.remove(old_path)
# finally: finally:
image_buffer.close() image_buffer.close()

View file

@ -46,7 +46,7 @@ class PreviewImages(TestCase):
) )
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
self.work = models.Work.objects.create(title="Test Work") self.work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create( self.edition = models.Edition.objects.create(
title="Example Edition", title="Example Edition",
remote_id="https://example.com/book/1", remote_id="https://example.com/book/1",
parent_work=self.work, parent_work=self.work,
@ -71,37 +71,34 @@ class PreviewImages(TestCase):
) )
def test_site_preview(self, *args, **kwargs): def test_site_preview(self, *args, **kwargs):
"""generate site preview""" """generate site preview"""
generate_site_preview_image_task() generate_site_preview_image_task()
self.site.refresh_from_db() self.site.refresh_from_db()
self.assertIsInstance(self.site.preview_image, ImageFieldFile) self.assertIsInstance(self.site.preview_image, ImageFieldFile)
result = Image.open(self.site.preview_image) self.assertIsNotNone(self.site.preview_image)
self.assertEqual( self.assertEqual(self.site.preview_image.width, settings.PREVIEW_IMG_WIDTH)
result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) self.assertEqual(self.site.preview_image.height, settings.PREVIEW_IMG_HEIGHT)
)
def test_edition_preview(self, *args, **kwargs): def test_edition_preview(self, *args, **kwargs):
"""generate user preview""" """generate edition preview"""
generate_edition_preview_image_task(self.book.id) generate_edition_preview_image_task(self.edition.id)
self.book.refresh_from_db() self.edition.refresh_from_db()
self.assertIsInstance(self.book.preview_image, ImageFieldFile) self.assertIsInstance(self.edition.preview_image, ImageFieldFile)
result = Image.open(self.book.preview_image) self.assertIsNotNone(self.edition.preview_image)
self.assertEqual( self.assertEqual(self.edition.preview_image.width, settings.PREVIEW_IMG_WIDTH)
result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) self.assertEqual(self.edition.preview_image.height, settings.PREVIEW_IMG_HEIGHT)
)
def test_user_preview(self, *args, **kwargs): def test_user_preview(self, *args, **kwargs):
"""generate user preview""" """generate user preview"""
generate_user_preview_image_task(self.local_user.id) generate_user_preview_image_task(self.local_user.id)
self.local_user.refresh_from_db() self.local_user.refresh_from_db()
self.assertIsInstance(self.local_user.preview_image, ImageFieldFile) self.assertIsInstance(self.local_user.preview_image, ImageFieldFile)
result = Image.open(self.local_user.preview_image) self.assertIsNotNone(self.local_user.preview_image)
self.assertEqual( self.assertEqual(self.local_user.preview_image.width, settings.PREVIEW_IMG_WIDTH)
result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) self.assertEqual(self.local_user.preview_image.height, settings.PREVIEW_IMG_HEIGHT)
)