diff options
| author | Jannis Leidel | 2013-12-09 17:42:04 +0100 |
|---|---|---|
| committer | Jannis Leidel | 2013-12-09 17:42:04 +0100 |
| commit | e75309b712537f3ee53005bf6330057c9640be21 (patch) | |
| tree | e83b9943c1cf4ee53754922629417468bfcaa153 | |
| parent | 2359b2e61d4f6018049042b3213a91af2b4db9fc (diff) | |
| download | django-debug-toolbar-e75309b712537f3ee53005bf6330057c9640be21.tar.bz2 | |
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
| -rw-r--r-- | debug_toolbar/panels/staticfiles.py | 72 | ||||
| -rw-r--r-- | debug_toolbar/settings.py | 1 | ||||
| -rw-r--r-- | debug_toolbar/templates/debug_toolbar/panels/staticfiles.html | 2 | ||||
| -rw-r--r-- | example/settings.py | 2 | ||||
| -rw-r--r-- | tests/additional_static/base.css | 3 | ||||
| -rw-r--r-- | tests/panels/test_staticfiles.py | 30 | ||||
| -rw-r--r-- | tests/settings.py | 2 | ||||
| -rw-r--r-- | tests/tests.py | 1 |
8 files changed, 88 insertions, 25 deletions
diff --git a/debug_toolbar/panels/staticfiles.py b/debug_toolbar/panels/staticfiles.py index 791cbbc..4ccac71 100644 --- a/debug_toolbar/panels/staticfiles.py +++ b/debug_toolbar/panels/staticfiles.py @@ -20,7 +20,9 @@ from debug_toolbar.utils import ThreadCollector class StaticFile(object): - + """ + Representing the different properties of a static file. + """ def __init__(self, path): self.path = path @@ -47,6 +49,11 @@ collector = FileCollector() class DebugConfiguredStorage(LazyObject): + """ + A staticfiles storage class to be used for collecting which paths + are resolved by using the {% static %} template tag (which uses the + `url` method). + """ def _setup(self): configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE) @@ -81,7 +88,6 @@ class StaticFilesPanel(panels.Panel): def __init__(self, *args, **kwargs): super(StaticFilesPanel, self).__init__(*args, **kwargs) self.num_found = 0 - self.ignore_patterns = [] self._paths = {} @property @@ -100,43 +106,63 @@ class StaticFilesPanel(panels.Panel): @property def nav_subtitle(self): num_used = self.num_used - return ungettext("%(num_used)s file used", "%(num_used)s files used", + return ungettext("%(num_used)s file used", + "%(num_used)s files used", num_used) % {'num_used': num_used} def process_request(self, request): collector.clear_collection() def process_response(self, request, response): - staticfiles_finders = SortedDict() + used_paths = collector.get_collection() + self._paths[threading.currentThread()] = used_paths + + self.record_stats({ + 'num_found': self.num_found, + 'num_used': self.num_used, + 'staticfiles': used_paths, + 'staticfiles_apps': self.get_staticfiles_apps(), + 'staticfiles_dirs': self.get_staticfiles_dirs(), + 'staticfiles_finders': self.get_staticfiles_finders(), + }) + + def get_staticfiles_finders(self): + """ + Returns a sorted mapping between the finder path and the list + of relative and file system paths which that finder was able + to find. + """ + finders_mapping = SortedDict() for finder in finders.get_finders(): - for path, finder_storage in finder.list(self.ignore_patterns): + for path, finder_storage in finder.list([]): if getattr(finder_storage, 'prefix', None): prefixed_path = join(finder_storage.prefix, path) else: prefixed_path = path - finder_path = '.'.join([finder.__class__.__module__, - finder.__class__.__name__]) + finder_cls = finder.__class__ + finder_path = '.'.join([finder_cls.__module__, + finder_cls.__name__]) real_path = finder_storage.path(path) payload = (prefixed_path, real_path) - staticfiles_finders.setdefault(finder_path, []).append(payload) + finders_mapping.setdefault(finder_path, []).append(payload) self.num_found += 1 + return finders_mapping + def get_staticfiles_dirs(self): + """ + Returns a list of paths to inspect for additional static files + """ dirs = getattr(settings, 'STATICFILES_DIRS', ()) + return [normpath(d) for d in dirs] - used_paths = collector.get_collection() - self._paths[threading.currentThread()] = used_paths - - self.record_stats({ - 'num_found': self.num_found, - 'num_used': self.num_used, - 'staticfiles': used_paths, - 'staticfiles_apps': self.get_static_apps(), - 'staticfiles_dirs': [normpath(d) for d in dirs], - 'staticfiles_finders': staticfiles_finders, - }) - - def get_static_apps(self): + def get_staticfiles_apps(self): + """ + Returns a list of app paths that have a static directory + """ + apps = [] for finder in finders.get_finders(): if isinstance(finder, finders.AppDirectoriesFinder): - return finder.apps - return [] + for app in finder.apps: + if app not in apps: + apps.append(app) + return apps diff --git a/debug_toolbar/settings.py b/debug_toolbar/settings.py index bde8a1c..5ed0005 100644 --- a/debug_toolbar/settings.py +++ b/debug_toolbar/settings.py @@ -76,6 +76,7 @@ PANELS_DEFAULTS = [ 'debug_toolbar.panels.request.RequestPanel', 'debug_toolbar.panels.sql.SQLPanel', 'debug_toolbar.panels.templates.TemplatesPanel', + 'debug_toolbar.panels.staticfiles.StaticFilesPanel', 'debug_toolbar.panels.cache.CachePanel', 'debug_toolbar.panels.signals.SignalsPanel', 'debug_toolbar.panels.logging.LoggingPanel', diff --git a/debug_toolbar/templates/debug_toolbar/panels/staticfiles.html b/debug_toolbar/templates/debug_toolbar/panels/staticfiles.html index e405a25..eb413b5 100644 --- a/debug_toolbar/templates/debug_toolbar/panels/staticfiles.html +++ b/debug_toolbar/templates/debug_toolbar/panels/staticfiles.html @@ -37,7 +37,7 @@ {% for finder, payload in staticfiles_finders.items %} -<h4>{{ finder }} ({{ payload|length }} files)</h4> +<h4>{{ finder }} ({% blocktrans count payload|length as payload_count %}{{ payload_count }} file{% plural %}{{ payload_count }} files{% endblocktrans %})</h4> <table> <thead> <tr> diff --git a/example/settings.py b/example/settings.py index ca78f70..867b26f 100644 --- a/example/settings.py +++ b/example/settings.py @@ -84,12 +84,12 @@ DEBUG_TOOLBAR_PANELS = [ 'debug_toolbar.panels.request.RequestPanel', 'debug_toolbar.panels.sql.SQLPanel', 'debug_toolbar.panels.templates.TemplatesPanel', + 'debug_toolbar.panels.staticfiles.StaticFilesPanel', 'debug_toolbar.panels.cache.CachePanel', 'debug_toolbar.panels.signals.SignalsPanel', 'debug_toolbar.panels.logging.LoggingPanel', 'debug_toolbar.panels.redirects.RedirectsPanel', 'debug_toolbar.panels.profiling.ProfilingPanel', - 'debug_toolbar.panels.staticfiles.StaticFilesPanel', ] STATICFILES_DIRS = [os.path.join(BASE_DIR, 'example', 'static')] diff --git a/tests/additional_static/base.css b/tests/additional_static/base.css new file mode 100644 index 0000000..8d7d127 --- /dev/null +++ b/tests/additional_static/base.css @@ -0,0 +1,3 @@ +body { + color: green; +}
\ No newline at end of file diff --git a/tests/panels/test_staticfiles.py b/tests/panels/test_staticfiles.py new file mode 100644 index 0000000..7f68205 --- /dev/null +++ b/tests/panels/test_staticfiles.py @@ -0,0 +1,30 @@ +# coding: utf-8 + +from __future__ import absolute_import, unicode_literals + +from django.conf import settings +from django.contrib.staticfiles.templatetags import staticfiles +from django.template import Context, RequestContext, Template + +from ..base import BaseTestCase + + +class StaticFilesPanelTestCase(BaseTestCase): + + def setUp(self): + super(StaticFilesPanelTestCase, self).setUp() + self.panel = self.toolbar.get_panel_by_id('StaticFilesPanel') + + def test_default_case(self): + self.panel.process_request(self.request) + self.panel.process_response(self.request, self.response) + self.assertIn('django.contrib.staticfiles.finders.' + 'AppDirectoriesFinder (87 files)', self.panel.content) + self.assertIn('django.contrib.staticfiles.finders.' + 'FileSystemFinder (1 file)', self.panel.content) + self.assertEqual(self.panel.num_used, 0) + self.assertEqual(self.panel.num_found, 88) + self.assertEqual(self.panel.get_staticfiles_apps(), + ['django.contrib.admin', 'debug_toolbar']) + self.assertEqual(self.panel.get_staticfiles_dirs(), + settings.STATICFILES_DIRS) diff --git a/tests/settings.py b/tests/settings.py index 24e0cd4..9ff9fb6 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -40,6 +40,8 @@ ROOT_URLCONF = 'tests.urls' STATIC_URL = '/static/' +STATICFILES_DIRS = [os.path.join(BASE_DIR, 'tests', 'additional_static')] + # Cache and database diff --git a/tests/tests.py b/tests/tests.py index 1818fe7..ca77f5b 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -8,6 +8,7 @@ if django.VERSION[:2] < (1, 6): # unittest-style discovery isn't available from .panels.test_redirects import * # noqa from .panels.test_request import * # noqa from .panels.test_sql import * # noqa + from .panels.test_staticfiles import * # noqa from .panels.test_template import * # noqa from .test_integration import * # noqa from .test_utils import * # noqa |
