Skip to content

Commit

Permalink
Force storage.file_overwrite to True
Browse files Browse the repository at this point in the history
This fixes uploading a static file with the same name twice resulting in
a second file with a randomized suffix in the name, and of course the
first one never to be updated to the newer version.

This is a bit related to antonagestam#30 and basically has the same fix. It's a bit
surprising that this never was a problem before, but that's probably
because, in contrary to the setting from antonagestam#30, that default in
django-storages for this setting is the one we need. This also means
that the impact of this change is a lot smaller. It only affects
projects which have manually set this setting to False, most likely to
prevent user uploads from overwriting each other.
  • Loading branch information
Gwildor committed Feb 27, 2017
1 parent 2e505ed commit 8ba488d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions collectfast/management/commands/collectstatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ def __init__(self, *args, **kwargs):
self.tasks = []
self.etags = {}
self.storage.preload_metadata = True
self.storage.file_overwrite = True
self.collectfast_enabled = settings.enabled
if not settings.preload_metadata_enabled:
warnings.warn(
"Collectfast does not work properly without "
"`AWS_PRELOAD_METADATA` set to `True`. Overriding "
"`storage.preload_metadata` and continuing.")

if not settings.file_overwrite_enabled:
warnings.warn(
"Collectfast does not work properly without "
"`AWS_S3_FILE_OVERWRITE` set to `True`. Overriding "
"`storage.file_overwrite` and continuing.")

def set_options(self, **options):
"""
Set options and handle deprecation.
Expand Down
2 changes: 2 additions & 0 deletions collectfast/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
enabled = getattr(settings, "COLLECTFAST_ENABLED", True)
preload_metadata_enabled = True is getattr(
settings, 'AWS_PRELOAD_METADATA', False)
file_overwrite_enabled = True is getattr(
settings, 'AWS_S3_FILE_OVERWRITE', False)
12 changes: 12 additions & 0 deletions collectfast/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ def test_warn_preload_metadata(case):
case.assertIn('AWS_PRELOAD_METADATA', str(w[0].message))


@test
@override_setting("file_overwrite_enabled", False)
@with_bucket
def test_warn_file_overwrite(case):
clean_static_dir()
create_static_file()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
call_collectstatic()
case.assertIn('AWS_S3_FILE_OVERWRITE', str(w[0].message))


@test
@override_setting("enabled", False)
@with_bucket
Expand Down
1 change: 1 addition & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def main():
"STATICFILES_STORAGE": "storages.backends.s3boto3.S3Boto3Storage",
"MIDDLEWARE_CLASSES": [],
"AWS_PRELOAD_METADATA": True,
"AWS_S3_FILE_OVERWRITE": True,
"AWS_STORAGE_BUCKET_NAME": "collectfast",
})

Expand Down

0 comments on commit 8ba488d

Please sign in to comment.