Browse Source

replaced downlaods count with filesize, test editor endpoint for returning attachments

Rafał Pitoń 8 years ago
parent
commit
e0743bdf6a

+ 1 - 0
misago/threads/api/attachments.py

@@ -38,6 +38,7 @@ class AttachmentViewSet(viewsets.ViewSet):
         attachment = Attachment(
             secret=Attachment.generate_new_secret(),
             filetype=filetype,
+            size=upload.size,
             uploader=request.user,
             uploader_name=request.user.username,
             uploader_slug=request.user.slug,

+ 1 - 1
misago/threads/migrations/0001_initial.py

@@ -230,10 +230,10 @@ class Migration(migrations.Migration):
                 ('uploader_slug', models.CharField(max_length=255)),
                 ('uploader_ip', models.GenericIPAddressField()),
                 ('filename', models.CharField(max_length=255)),
+                ('size', models.PositiveIntegerField(default=0)),
                 ('thumbnail', models.ImageField(blank=True, null=True, upload_to='attachments')),
                 ('image', models.ImageField(blank=True, null=True, upload_to='attachments')),
                 ('file', models.FileField(blank=True, null=True, upload_to='attachments')),
-                ('downloads', models.PositiveIntegerField(default=0)),
                 ('post', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='misago_threads.Post')),
             ],
         ),

+ 1 - 2
misago/threads/models/attachment.py

@@ -33,13 +33,12 @@ class Attachment(models.Model):
     uploader_ip = models.GenericIPAddressField()
 
     filename = models.CharField(max_length=255)
+    size = models.PositiveIntegerField(default=0)
 
     thumbnail = models.ImageField(blank=True, null=True, upload_to='attachments')
     image = models.ImageField(blank=True, null=True, upload_to='attachments')
     file = models.FileField(blank=True, null=True, upload_to='attachments')
 
-    downloads = models.PositiveIntegerField(default=0)
-
     @classmethod
     def generate_new_secret(cls):
         return get_random_string(settings.MISAGO_ATTACHMENT_SECRET_LENGTH)

+ 1 - 1
misago/threads/serializers/attachment.py

@@ -30,7 +30,7 @@ class AttachmentSerializer(serializers.ModelSerializer):
             'uploader_name',
             'uploader_ip',
             'filename',
-            'downloads',
+            'size',
 
             'acl',
             'is_image',

+ 0 - 29
misago/threads/tests/test_attachmentview.py

@@ -80,9 +80,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
 
         return attachment
 
-    def get_downloads_count(self):
-        return Attachment.objects.order_by('id').last().downloads
-
     def assertIs404(self, response):
         self.assertEqual(response.status_code, 302)
         self.assertTrue(response['location'].endswith(settings.MISAGO_404_IMAGE))
@@ -116,8 +113,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
 
         self.assertIs404(response)
 
-        self.assertEqual(self.get_downloads_count(), 0)
-
     def test_other_user_file_no_permission(self):
         """user tries to retrieve other user's file without perm"""
         attachment = self.upload_document(by_other_user=True)
@@ -127,8 +122,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertIs403(response)
 
-        self.assertEqual(self.get_downloads_count(), 0)
-
     def test_other_user_orphaned_file(self):
         """user tries to retrieve other user's orphaned file"""
         attachment = self.upload_document(is_orphaned=True, by_other_user=True)
@@ -136,8 +129,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertIs403(response)
 
-        self.assertEqual(self.get_downloads_count(), 0)
-
     def test_document_thumbnail(self):
         """user tries to retrieve thumbnail from non-image attachment"""
         attachment = self.upload_document()
@@ -148,8 +139,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         }))
         self.assertIs404(response)
 
-        self.assertEqual(self.get_downloads_count(), 0)
-
     def test_no_role(self):
         """user tries to retrieve attachment without perm to its type"""
         attachment = self.upload_document()
@@ -160,8 +149,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertIs403(response)
 
-        self.assertEqual(self.get_downloads_count(), 0)
-
     def test_type_disabled(self):
         """user tries to retrieve attachment the type disabled downloads"""
         attachment = self.upload_document()
@@ -172,8 +159,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertIs403(response)
 
-        self.assertEqual(self.get_downloads_count(), 0)
-
     def test_locked_type(self):
         """user retrieves own locked file"""
         attachment = self.upload_document()
@@ -184,8 +169,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertSuccess(response)
 
