|
| 1 | +from __future__ import absolute_import, unicode_literals |
| 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.encoding import python_2_unicode_compatible |
| 15 | +from django.utils.functional import LazyObject |
| 16 | +from django.utils.translation import ungettext, ugettext_lazy as _ |
| 17 | +try: |
| 18 | + from collections import OrderedDict |
| 19 | +except ImportError: |
| 20 | + from django.utils.datastructures import SortedDict as OrderedDict |
| 21 | + |
| 22 | +from debug_toolbar import panels |
| 23 | +from debug_toolbar.utils import ThreadCollector |
| 24 | + |
| 25 | + |
| 26 | +@python_2_unicode_compatible |
| 27 | +class StaticFile(object): |
| 28 | + """ |
| 29 | + Representing the different properties of a static file. |
| 30 | + """ |
| 31 | + def __init__(self, path): |
| 32 | + self.path = path |
| 33 | + |
| 34 | + def __str__(self): |
| 35 | + return self.path |
| 36 | + |
| 37 | + def real_path(self): |
| 38 | + return finders.find(self.path) |
| 39 | + |
| 40 | + def url(self): |
| 41 | + return storage.staticfiles_storage.url(self.path) |
| 42 | + |
| 43 | + |
| 44 | +class FileCollector(ThreadCollector): |
| 45 | + |
| 46 | + def collect(self, path, thread=None): |
| 47 | + # handle the case of {% static "admin/" %} |
| 48 | + if path.endswith('/'): |
| 49 | + return |
| 50 | + super(FileCollector, self).collect(StaticFile(path), thread) |
| 51 | + |
| 52 | + |
| 53 | +collector = FileCollector() |
| 54 | + |
| 55 | + |
| 56 | +class DebugConfiguredStorage(LazyObject): |
| 57 | + """ |
| 58 | + A staticfiles storage class to be used for collecting which paths |
| 59 | + are resolved by using the {% static %} template tag (which uses the |
| 60 | + `url` method). |
| 61 | + """ |
| 62 | + def _setup(self): |
| 63 | + |
| 64 | + configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE) |
| 65 | + |
| 66 | + class DebugStaticFilesStorage(configured_storage_cls): |
| 67 | + |
| 68 | + def __init__(self, collector, *args, **kwargs): |
| 69 | + super(DebugStaticFilesStorage, self).__init__(*args, **kwargs) |
| 70 | + self.collector = collector |
| 71 | + |
| 72 | + def url(self, path): |
| 73 | + self.collector.collect(path) |
| 74 | + return super(DebugStaticFilesStorage, self).url(path) |
| 75 | + |
| 76 | + self._wrapped = DebugStaticFilesStorage(collector) |
| 77 | + |
| 78 | +_original_storage = storage.staticfiles_storage |
| 79 | + |
| 80 | + |
| 81 | +class StaticFilesPanel(panels.Panel): |
| 82 | + """ |
| 83 | + A panel to display the found staticfiles. |
| 84 | + """ |
| 85 | + name = 'Static files' |
| 86 | + template = 'debug_toolbar/panels/staticfiles.html' |
| 87 | + |
| 88 | + @property |
| 89 | + def title(self): |
| 90 | + return (_("Static files (%(num_found)s found, %(num_used)s used)") % |
| 91 | + {'num_found': self.num_found, 'num_used': self.num_used}) |
| 92 | + |
| 93 | + def __init__(self, *args, **kwargs): |
| 94 | + super(StaticFilesPanel, self).__init__(*args, **kwargs) |
| 95 | + self.num_found = 0 |
| 96 | + self._paths = {} |
| 97 | + |
| 98 | + def enable_instrumentation(self): |
| 99 | + storage.staticfiles_storage = staticfiles.staticfiles_storage = DebugConfiguredStorage() |
| 100 | + |
| 101 | + def disable_instrumentation(self): |
| 102 | + storage.staticfiles_storage = staticfiles.staticfiles_storage = _original_storage |
| 103 | + |
| 104 | + @property |
| 105 | + def has_content(self): |
| 106 | + if "django.contrib.staticfiles" not in settings.INSTALLED_APPS: |
| 107 | + raise ImproperlyConfigured("Could not find staticfiles in " |
| 108 | + "INSTALLED_APPS setting.") |
| 109 | + return True |
| 110 | + |
| 111 | + @property |
| 112 | + def num_used(self): |
| 113 | + return len(self._paths[threading.currentThread()]) |
| 114 | + |
| 115 | + nav_title = _('Static files') |
| 116 | + |
| 117 | + @property |
| 118 | + def nav_subtitle(self): |
| 119 | + num_used = self.num_used |
| 120 | + return ungettext("%(num_used)s file used", |
| 121 | + "%(num_used)s files used", |
| 122 | + num_used) % {'num_used': num_used} |
| 123 | + |
| 124 | + def process_request(self, request): |
| 125 | + collector.clear_collection() |
| 126 | + |
| 127 | + def process_response(self, request, response): |
| 128 | + used_paths = collector.get_collection() |
| 129 | + self._paths[threading.currentThread()] = used_paths |
| 130 | + |
| 131 | + self.record_stats({ |
| 132 | + 'num_found': self.num_found, |
| 133 | + 'num_used': self.num_used, |
| 134 | + 'staticfiles': used_paths, |
| 135 | + 'staticfiles_apps': self.get_staticfiles_apps(), |
| 136 | + 'staticfiles_dirs': self.get_staticfiles_dirs(), |
| 137 | + 'staticfiles_finders': self.get_staticfiles_finders(), |
| 138 | + }) |
| 139 | + |
| 140 | + def get_staticfiles_finders(self): |
| 141 | + """ |
| 142 | + Returns a sorted mapping between the finder path and the list |
| 143 | + of relative and file system paths which that finder was able |
| 144 | + to find. |
| 145 | + """ |
| 146 | + finders_mapping = OrderedDict() |
| 147 | + for finder in finders.get_finders(): |
| 148 | + for path, finder_storage in finder.list([]): |
| 149 | + if getattr(finder_storage, 'prefix', None): |
| 150 | + prefixed_path = join(finder_storage.prefix, path) |
| 151 | + else: |
| 152 | + prefixed_path = path |
| 153 | + finder_cls = finder.__class__ |
| 154 | + finder_path = '.'.join([finder_cls.__module__, |
| 155 | + finder_cls.__name__]) |
| 156 | + real_path = finder_storage.path(path) |
| 157 | + payload = (prefixed_path, real_path) |
| 158 | + finders_mapping.setdefault(finder_path, []).append(payload) |
| 159 | + self.num_found += 1 |
| 160 | + return finders_mapping |
| 161 | + |
| 162 | + def get_staticfiles_dirs(self): |
| 163 | + """ |
| 164 | + Returns a list of paths to inspect for additional static files |
| 165 | + """ |
| 166 | + dirs = getattr(settings, 'STATICFILES_DIRS', ()) |
| 167 | + return [normpath(d) for d in dirs] |
| 168 | + |
| 169 | + def get_staticfiles_apps(self): |
| 170 | + """ |
| 171 | + Returns a list of app paths that have a static directory |
| 172 | + """ |
| 173 | + apps = [] |
| 174 | + for finder in finders.get_finders(): |
| 175 | + if isinstance(finder, finders.AppDirectoriesFinder): |
| 176 | + for app in finder.apps: |
| 177 | + if app not in apps: |
| 178 | + apps.append(app) |
| 179 | + return apps |
0 commit comments