Skip to content

Commit e75309b

Browse files
committed
Another pass over the staticfiles panel.
- adds some docstrings - adds some basic tests - adds the staticfiles panel to the default list of panels - fixed a pluralization bug in the template - refactored some things into own methods for easier testing
1 parent 2359b2e commit e75309b

File tree

8 files changed

+88
-25
lines changed

8 files changed

+88
-25
lines changed

debug_toolbar/panels/staticfiles.py

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121

2222
class StaticFile(object):
23-
23+
"""
24+
Representing the different properties of a static file.
25+
"""
2426
def __init__(self, path):
2527
self.path = path
2628

@@ -47,6 +49,11 @@ def collect(self, path, thread=None):
4749

4850

4951
class DebugConfiguredStorage(LazyObject):
52+
"""
53+
A staticfiles storage class to be used for collecting which paths
54+
are resolved by using the {% static %} template tag (which uses the
55+
`url` method).
56+
"""
5057
def _setup(self):
5158

5259
configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE)
@@ -81,7 +88,6 @@ def title(self):
8188
def __init__(self, *args, **kwargs):
8289
super(StaticFilesPanel, self).__init__(*args, **kwargs)
8390
self.num_found = 0
84-
self.ignore_patterns = []
8591
self._paths = {}
8692

8793
@property
@@ -100,43 +106,63 @@ def num_used(self):
100106
@property
101107
def nav_subtitle(self):
102108
num_used = self.num_used
103-
return ungettext("%(num_used)s file used", "%(num_used)s files used",
109+
return ungettext("%(num_used)s file used",
110+
"%(num_used)s files used",
104111
num_used) % {'num_used': num_used}
105112

106113
def process_request(self, request):
107114
collector.clear_collection()
108115

109116
def process_response(self, request, response):
110-
staticfiles_finders = SortedDict()
117+
used_paths = collector.get_collection()
118+
self._paths[threading.currentThread()] = used_paths
119+
120+
self.record_stats({
121+
'num_found': self.num_found,
122+
'num_used': self.num_used,
123+
'staticfiles': used_paths,
124+
'staticfiles_apps': self.get_staticfiles_apps(),
125+
'staticfiles_dirs': self.get_staticfiles_dirs(),
126+
'staticfiles_finders': self.get_staticfiles_finders(),
127+
})
128+
129+
def get_staticfiles_finders(self):
130+
"""
131+
Returns a sorted mapping between the finder path and the list
132+
of relative and file system paths which that finder was able
133+
to find.
134+
"""
135+
finders_mapping = SortedDict()
111136
for finder in finders.get_finders():
112-
for path, finder_storage in finder.list(self.ignore_patterns):
137+
for path, finder_storage in finder.list([]):
113138
if getattr(finder_storage, 'prefix', None):
114139
prefixed_path = join(finder_storage.prefix, path)
115140
else:
116141
prefixed_path = path
117-
finder_path = '.'.join([finder.__class__.__module__,
118-
finder.__class__.__name__])
142+
finder_cls = finder.__class__
143+
finder_path = '.'.join([finder_cls.__module__,
144+
finder_cls.__name__])
119145
real_path = finder_storage.path(path)
120146
payload = (prefixed_path, real_path)
121-
staticfiles_finders.setdefault(finder_path, []).append(payload)
147+
finders_mapping.setdefault(finder_path, []).append(payload)
122148
self.num_found += 1
149+
return finders_mapping
123150

151+
def get_staticfiles_dirs(self):
152+
"""
153+
Returns a list of paths to inspect for additional static files
154+
"""
124155
dirs = getattr(settings, 'STATICFILES_DIRS', ())
156+
return [normpath(d) for d in dirs]
125157

126-
used_paths = collector.get_collection()
127-
self._paths[threading.currentThread()] = used_paths
128-
129-
self.record_stats({
130-
'num_found': self.num_found,
131-
'num_used': self.num_used,
132-
'staticfiles': used_paths,
133-
'staticfiles_apps': self.get_static_apps(),
134-
'staticfiles_dirs': [normpath(d) for d in dirs],
135-
'staticfiles_finders': staticfiles_finders,
136-
})
137-
138-
def get_static_apps(self):
158+
def get_staticfiles_apps(self):
159+
"""
160+
Returns a list of app paths that have a static directory
161+
"""
162+
apps = []
139163
for finder in finders.get_finders():
140164
if isinstance(finder, finders.AppDirectoriesFinder):
141-
return finder.apps
142-
return []
165+
for app in finder.apps:
166+
if app not in apps:
167+
apps.append(app)
168+
return apps

debug_toolbar/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
'debug_toolbar.panels.request.RequestPanel',
7777
'debug_toolbar.panels.sql.SQLPanel',
7878
'debug_toolbar.panels.templates.TemplatesPanel',
79+
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
7980
'debug_toolbar.panels.cache.CachePanel',
8081
'debug_toolbar.panels.signals.SignalsPanel',
8182
'debug_toolbar.panels.logging.LoggingPanel',

debug_toolbar/templates/debug_toolbar/panels/staticfiles.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h4>{% blocktrans count staticfiles|length as staticfiles_count %}Static file{%
3737

3838

3939
{% for finder, payload in staticfiles_finders.items %}
40-
<h4>{{ finder }} ({{ payload|length }} files)</h4>
40+
<h4>{{ finder }} ({% blocktrans count payload|length as payload_count %}{{ payload_count }} file{% plural %}{{ payload_count }} files{% endblocktrans %})</h4>
4141
<table>
4242
<thead>
4343
<tr>

example/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@
8484
'debug_toolbar.panels.request.RequestPanel',
8585
'debug_toolbar.panels.sql.SQLPanel',
8686
'debug_toolbar.panels.templates.TemplatesPanel',
87+
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
8788
'debug_toolbar.panels.cache.CachePanel',
8889
'debug_toolbar.panels.signals.SignalsPanel',
8990
'debug_toolbar.panels.logging.LoggingPanel',
9091
'debug_toolbar.panels.redirects.RedirectsPanel',
9192
'debug_toolbar.panels.profiling.ProfilingPanel',
92-
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
9393
]
9494

9595
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'example', 'static')]

