Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from pipeline.storage import PipelineMixin, PipelineStorage
from whitenoise.storage import CompressedManifestStaticFilesStorage, HelpfulExceptionMixin, CompressedStaticFilesMixin
class CompressedManifestPipelineStorage(PipelineMixin, CompressedManifestStaticFilesStorage):
pass
class CompressedPipelineStorage(HelpfulExceptionMixin, CompressedStaticFilesMixin, PipelineStorage):
pass
"""
upstream_converter = super(PatchedManifestStaticFilesStorage, self).url_converter(*args, **kwargs)
def converter(matchobj):
try:
upstream_converter(matchobj)
except ValueError:
# e.g. a static file 'static/media/logo.6a30f15f.svg' could not be found
# because the upstream converter stripped 'static/' from the path
matched, url = matchobj.groups()
return matched
return converter
class SPAStaticFilesStorage(
HelpfulExceptionMixin, CompressedStaticFilesMixin,
PatchedManifestStaticFilesStorage):
pass
compressor = self.create_compressor(extensions=extensions, quiet=True)
for name, hashed_name, processed in files:
yield name, hashed_name, processed
if isinstance(processed, Exception):
continue
unique_names = set(filter(None, [name, hashed_name]))
for name in unique_names:
if compressor.should_compress(name):
path = self.path(name)
prefix_len = len(path) - len(name)
for compressed_path in compressor.compress(path):
compressed_name = compressed_path[prefix_len:]
yield name, compressed_name, True
class CompressedStaticFilesStorage(CompressedStaticFilesMixin, StaticFilesStorage):
pass
class HelpfulExceptionMixin(object):
"""
If a CSS file contains references to images, fonts etc that can't be found
then Django's `post_process` blows up with a not particularly helpful
ValueError that leads people to think WhiteNoise is broken.
Here we attempt to intercept such errors and reformat them to be more
helpful in revealing the source of the problem.
"""
ERROR_MSG_RE = re.compile("^The file '(.+)' could not be found")
ERROR_MSG = textwrap.dedent(
def post_process(self, *args, **kwargs):
super_post_process = getattr(
super(CompressedStaticFilesMixin, self),
"post_process",
self.fallback_post_process,
)
files = super_post_process(*args, **kwargs)
if not kwargs.get("dry_run"):
files = self.post_process_with_compression(files)
return files