Commit f4c2b8e0 authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #20189 -- Allowed customizing staticfiles ignored_patterns list

Thanks Tim Graham for the review.
parent 6f5fcfc6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5,3 +5,4 @@ from django.utils.translation import ugettext_lazy as _
class StaticFilesConfig(AppConfig):
    name = 'django.contrib.staticfiles'
    verbose_name = _("Static Files")
    ignore_patterns = ['CVS', '.*', '*~']
+3 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import os
from collections import OrderedDict

from django.apps import apps
from django.contrib.staticfiles.finders import get_finders
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.files.storage import FileSystemStorage
@@ -74,7 +75,7 @@ class Command(BaseCommand):
        parser.add_argument(
            '--no-default-ignore', action='store_false',
            dest='use_default_ignore_patterns', default=True,
            help="Don't ignore the common private glob-style patterns 'CVS', '.*' and '*~'.",
            help="Don't ignore the common private glob-style patterns (defaults to 'CVS', '.*' and '*~').",
        )

    def set_options(self, **options):
@@ -88,7 +89,7 @@ class Command(BaseCommand):
        self.dry_run = options['dry_run']
        ignore_patterns = options['ignore_patterns']
        if options['use_default_ignore_patterns']:
            ignore_patterns += ['CVS', '.*', '*~']
            ignore_patterns += apps.get_app_config('staticfiles').ignore_patterns
        self.ignore_patterns = list(set(ignore_patterns))
        self.post_process = options['post_process']

+18 −0
Original line number Diff line number Diff line
@@ -128,6 +128,24 @@ For a full list of options, refer to the commands own help by running::

   $ python manage.py collectstatic --help

Customizing the ignored pattern list
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 1.10

The default ignored pattern list, ``['CVS', '.*', '*~']``, can be customized in
a more persistent way than providing the ``--ignore`` command option at each
``collectstatic`` invocation. Provide a custom :class:`~django.apps.AppConfig`
class, override the ``ignore_patterns`` attribute of this class and specify
that class path inside your :setting:`INSTALLED_APPS` setting:

.. code-block:: python

    from django.contrib.staticfiles.apps import StaticFilesConfig

    class MyStaticFilesConfig(StaticFilesConfig):
        ignore_patterns = [...]  # your custom ignore list

``findstatic``
--------------

+5 −0
Original line number Diff line number Diff line
from django.contrib.staticfiles.apps import StaticFilesConfig


class IgnorePatternsAppConfig(StaticFilesConfig):
    ignore_patterns = ['*.css']
+14 −0
Original line number Diff line number Diff line
@@ -195,6 +195,20 @@ class TestCollectionExcludeNoDefaultIgnore(TestDefaults, CollectionTestCase):
        self.assertFileContains('test/CVS', 'should be ignored')


@override_settings(INSTALLED_APPS=[
    'staticfiles_tests.apps.staticfiles_config.IgnorePatternsAppConfig',
    'staticfiles_tests.apps.test',
])
class TestCollectionCustomIgnorePatterns(CollectionTestCase):
    def test_custom_ignore_patterns(self):
        """
        A custom ignore_patterns list, ['*.css'] in this case, can be specified
        in an AppConfig definition.
        """
        self.assertFileNotFound('test/nonascii.css')
        self.assertFileContains('test/.hidden', 'should be ignored')


class TestCollectionDryRun(TestNoFilesCreated, CollectionTestCase):
    """
    Test ``--dry-run`` option for ``collectstatic`` management command.