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

Fix 120 #127

Merged
merged 1 commit into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link

@pachewise pachewise Mar 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this work if we didn't have boto/boto3 installed? (how do we test that)

edit: thinking through it, it should work, but I'm thinking we should cover that test case.

tested it locally, this works even if you don't have boto/boto3.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the isinstance(storage, S3Boto3Storage) will only get evaluated if has_boto is True, which will only happen if the import is successful. Would be good to test this but I think it might be a lot of effort.



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
22 changes: 16 additions & 6 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 @@ -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
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