From f4c2b8e04a297f627a8e722d78eda6cbf5cc8a6e Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 15 Apr 2016 21:05:06 +0200 Subject: [PATCH] Fixed #20189 -- Allowed customizing staticfiles ignored_patterns list Thanks Tim Graham for the review. --- django/contrib/staticfiles/apps.py | 1 + .../management/commands/collectstatic.py | 5 +++-- docs/ref/contrib/staticfiles.txt | 18 ++++++++++++++++++ .../apps/staticfiles_config.py | 5 +++++ tests/staticfiles_tests/test_management.py | 14 ++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/staticfiles_tests/apps/staticfiles_config.py diff --git a/django/contrib/staticfiles/apps.py b/django/contrib/staticfiles/apps.py index f406e16181..0911c0113d 100644 --- a/django/contrib/staticfiles/apps.py +++ b/django/contrib/staticfiles/apps.py @@ -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', '.*', '*~'] diff --git a/django/contrib/staticfiles/management/commands/collectstatic.py b/django/contrib/staticfiles/management/commands/collectstatic.py index 1e4a577b0b..db29ff73cc 100644 --- a/django/contrib/staticfiles/management/commands/collectstatic.py +++ b/django/contrib/staticfiles/management/commands/collectstatic.py @@ -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'] diff --git a/docs/ref/contrib/staticfiles.txt b/docs/ref/contrib/staticfiles.txt index e09bdaad49..3e70d82f7e 100644 --- a/docs/ref/contrib/staticfiles.txt +++ b/docs/ref/contrib/staticfiles.txt @@ -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`` -------------- diff --git a/tests/staticfiles_tests/apps/staticfiles_config.py b/tests/staticfiles_tests/apps/staticfiles_config.py new file mode 100644 index 0000000000..e48a0c8d99 --- /dev/null +++ b/tests/staticfiles_tests/apps/staticfiles_config.py @@ -0,0 +1,5 @@ +from django.contrib.staticfiles.apps import StaticFilesConfig + + +class IgnorePatternsAppConfig(StaticFilesConfig): + ignore_patterns = ['*.css'] diff --git a/tests/staticfiles_tests/test_management.py b/tests/staticfiles_tests/test_management.py index f0c6384df7..d2ae7e2ed5 100644 --- a/tests/staticfiles_tests/test_management.py +++ b/tests/staticfiles_tests/test_management.py @@ -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.