|
| 1 | +from __future__ import absolute_import |
| 2 | +from os.path import normpath, join |
| 3 | +try: |
| 4 | + import threading |
| 5 | +except ImportError: |
| 6 | + threading = None |
| 7 | + |
| 8 | +from django.conf import settings |
| 9 | +from django.core.exceptions import ImproperlyConfigured |
| 10 | +from django.core.files.storage import get_storage_class |
| 11 | +from django.contrib.staticfiles import finders, storage |
| 12 | +from django.contrib.staticfiles.templatetags import staticfiles |
| 13 | + |
| 14 | +from django.utils.translation import ungettext, ugettext_lazy as _ |
| 15 | +from django.utils.datastructures import SortedDict |
| 16 | +from django.utils.functional import LazyObject |
| 17 | + |
| 18 | +from debug_toolbar import panels |
| 19 | +from debug_toolbar.utils import ThreadCollector |
| 20 | + |
| 21 | + |
| 22 | +class StaticFile(object): |
| 23 | + |
| 24 | + def __init__(self, path): |
| 25 | + self.path = path |
| 26 | + |
| 27 | + def __unicode__(self): |
| 28 | + return self.path |
| 29 | + |
| 30 | + def real_path(self): |
| 31 | + return finders.find(self.path) |
| 32 | + |
| 33 | + def url(self): |
| 34 | + return storage.staticfiles_storage.url(self.path) |
| 35 | + |
| 36 | + |
| 37 | +class FileCollector(ThreadCollector): |
| 38 | + |
| 39 | + def collect(self, path, thread=None): |
| 40 | + # handle the case of {% static "admin/" %} |
| 41 | + if path.endswith('/'): |
| 42 | + return |
| 43 | + super(FileCollector, self).collect(StaticFile(path), thread) |
| 44 | + |
| 45 | + |
| 46 | +collector = FileCollector() |
| 47 | + |
| 48 | + |
| 49 | +class DebugConfiguredStorage(LazyObject): |
| 50 | + def _setup(self): |
| 51 | + |
| 52 | + configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE) |
| 53 | + |
| 54 | + class DebugStaticFilesStorage(configured_storage_cls): |
| 55 | + |
| 56 | + def __init__(self, collector, *args, **kwargs): |
| 57 | + super(DebugStaticFilesStorage, self).__init__(*args, **kwargs) |
| 58 | + self.collector = collector |
| 59 | + |
| 60 | + def url(self, path): |
| 61 | + self.collector.collect(path) |
| 62 | + return super(DebugStaticFilesStorage, self).url(path) |
| 63 | + |
| 64 | + self._wrapped = DebugStaticFilesStorage(collector) |
| 65 | + |
| 66 | +storage.staticfiles_storage = staticfiles.staticfiles_storage = DebugConfiguredStorage() |
| 67 | + |
| 68 | + |
| 69 | +class StaticFilesPanel(panels.Panel): |
| 70 | + """ |
| 71 | + A panel to display the found staticfiles. |
| 72 | + """ |
| 73 | + name = 'Static files' |
| 74 | + template = 'debug_toolbar/panels/staticfiles.html' |
| 75 | + |
| 76 | + @property |
| 77 | + def title(self): |
| 78 | + return (_("Static files (%(num_found)s found)") % |
| 79 | + {'num_found': self.num_found, 'num_used': self.num_used}) |
| 80 | + |
| 81 | + def __init__(self, *args, **kwargs): |
| 82 | + super(StaticFilesPanel, self).__init__(*args, **kwargs) |
| 83 | + self.num_found = 0 |
| 84 | + self.ignore_patterns = [] |
| 85 | + self._paths = {} |
| 86 | + |
| 87 | + @property |
| 88 | + def has_content(self): |
| 89 | + if "django.contrib.staticfiles" not in settings.INSTALLED_APPS: |
| 90 | + raise ImproperlyConfigured("Could not find staticfiles in " |
| 91 | + "INSTALLED_APPS setting.") |
| 92 | + return True |
| 93 | + |
| 94 | + @property |
| 95 | + def num_used(self): |
| 96 | + return len(self._paths[threading.currentThread()]) |
| 97 | + |
| 98 | + nav_title = _('Static files') |
| 99 | + |
| 100 | + @property |
| 101 | + def nav_subtitle(self): |
| 102 | + num_used = self.num_used |
| 103 | + return ungettext("%(num_used)s file used", "%(num_used)s files used", |
| 104 | + num_used) % {'num_used': num_used} |
| 105 | + |
| 106 | + def process_request(self, request): |
| 107 | + collector.clear_collection() |
| 108 | + |
| 109 | + def process_response(self, request, response): |
| 110 | + staticfiles_finders = SortedDict() |
| 111 | + for finder in finders.get_finders(): |
| 112 | + for path, finder_storage in finder.list(self.ignore_patterns): |
| 113 | + if getattr(finder_storage, 'prefix', None): |
| 114 | + prefixed_path = join(finder_storage.prefix, path) |
| 115 | + else: |
| 116 | + prefixed_path = path |
| 117 | + finder_path = '.'.join([finder.__class__.__module__, |
| 118 | + finder.__class__.__name__]) |
| 119 | + real_path = finder_storage.path(path) |
| 120 | + payload = (prefixed_path, real_path) |
| 121 | + staticfiles_finders.setdefault(finder_path, []).append(payload) |
| 122 | + self.num_found += 1 |
| 123 | + |
| 124 | + dirs = getattr(settings, 'STATICFILES_DIRS', ()) |
| 125 | + |
| 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): |
| 139 | + for finder in finders.get_finders(): |
| 140 | + if isinstance(finder, finders.AppDirectoriesFinder): |
| 141 | + return finder.apps |
| 142 | + return [] |
0 commit comments