Skip to content

Commit

Permalink
Fixed #19715 -- Simplified findstatic output when verbosity set to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt authored and claudep committed Feb 1, 2013
1 parent 56e5531 commit 393c268
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -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/>
Expand Down
13 changes: 8 additions & 5 deletions django/contrib/staticfiles/management/commands/findstatic.py
Expand Up @@ -3,7 +3,7 @@
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

Expand All @@ -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)
18 changes: 14 additions & 4 deletions docs/ref/contrib/staticfiles.txt
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions tests/regressiontests/staticfiles_tests/tests.py
Expand Up @@ -195,21 +195,33 @@ def _get_file(self, filepath):
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):
"""
Expand Down

0 comments on commit 393c268

Please sign in to comment.