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

Commit

Permalink
Merge 165b002 into 8f47529
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam committed Mar 16, 2018
2 parents 8f47529 + 165b002 commit 512280f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
*.egg-info
/static/
/static_root/
build/
dist/
15 changes: 14 additions & 1 deletion collectfast/boto.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
18 changes: 14 additions & 4 deletions collectfast/tests/test_command.py
Original file line number Diff line number Diff line change
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 @@ -62,6 +62,16 @@ def test_collectfast_disabled(case):
call_collectstatic()


@test
@override_django_settings(
STATICFILES_STORAGE="django.contrib.staticfiles.storage.StaticFilesStorage")
@override_setting("enabled", True)
def test_collectfast_disabled_default_storage(case):
clean_static_dir()
create_static_file()
call_collectstatic()


@test
@with_bucket
def test_disable_collectfast(case):
Expand Down
3 changes: 1 addition & 2 deletions collectfast/tests/utils.py
Original file line number Diff line number Diff line change
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 512280f

Please sign in to comment.