From d0f494eee2685565d6234a6e8214275ce9bb2751 Mon Sep 17 00:00:00 2001 From: Anton Agestam Date: Sat, 17 Mar 2018 15:02:15 +0100 Subject: [PATCH] only check preload_metadata if the storage class is boto --- .gitignore | 2 ++ collectfast/boto.py | 15 ++++++++++++- .../management/commands/collectstatic.py | 5 +++-- collectfast/tests/no_preload_metadata.py | 9 ++++++++ collectfast/tests/test_command.py | 22 ++++++++++++++----- collectfast/tests/utils.py | 3 +-- 6 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 collectfast/tests/no_preload_metadata.py diff --git a/.gitignore b/.gitignore index ce1a4cf..0b117ed 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ *.egg-info /static/ /static_root/ +build/ +dist/ diff --git a/collectfast/boto.py b/collectfast/boto.py index 87722af..c41294c 100644 --- a/collectfast/boto.py +++ b/collectfast/boto.py @@ -1,8 +1,21 @@ from . import settings +try: + from storages.backends.s3boto import S3BotoStorage + from storages.backends.s3boto3 import S3Boto3Storage + has_boto = True +except: + has_boto = False + def is_boto3(storage): - return type(storage).__name__ == 'S3Boto3Storage' + return has_boto and isinstance(storage, S3Boto3Storage) + + +def is_boto(storage): + return has_boto and ( + isinstance(storage, S3BotoStorage) or + isinstance(storage, S3Boto3Storage)) def reset_connection(storage): diff --git a/collectfast/management/commands/collectstatic.py b/collectfast/management/commands/collectstatic.py index adbd30e..94e445a 100644 --- a/collectfast/management/commands/collectstatic.py +++ b/collectfast/management/commands/collectstatic.py @@ -6,7 +6,7 @@ from django.utils.encoding import smart_str from collectfast.etag import should_copy_file -from collectfast.boto import reset_connection +from collectfast.boto import reset_connection, is_boto from collectfast import settings @@ -32,7 +32,8 @@ def __init__(self, *args, **kwargs): self.tasks = [] self.etags = {} self.collectfast_enabled = settings.enabled - if self.storage.preload_metadata is not True: + + if is_boto(self.storage) and self.storage.preload_metadata is not True: self.storage.preload_metadata = True warnings.warn( "Collectfast does not work properly without " diff --git a/collectfast/tests/no_preload_metadata.py b/collectfast/tests/no_preload_metadata.py new file mode 100644 index 0000000..4ec539d --- /dev/null +++ b/collectfast/tests/no_preload_metadata.py @@ -0,0 +1,9 @@ +from storages.backends.s3boto3 import S3Boto3Storage + + +class NPM(S3Boto3Storage): + """ + Dummy class for testing that collectfast warns about overriding the + `preload_metadata` attriute. + """ + preload_metadata = False diff --git a/collectfast/tests/test_command.py b/collectfast/tests/test_command.py index d1516a0..fd8bb09 100644 --- a/collectfast/tests/test_command.py +++ b/collectfast/tests/test_command.py @@ -2,7 +2,7 @@ from django.core.management import call_command from django.utils.six import StringIO -from mock import patch +from django.test import override_settings as override_django_settings from .utils import test, clean_static_dir, create_static_file, override_setting from .utils import with_bucket, override_storage_attr @@ -42,9 +42,9 @@ def test_threads(case): @test @with_bucket -@patch('django.contrib.staticfiles.management.commands.collectstatic.staticfiles_storage') # noqa -def test_warn_preload_metadata(case, mocked): - mocked.staticfiles_storage.return_value = False +@override_django_settings( + STATICFILES_STORAGE="collectfast.tests.no_preload_metadata.NPM") +def test_warn_preload_metadata(case): clean_static_dir() create_static_file() with warnings.catch_warnings(record=True) as w: @@ -54,12 +54,22 @@ def test_warn_preload_metadata(case, mocked): @test -@override_setting("enabled", False) @with_bucket def test_collectfast_disabled(case): clean_static_dir() create_static_file() - call_collectstatic() + result = call_collectstatic(disable_collectfast=True) + case.assertIn("1 static file copied.", result) + + +@test +@override_django_settings( + STATICFILES_STORAGE="django.contrib.staticfiles.storage.StaticFilesStorage") +def test_collectfast_disabled_default_storage(case): + clean_static_dir() + create_static_file() + result = call_collectstatic(disable_collectfast=True) + case.assertIn("1 static file copied.", result) @test diff --git a/collectfast/tests/utils.py b/collectfast/tests/utils.py index 25f774c..964bcda 100644 --- a/collectfast/tests/utils.py +++ b/collectfast/tests/utils.py @@ -30,8 +30,7 @@ def with_bucket(func): @functools.wraps(func) @moto.mock_s3 def wraps(*args, **kwargs): - boto.connect_s3().create_bucket( - django_settings.AWS_STORAGE_BUCKET_NAME) + boto.connect_s3().create_bucket(django_settings.AWS_STORAGE_BUCKET_NAME) return func(*args, **kwargs) return wraps