-        self.assertEqual(self.get_downloads_count(), 1)
-
     def test_own_file(self):
         """user retrieves own file"""
         attachment = self.upload_document()
@@ -200,8 +183,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertSuccess(response)
 
-        self.assertEqual(self.get_downloads_count(), 1)
-
     def test_other_user_orphaned_file_is_staff(self):
         """user retrieves other user's orphaned file because he is staff"""
         attachment = self.upload_document(is_orphaned=True, by_other_user=True)
@@ -212,8 +193,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertSuccess(response)
 
-        self.assertEqual(self.get_downloads_count(), 1)
-
     def test_orphaned_file_is_uploader(self):
         """user retrieves orphaned file because he is its uploader"""
         attachment = self.upload_document(is_orphaned=True)
@@ -221,8 +200,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertSuccess(response)
 
-        self.assertEqual(self.get_downloads_count(), 1)
-
     def test_has_role(self):
         """user retrieves file he has roles to download"""
         attachment = self.upload_document()
@@ -233,8 +210,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertSuccess(response)
 
-        self.assertEqual(self.get_downloads_count(), 1)
-
     def test_image(self):
         """user retrieves """
         attachment = self.upload_image()
@@ -242,13 +217,9 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
         response = self.client.get(attachment.get_absolute_url())
         self.assertSuccess(response)
 
-        self.assertEqual(self.get_downloads_count(), 1)
-
     def test_image_thumb(self):
         """user retrieves image's thumbnail"""
         attachment = self.upload_image()
 
         response = self.client.get(attachment.get_thumbnail_url())
         self.assertSuccess(response)
-
-        self.assertEqual(self.get_downloads_count(), 0)

+ 36 - 2
misago/threads/tests/test_threads_editor_api.py

@@ -1,13 +1,21 @@
 import json
+import os
 
 from django.core.urlresolvers import reverse
 from django.utils.encoding import smart_str
 
+from misago.acl import add_acl
 from misago.acl.testutils import override_acl
 from misago.categories.models import Category
 from misago.users.testutils import AuthenticatedUserTestCase
 
 from .. import testutils
+from ..models import Attachment
+from ..serializers import AttachmentSerializer
+
+
+TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
+TEST_DOCUMENT_PATH = os.path.join(TESTFILES_DIR, 'document.pdf')
 
 
 class EditorApiTestCase(AuthenticatedUserTestCase):
@@ -618,12 +626,34 @@ class EditReplyEditorApiTests(EditorApiTestCase):
 
     def test_edit(self):
         """endpoint returns valid configuration for editor"""
+        # TODO: HERE UPLOAD ATTACHMENT OR TWO AND TIE THEM TO EDITED POST
+        for i in range(3):
+            self.override_acl({
+                'max_attachment_size': 1000,
+            })
+
+            with open(TEST_DOCUMENT_PATH, 'rb') as upload:
+                response = self.client.post(reverse('misago:api:attachment-list'), data={
+                    'upload': upload
+                })
+            self.assertEqual(response.status_code, 200)
+
+        attachments = list(Attachment.objects.order_by('id'))
+
+        attachments[0].uploader = None
+        attachments[0].save()
+
+        for attachment in attachments[:2]:
+            attachment.post = self.post
+            attachment.save()
+
         self.override_acl({
             'can_edit_posts': 1,
         })
-
         response = self.client.get(self.api_link)
 
+        map(lambda a: add_acl(self.user, a), attachments)
+
         self.assertEqual(response.status_code, 200)
         self.assertEqual(json.loads(smart_str(response.content)), {
             'id': self.post.pk,
@@ -631,5 +661,9 @@ class EditReplyEditorApiTests(EditorApiTestCase):
             'post': self.post.original,
             'can_protect': False,
             'is_protected': self.post.is_protected,
-            'poster': self.post.poster_name
+            'poster': self.post.poster_name,
+            'attachments': [
+                AttachmentSerializer(attachments[1], context={'user': self.user}).data,
+                AttachmentSerializer(attachments[0], context={'user': self.user}).data,
+            ]
         })

+ 0 - 4
misago/threads/views/attachment.py

@@ -30,10 +30,6 @@ def serve_file(request, pk, secret, thumbnail):
     if not request.user.is_staff:
         allow_file_download(request, attachment)
 
-    if not thumbnail:
-        attachment.downloads = F('downloads') + 1
-        attachment.save(update_fields=['downloads'])
-
     if attachment.is_image:
         if thumbnail:
             return attachment.thumbnail.url