diff --git a/bookwyrm/preview_images.py b/bookwyrm/preview_images.py index 0d9f26e7d..41dbf8bc7 100644 --- a/bookwyrm/preview_images.py +++ b/bookwyrm/preview_images.py @@ -309,33 +309,33 @@ def save_and_cleanup(image, instance=None): file_name = "%s-%s.jpg" % (str(instance.id), str(uuid4())) image_buffer = BytesIO() - # try: try: - old_path = instance.preview_image.path - except ValueError: - old_path = "" + try: + old_path = instance.preview_image.path + except ValueError: + old_path = "" - # Save - image.save(image_buffer, format="jpeg", quality=75) - instance.preview_image = InMemoryUploadedFile( - ContentFile(image_buffer.getvalue()), - "preview_image", - file_name, - "image/jpg", - image_buffer.tell(), - None, - ) + # Save + image.save(image_buffer, format="jpeg", quality=75) + instance.preview_image = InMemoryUploadedFile( + ContentFile(image_buffer.getvalue()), + "preview_image", + file_name, + "image/jpg", + image_buffer.tell(), + None, + ) - save_without_broadcast = isinstance(instance, (models.Book, models.User)) - if save_without_broadcast: - instance.save(update_fields=["preview_image"], broadcast=False) - else: - instance.save(update_fields=["preview_image"]) + save_without_broadcast = isinstance(instance, (models.Book, models.User)) + if save_without_broadcast: + instance.save(update_fields=["preview_image"], broadcast=False) + else: + instance.save(update_fields=["preview_image"]) - # Clean up old file after saving - if os.path.exists(old_path): - os.remove(old_path) - # finally: + # Clean up old file after saving + if os.path.exists(old_path): + os.remove(old_path) + finally: image_buffer.close() diff --git a/bookwyrm/tests/test_preview_images.py b/bookwyrm/tests/test_preview_images.py index 2777fc488..a48c60803 100644 --- a/bookwyrm/tests/test_preview_images.py +++ b/bookwyrm/tests/test_preview_images.py @@ -46,7 +46,7 @@ class PreviewImages(TestCase): ) with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): self.work = models.Work.objects.create(title="Test Work") - self.book = models.Edition.objects.create( + self.edition = models.Edition.objects.create( title="Example Edition", remote_id="https://example.com/book/1", parent_work=self.work, @@ -71,37 +71,34 @@ class PreviewImages(TestCase): ) def test_site_preview(self, *args, **kwargs): - """generate site preview""" + """generate site preview""" generate_site_preview_image_task() self.site.refresh_from_db() self.assertIsInstance(self.site.preview_image, ImageFieldFile) - result = Image.open(self.site.preview_image) - self.assertEqual( - result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) - ) + self.assertIsNotNone(self.site.preview_image) + self.assertEqual(self.site.preview_image.width, settings.PREVIEW_IMG_WIDTH) + self.assertEqual(self.site.preview_image.height, settings.PREVIEW_IMG_HEIGHT) def test_edition_preview(self, *args, **kwargs): - """generate user preview""" - generate_edition_preview_image_task(self.book.id) + """generate edition preview""" + 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) - result = Image.open(self.book.preview_image) - self.assertEqual( - result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) - ) + self.assertIsInstance(self.edition.preview_image, ImageFieldFile) + self.assertIsNotNone(self.edition.preview_image) + self.assertEqual(self.edition.preview_image.width, settings.PREVIEW_IMG_WIDTH) + self.assertEqual(self.edition.preview_image.height, settings.PREVIEW_IMG_HEIGHT) def test_user_preview(self, *args, **kwargs): - """generate user preview""" + """generate user preview""" generate_user_preview_image_task(self.local_user.id) self.local_user.refresh_from_db() self.assertIsInstance(self.local_user.preview_image, ImageFieldFile) - result = Image.open(self.local_user.preview_image) - self.assertEqual( - result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) - ) + self.assertIsNotNone(self.local_user.preview_image) + self.assertEqual(self.local_user.preview_image.width, settings.PREVIEW_IMG_WIDTH) + self.assertEqual(self.local_user.preview_image.height, settings.PREVIEW_IMG_HEIGHT)