Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
only check preload_metadata if the storage class is boto
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam committed Mar 17, 2018
1 parent 8f47529 commit d0f494e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,5 @@
*.egg-info
/static/
/static_root/
build/
dist/
15 changes: 14 additions & 1 deletion 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):
Expand Down
5 changes: 3 additions & 2 deletions collectfast/management/commands/collectstatic.py
Expand Up @@ -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


Expand All @@ -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 "
Expand Down
9 changes: 9 additions & 0 deletions 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
22 changes: 16 additions & 6 deletions collectfast/tests/test_command.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions collectfast/tests/utils.py
Expand Up @@ -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

Expand Down

0 comments on commit d0f494e

Please sign in to comment.