Browse Source

further cleanup on attachment model and tests harness

Rafał Pitoń 8 years ago
parent
commit
54954b8638
2 changed files with 37 additions and 0 deletions
  1. 12 0
      misago/threads/models/attachment.py
  2. 25 0
      misago/threads/tests/test_attachments_api.py

+ 12 - 0
misago/threads/models/attachment.py

@@ -60,6 +60,18 @@ class Attachment(models.Model):
     image = models.ImageField(blank=True, null=True, upload_to=upload_to)
     image = models.ImageField(blank=True, null=True, upload_to=upload_to)
     file = models.FileField(blank=True, null=True, upload_to=upload_to)
     file = models.FileField(blank=True, null=True, upload_to=upload_to)
 
 
+    def delete(self, *args, **kwargs):
+        self.delete_files()
+        return super(Attachment, self).delete(*args, **kwargs)
+
+    def delete_files(self):
+        if self.thumbnail:
+            self.thumbnail.delete(save=False)
+        if self.image:
+            self.image.delete(save=False)
+        if self.file:
+            self.file.delete(save=False)
+
     @classmethod
     @classmethod
     def generate_new_secret(cls):
     def generate_new_secret(cls):
         return get_random_string(settings.MISAGO_ATTACHMENT_SECRET_LENGTH)
         return get_random_string(settings.MISAGO_ATTACHMENT_SECRET_LENGTH)

+ 25 - 0
misago/threads/tests/test_attachments_api.py

@@ -1,9 +1,11 @@
 import json
 import json
 import os
 import os
 
 
+from django.conf import settings
 from django.core.urlresolvers import reverse
 from django.core.urlresolvers import reverse
 from django.utils import six
 from django.utils import six
 from django.utils.encoding import smart_str
 from django.utils.encoding import smart_str
+from PIL import Image
 
 
 from misago.acl.models import Role
 from misago.acl.models import Role
 from misago.acl.testutils import override_acl
 from misago.acl.testutils import override_acl
@@ -210,6 +212,12 @@ class AttachmentsApiTestCase(AuthenticatedUserTestCase):
         self.assertIsNone(response_json['url']['thumb'])
         self.assertIsNone(response_json['url']['thumb'])
         self.assertEqual(response_json['url']['uploader'], self.user.get_absolute_url())
         self.assertEqual(response_json['url']['uploader'], self.user.get_absolute_url())
 
 
+        # files associated with attachment are deleted on its deletion
+        file_path = attachment.file.path
+        self.assertTrue(os.path.exists(file_path))
+        attachment.delete()
+        self.assertFalse(os.path.exists(file_path))
+
     def test_small_image_upload(self):
     def test_small_image_upload(self):
         """successful small image upload creates orphan attachment without thumbnail"""
         """successful small image upload creates orphan attachment without thumbnail"""
         attachment_type = AttachmentType.objects.create(
         attachment_type = AttachmentType.objects.create(
@@ -281,6 +289,23 @@ class AttachmentsApiTestCase(AuthenticatedUserTestCase):
         self.assertEqual(response_json['url']['thumb'], attachment.get_thumbnail_url())
         self.assertEqual(response_json['url']['thumb'], attachment.get_thumbnail_url())
         self.assertEqual(response_json['url']['uploader'], self.user.get_absolute_url())
         self.assertEqual(response_json['url']['uploader'], self.user.get_absolute_url())
 
 
+        # thumbnail was scaled down
+        thumbnail = Image.open(attachment.thumbnail.path)
+        self.assertEqual(thumbnail.size[0], settings.MISAGO_ATTACHMENT_IMAGE_SIZE_LIMIT[0])
+        self.assertLess(thumbnail.size[1], settings.MISAGO_ATTACHMENT_IMAGE_SIZE_LIMIT[1])
+
+        # files associated with attachment are deleted on its deletion
+        image_path = attachment.image.path
+        thumbnail_path = attachment.thumbnail.path
+
+        self.assertTrue(os.path.exists(image_path))
+        self.assertTrue(os.path.exists(thumbnail_path))
+
+        attachment.delete()
+
+        self.assertFalse(os.path.exists(image_path))
+        self.assertFalse(os.path.exists(thumbnail_path))
+
     def test_animated_image_upload(self):
     def test_animated_image_upload(self):
         """successful gif upload creates orphan attachment with thumbnail"""
         """successful gif upload creates orphan attachment with thumbnail"""
         attachment_type = AttachmentType.objects.create(
         attachment_type = AttachmentType.objects.create(