diff --git a/collectfast/management/commands/collectstatic.py b/collectfast/management/commands/collectstatic.py index 537a469..fa58b9d 100644 --- a/collectfast/management/commands/collectstatic.py +++ b/collectfast/management/commands/collectstatic.py @@ -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.""" @@ -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 @@ -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 diff --git a/collectfast/settings.py b/collectfast/settings.py index 8c6f267..95a39d0 100644 --- a/collectfast/settings.py +++ b/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", diff --git a/collectfast/tests/test_settings.py b/collectfast/tests/test_settings.py new file mode 100644 index 0000000..9fc858f --- /dev/null +++ b/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)