tests/additional_static/base.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: green;
3+
}

tests/panels/test_staticfiles.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# coding: utf-8
2+
3+
from __future__ import absolute_import, unicode_literals
4+
5+
from django.conf import settings
6+
from django.contrib.staticfiles.templatetags import staticfiles
7+
from django.template import Context, RequestContext, Template
8+
9+
from ..base import BaseTestCase
10+
11+
12+
class StaticFilesPanelTestCase(BaseTestCase):
13+
14+
def setUp(self):
15+
super(StaticFilesPanelTestCase, self).setUp()
16+
self.panel = self.toolbar.get_panel_by_id('StaticFilesPanel')
17+
18+
def test_default_case(self):
19+
self.panel.process_request(self.request)
20+
self.panel.process_response(self.request, self.response)
21+
self.assertIn('django.contrib.staticfiles.finders.'
22+
'AppDirectoriesFinder (87 files)', self.panel.content)
23+
self.assertIn('django.contrib.staticfiles.finders.'
24+
'FileSystemFinder (1 file)', self.panel.content)
25+
self.assertEqual(self.panel.num_used, 0)
26+
self.assertEqual(self.panel.num_found, 88)
27+
self.assertEqual(self.panel.get_staticfiles_apps(),
28+
['django.contrib.admin', 'debug_toolbar'])
29+
self.assertEqual(self.panel.get_staticfiles_dirs(),
30+
settings.STATICFILES_DIRS)

tests/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
STATIC_URL = '/static/'
4242

43+
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'tests', 'additional_static')]
44+
4345

4446
# Cache and database
4547

tests/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .panels.test_redirects import * # noqa
99
from .panels.test_request import * # noqa
1010
from .panels.test_sql import * # noqa
11+
from .panels.test_staticfiles import * # noqa
1112
from .panels.test_template import * # noqa
1213
from .test_integration import * # noqa
1314
from .test_utils import * # noqa

0 commit comments

Comments
 (0)