Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Added a Custom Static Storage #172

Merged
Prev Previous commit
Next Next commit
[openwisp_utils/storage] Added a Custom Static storage class #166
Ported changes from  storage.py in ansible-openwisp2 to storage.py in openwisp_utils/.
Created test_storage in tests/test_project/tests , The test creates a temporary folder and 2 text files in it and then runs collectstatic in that folder and checks whether the file hashed is correct or not and later deletes that folder.
  • Loading branch information
Arsh0023 committed Mar 22, 2021
commit 02ff52e0747b2177548a2507e81ab46aa8fb61a3
33 changes: 33 additions & 0 deletions openwisp_utils/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fnmatch

from compress_staticfiles.storage import (
CompressStaticFilesStorage as BaseCompressStaticFilesStorage,
)
from django.conf import settings


class FileHashedNameMixin:
default_excluded_patterns = ['*.png']
Arsh0023 marked this conversation as resolved.
Show resolved Hide resolved
excluded_patterns = default_excluded_patterns + getattr(
settings, "OPENWISP_STATICFILES_VERSIONED_EXCLUDE", []
)

def hashed_name(self, name, content=None, filename=None):
if not any(
fnmatch.fnmatch(name, pattern) for pattern in self.excluded_patterns
):
return super().hashed_name(name, content, filename)
return name


class CompressStaticFilesStorage(
FileHashedNameMixin, BaseCompressStaticFilesStorage,
):
"""
A static files storage backend for compression that inherits from
django-compress-staticfiles's CompressStaticFilesStorage class;
also adds support for excluding file types using
"OPENWISP_STATICFILES_VERSIONED_EXCLUDE" setting.
"""

pass
44 changes: 44 additions & 0 deletions tests/test_project/tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import shutil
import tempfile

from django.conf import settings
from django.contrib.staticfiles import storage
from django.contrib.staticfiles.management.commands.collectstatic import (
Command as CollectstaticCommand,
)
from django.core.management import call_command
from django.test import TestCase, override_settings


@override_settings(
STATICFILES_STORAGE='openwisp_utils.storage.CompressStaticFilesStorage',
STATIC_ROOT=os.path.join(settings.BASE_DIR, 'test_static_root'),
STATICFILES_FINDERS=['django.contrib.staticfiles.finders.FileSystemFinder',],
OPENWISP_STATICFILES_VERSIONED_EXCLUDE=['*skip_this.txt'],
)
class TestCompressStaticFilesStorage(TestCase):
def setUp(self):
Arsh0023 marked this conversation as resolved.
Show resolved Hide resolved
temp_dir = tempfile.mkdtemp()
os.makedirs(os.path.join(temp_dir, 'test'))

self.file1 = os.path.join(temp_dir, 'test', 'skip_this.txt')
with open(self.file1, 'w') as f:
f.write('this will not be hashed')

self.file2 = os.path.join(temp_dir, 'test', 'this.txt')
with open(self.file2, 'w') as f:
f.write('this will be hashed')

self.patched_settings = self.settings(STATICFILES_DIRS=[temp_dir])
self.patched_settings.enable()
self.addCleanup(shutil.rmtree, temp_dir)

def tearDown(self):
Arsh0023 marked this conversation as resolved.
Show resolved Hide resolved
shutil.rmtree(settings.STATIC_ROOT)
Arsh0023 marked this conversation as resolved.
Show resolved Hide resolved

def test_hashed_name(self):
call_command('collectstatic')
hashed_files = storage.staticfiles_storage.hashed_files
self.assertEqual(hashed_files['test/skip_this.txt'], 'test/skip_this.txt')
self.assertNotEqual(hashed_files['test/this.txt'], 'test/this.txt')