Refactor and implement download code natively

- Use modified port of Django 2.2 FileResponse.
- Remove Django DownloadView library.

Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2019-12-12 19:39:44 -04:00
parent 826f7fddf2
commit a7b31fc171
24 changed files with 355 additions and 278 deletions
+3 -5
View File
@@ -2,8 +2,6 @@ from __future__ import absolute_import, unicode_literals
from django.test import TestCase
from django_downloadview import assert_download_response
from mayan.apps.acls.tests.mixins import ACLTestCaseMixin
from mayan.apps.converter.tests.mixins import LayerTestCaseMixin
from mayan.apps.permissions.tests.mixins import PermissionTestCaseMixin
@@ -14,7 +12,7 @@ from mayan.apps.user_management.tests.mixins import UserTestMixin
from .mixins import (
ClientMethodsTestCaseMixin, ConnectionsCheckTestCaseMixin,
ContentTypeCheckTestCaseMixin, ModelTestCaseMixin,
ContentTypeCheckTestCaseMixin, DownloadTestCaseMixin, ModelTestCaseMixin,
OpenFileCheckTestCaseMixin, RandomPrimaryKeyModelMonkeyPatchMixin,
SilenceLoggerTestCaseMixin, TempfileCheckTestCasekMixin,
TestViewTestCaseMixin
@@ -22,7 +20,8 @@ from .mixins import (
class BaseTestCase(
LayerTestCaseMixin, SilenceLoggerTestCaseMixin, ConnectionsCheckTestCaseMixin,
LayerTestCaseMixin, SilenceLoggerTestCaseMixin,
ConnectionsCheckTestCaseMixin, DownloadTestCaseMixin,
RandomPrimaryKeyModelMonkeyPatchMixin, ACLTestCaseMixin,
ModelTestCaseMixin, OpenFileCheckTestCaseMixin, PermissionTestCaseMixin,
SmartSettingsTestCaseMixin, TempfileCheckTestCasekMixin, UserTestMixin,
@@ -31,7 +30,6 @@ class BaseTestCase(
"""
This is the most basic test case class any test in the project should use.
"""
assert_download_response = assert_download_response
class GenericViewTestCase(
+36 -1
View File
@@ -18,12 +18,16 @@ from django.http import HttpResponse
from django.template import Context, Template
from django.test.utils import ContextList
from django.urls import clear_url_caches, reverse
from django.utils.encoding import force_bytes
from django.utils.encoding import (
DjangoUnicodeDecodeError, force_bytes, force_text
)
from django.utils.six import PY3
from mayan.apps.acls.classes import ModelPermission
from mayan.apps.storage.settings import setting_temporary_directory
from ..compat import FileResponse
from .literals import (
TEST_SERVER_HOST, TEST_SERVER_SCHEME, TEST_VIEW_NAME, TEST_VIEW_URL
)
@@ -141,6 +145,37 @@ class ContentTypeCheckTestCaseMixin(object):
self.client = CustomClient()
class DownloadTestCaseMixin(object):
def assert_download_response(
self, response, content=None, filename=None, is_attachment=None,
mime_type=None
):
self.assertTrue(isinstance(response, FileResponse))
if filename:
self.assertEqual(
response[
'Content-Disposition'
].split('filename="')[1].split('"')[0], filename
)
if content:
response_content = b''.join(list(response))
try:
response_content = force_text(response_content)
except DjangoUnicodeDecodeError:
"""Leave as bytes"""
self.assertEqual(response_content, content)
if is_attachment is not None:
self.assertEqual(response['Content-Disposition'], 'attachment')
if mime_type:
self.assertTrue(response['Content-Type'].startswith(mime_type))
class EnvironmentTestCaseMixin(object):
def setUp(self):
super(EnvironmentTestCaseMixin, self).setUp()