Ticket #19670: 19670.diff

File 19670.diff, 4.7 KB (added by DrMeers, 21 months ago)

WIP patch

  • django/contrib/staticfiles/storage.py

    diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py
    index d085cf7..98e1cf4 100644
    a b class CachedFilesMixin(object): 
    248248                # ..to apply each replacement pattern to the content
    249249                if name in adjustable_paths:
    250250                    content = original_file.read().decode(settings.FILE_CHARSET)
    251                     for patterns in self._patterns.values():
    252                         for pattern, template in patterns:
    253                             converter = self.url_converter(name, template)
    254                             try:
    255                                 content = pattern.sub(converter, content)
    256                             except ValueError as exc:
    257                                 yield name, None, exc
     251                    for extension, patterns in self._patterns.iteritems():
     252                        if matches_patterns(path, (extension,)):
     253                            for pattern, template in patterns:
     254                                converter = self.url_converter(name, template)
     255                                try:
     256                                    content = pattern.sub(converter, content)
     257                                except ValueError as exc:
     258                                    yield name, None, exc
    258259                    if hashed_file_exists:
    259260                        self.delete(hashed_name)
    260261                    # then save the processed result
  • tests/staticfiles_tests/storage.py

    diff --git a/tests/staticfiles_tests/storage.py b/tests/staticfiles_tests/storage.py
    index 4d49e6f..dfee5f3 100644
    a b class SimpleCachedStaticFilesStorage(CachedStaticFilesStorage): 
    2424
    2525    def file_hash(self, name, content=None):
    2626        return 'deploy12345'
     27
     28
     29class MultiCachedStaticFilesStorage(CachedStaticFilesStorage):
     30    patterns = tuple(CachedStaticFilesStorage.patterns) + (
     31        (
     32            "*.js", (
     33                (r"""(url\(['"]{0,1}\s*(.*?)["']{0,1}\))""", 'JS_URL("%s")'),
     34            ),
     35        ),
     36    )
  • tests/staticfiles_tests/tests.py

    diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py
    index 912dcff..c11f983 100644
    a b class TestCollectionFilesOverride(CollectionTestCase): 
    349349        self.assertFileContains('file2.txt', 'duplicate of file2.txt')
    350350
    351351
     352
    352353@override_settings(
    353354    STATICFILES_STORAGE='staticfiles_tests.storage.DummyStorage',
    354355)
    class TestCollectionCachedStorage(BaseCollectionTestCase, 
    566567        self.assertEqual("Post-processing 'faulty.css' failed!\n\n", err.getvalue())
    567568
    568569
     570@override_settings(
     571    **dict(
     572        TEST_SETTINGS,
     573        STATICFILES_STORAGE=(
     574            'staticfiles_tests.storage.MultiCachedStaticFilesStorage'
     575        ),
     576        DEBUG=False, # see note above
     577    )
     578)
     579class TestMultiCachedStorage(
     580       BaseCollectionTestCase,
     581       BaseStaticFilesTestCase,
     582       TestCase,
     583):
     584    def cached_file_path(self, path):
     585        fullpath = self.render_template(self.static_template_snippet(path))
     586        return fullpath.replace(settings.STATIC_URL, '')
     587
     588    def test_multi_extension_patterns(self):
     589        """ #19670 """
     590        storage.staticfiles_storage.cache.clear() # avoid interference
     591        import pdb; pdb.set_trace()
     592
     593        self.assertEqual(
     594            settings.STATICFILES_STORAGE,
     595            'staticfiles_tests.storage.MultiCachedStaticFilesStorage'
     596        )
     597        self.assertEqual(
     598            storage.staticfiles_storage._wrapped.__class__.__name__,
     599            'MultiCachedStaticFilesStorage'
     600        )
     601
     602        # confirm CSS files haven't been touched by JS patterns
     603        relpath = self.cached_file_path("cached/import.css")
     604        self.assertEqual(relpath, "cached/import.2b1d40b0bbd4.css")
     605        with storage.staticfiles_storage.open(relpath) as relfile:
     606            self.assertIn(
     607                b"""import url("styles.93b1147e8552.css")""",
     608                relfile.read()
     609            )
     610
     611        # confirm JS patterns have been applied to JS files
     612        relpath = self.cached_file_path("cached/test.js")
     613        self.assertEqual(relpath, "cached/test.d45dc1068bc8.js")
     614        with storage.staticfiles_storage.open(relpath) as relfile:
     615            self.assertIn(
     616                b"""JS_URL("import.2b1d40b0bbd4.css")""",
     617                relfile.read()
     618            )
     619
     620
    569621# we set DEBUG to False here since the template tag wouldn't work otherwise
    570622@override_settings(**dict(TEST_SETTINGS,
    571623    STATICFILES_STORAGE='staticfiles_tests.storage.SimpleCachedStaticFilesStorage',
Back to Top