Przeglądaj źródła

Fixed #19715 -- Simplified findstatic output when verbosity set to 0

Matt Robenolt 12 lat temu
rodzic
commit
393c268e72

+ 1 - 0
AUTHORS

@@ -464,6 +464,7 @@ answer newbie questions, and generally made Django that much better:
     Mike Richardson
     Matt Riggott
     Alex Robbins <alexander.j.robbins@gmail.com>
+    Matt Robenolt <m@robenolt.com>
     Henrique Romano <onaiort@gmail.com>
     Armin Ronacher
     Daniel Roseman <http://roseman.org.uk/>

+ 8 - 5
django/contrib/staticfiles/management/commands/findstatic.py

@@ -3,7 +3,7 @@ from __future__ import unicode_literals
 import os
 from optparse import make_option
 from django.core.management.base import LabelCommand
-from django.utils.encoding import smart_text
+from django.utils.encoding import force_text
 
 from django.contrib.staticfiles import finders
 
@@ -19,13 +19,16 @@ class Command(LabelCommand):
     def handle_label(self, path, **options):
         verbosity = int(options.get('verbosity', 1))
         result = finders.find(path, all=options['all'])
-        path = smart_text(path)
+        path = force_text(path)
         if result:
             if not isinstance(result, (list, tuple)):
                 result = [result]
-            output = '\n  '.join(
-                (smart_text(os.path.realpath(path)) for path in result))
-            self.stdout.write("Found '%s' here:\n  %s" % (path, output))
+            result = (force_text(os.path.realpath(path)) for path in result)
+            if verbosity >= 1:
+                output = '\n  '.join(result)
+                return "Found '%s' here:\n  %s" % (path, output)
+            else:
+                return '\n'.join(result)
         else:
             if verbosity >= 1:
                 self.stderr.write("No matching file found for '%s'." % path)

+ 14 - 4
docs/ref/contrib/staticfiles.txt

@@ -112,19 +112,29 @@ Searches for one or more relative paths with the enabled finders.
 For example::
 
    $ python manage.py findstatic css/base.css admin/js/core.js
-   /home/special.polls.com/core/static/css/base.css
-   /home/polls.com/core/static/css/base.css
-   /home/polls.com/src/django/contrib/admin/media/js/core.js
+   Found 'css/base.css' here:
+     /home/special.polls.com/core/static/css/base.css
+     /home/polls.com/core/static/css/base.css
+   Found 'admin/js/core.js' here:
+     /home/polls.com/src/django/contrib/admin/media/js/core.js
 
 By default, all matching locations are found. To only return the first match
 for each relative path, use the ``--first`` option::
 
    $ python manage.py findstatic css/base.css --first
-   /home/special.polls.com/core/static/css/base.css
+   Found 'css/base.css' here:
+     /home/special.polls.com/core/static/css/base.css
 
 This is a debugging aid; it'll show you exactly which static file will be
 collected for a given path.
 
+By setting the :djadminopt:`--verbosity` flag to 0, you can suppress the extra
+output and just get the path names::
+
+   $ python manage.py findstatic css/base.css --verbosity 0
+   /home/special.polls.com/core/static/css/base.css
+   /home/polls.com/core/static/css/base.css
+
 .. _staticfiles-runserver:
 
 runserver

+ 15 - 3
tests/regressiontests/staticfiles_tests/tests.py

@@ -195,21 +195,33 @@ class TestFindStatic(CollectionTestCase, TestDefaults):
         call_command('findstatic', filepath, all=False, verbosity=0, stdout=out)
         out.seek(0)
         lines = [l.strip() for l in out.readlines()]
-        with codecs.open(force_text(lines[1].strip()), "r", "utf-8") as f:
+        with codecs.open(force_text(lines[0].strip()), "r", "utf-8") as f:
             return f.read()
 
     def test_all_files(self):
         """
-        Test that findstatic returns all candidate files if run without --first.
+        Test that findstatic returns all candidate files if run without --first and -v1.
         """
         out = six.StringIO()
-        call_command('findstatic', 'test/file.txt', verbosity=0, stdout=out)
+        call_command('findstatic', 'test/file.txt', verbosity=1, stdout=out)
         out.seek(0)
         lines = [l.strip() for l in out.readlines()]
         self.assertEqual(len(lines), 3)  # three because there is also the "Found <file> here" line
         self.assertIn('project', force_text(lines[1]))
         self.assertIn('apps', force_text(lines[2]))
 
+    def test_all_files_less_verbose(self):
+        """
+        Test that findstatic returns all candidate files if run without --first and -v0.
+        """
+        out = six.StringIO()
+        call_command('findstatic', 'test/file.txt', verbosity=0, stdout=out)
+        out.seek(0)
+        lines = [l.strip() for l in out.readlines()]
+        self.assertEqual(len(lines), 2)
+        self.assertIn('project', force_text(lines[0]))
+        self.assertIn('apps', force_text(lines[1]))
+
 
 class TestCollection(CollectionTestCase, TestDefaults):
     """