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

Commit

Permalink
Merge 1865d6c into 45c6820
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam committed Feb 23, 2020
2 parents 45c6820 + 1865d6c commit c970030
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
27 changes: 15 additions & 12 deletions collectfast/management/commands/collectstatic.py
Expand Up @@ -78,9 +78,8 @@ def handle(self, *args: Any, **options: Any) -> Optional[str]:
ret = super().handle(**options)
if not self.collectfast_enabled:
return ret
return "{} static file{} copied.".format(
self.num_copied_files, "" if self.num_copied_files == 1 else "s"
)
plural = "" if self.num_copied_files == 1 else "s"
return f"{self.num_copied_files} static file{plural} copied."

def maybe_copy_file(self, args: Task) -> None:
"""Determine if file should be copied or not and handle exceptions."""
Expand All @@ -90,7 +89,7 @@ def maybe_copy_file(self, args: Task) -> None:
self.strategy.pre_should_copy_hook()

if not self.strategy.should_copy_file(path, prefixed_path, source_storage):
self.log("Skipping '%s'" % path)
self.log(f"Skipping '{path}'")
return

self.num_copied_files += 1
Expand All @@ -113,12 +112,16 @@ def delete_file(
"""Override delete_file to skip modified time and exists lookups."""
if not self.collectfast_enabled:
return super().delete_file(path, prefixed_path, source_storage)
if not self.dry_run:
try:
self.log("Deleting '%s' on remote storage" % path)
self.storage.delete(prefixed_path)
except self.strategy.delete_not_found_exception:
pass
else:
self.log("Pretending to delete '%s'" % path)

if self.dry_run:
self.log(f"Pretending to delete '{path}'")
return True

self.log(f"Deleting '{path}' on remote storage")

try:
self.storage.delete(prefixed_path)
except self.strategy.delete_not_found_exception:
pass

return True
36 changes: 25 additions & 11 deletions collectfast/settings.py
@@ -1,21 +1,35 @@
from typing import Sequence
from typing import Container
from typing import Type
from typing import TypeVar

from django.conf import settings
from typing_extensions import Final


debug: Final[bool] = getattr(
settings, "COLLECTFAST_DEBUG", getattr(settings, "DEBUG", False)
T = TypeVar("T")


def _get_setting(type_: Type[T], key: str, default: T) -> T:
value = getattr(settings, key, default)
if not isinstance(value, type_):
raise ValueError(
f"The {key!r} setting must be of type {type_!r}, found {type(value)}"
)
return value


debug: Final = _get_setting(
bool, "COLLECTFAST_DEBUG", _get_setting(bool, "DEBUG", False)
)
cache_key_prefix: Final[str] = getattr(
settings, "COLLECTFAST_CACHE_KEY_PREFIX", "collectfast06_asset_"
cache_key_prefix: Final = _get_setting(
str, "COLLECTFAST_CACHE_KEY_PREFIX", "collectfast06_asset_"
)
cache: Final[str] = getattr(settings, "COLLECTFAST_CACHE", "default")
threads: Final[bool] = getattr(settings, "COLLECTFAST_THREADS", False)
enabled: Final[bool] = getattr(settings, "COLLECTFAST_ENABLED", True)
aws_is_gzipped: Final[bool] = getattr(settings, "AWS_IS_GZIPPED", False)
gzip_content_types: Final[Sequence[str]] = getattr(
settings,
cache: Final = _get_setting(str, "COLLECTFAST_CACHE", "default")
threads: Final = _get_setting(bool, "COLLECTFAST_THREADS", False)
enabled: Final = _get_setting(bool, "COLLECTFAST_ENABLED", True)
aws_is_gzipped: Final = _get_setting(bool, "AWS_IS_GZIPPED", False)
gzip_content_types: Final[Container] = _get_setting(
tuple,
"GZIP_CONTENT_TYPES",
(
"text/css",
Expand Down
19 changes: 19 additions & 0 deletions collectfast/tests/test_settings.py
@@ -0,0 +1,19 @@
import pytest
from django.test.utils import override_settings

from collectfast.settings import _get_setting


@override_settings(FOO=2)
def test_get_setting_returns_valid_value():
assert 2 == _get_setting(int, "FOO", 1)


def test_get_setting_returns_default_value_for_missing_setting():
assert 1 == _get_setting(int, "FOO", 1)


@override_settings(FOO="bar")
def test_get_setting_raises_for_invalid_type():
with pytest.raises(ValueError):
_get_setting(int, "FOO", 1)

0 comments on commit c970030

Please sign in to comment.