Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

toolbar.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """
  2. The main DebugToolbar class that loads and renders the Toolbar.
  3. """
  4. from __future__ import absolute_import, unicode_literals
  5. import uuid
  6. from collections import OrderedDict
  7. from django.apps import apps
  8. from django.conf.urls import url
  9. from django.core.exceptions import ImproperlyConfigured
  10. from django.template import TemplateSyntaxError
  11. from django.template.loader import render_to_string
  12. from django.utils.module_loading import import_string
  13. from debug_toolbar import settings as dt_settings
  14. class DebugToolbar(object):
  15. def __init__(self, request):
  16. self.request = request
  17. self.config = dt_settings.get_config().copy()
  18. self._panels = OrderedDict()
  19. for panel_class in self.get_panel_classes():
  20. panel_instance = panel_class(self)
  21. self._panels[panel_instance.panel_id] = panel_instance
  22. self.stats = {}
  23. self.server_timing_stats = {}
  24. self.store_id = None
  25. # Manage panels
  26. @property
  27. def panels(self):
  28. """
  29. Get a list of all available panels.
  30. """
  31. return list(self._panels.values())
  32. @property
  33. def enabled_panels(self):
  34. """
  35. Get a list of panels enabled for the current request.
  36. """
  37. return [panel for panel in self._panels.values() if panel.enabled]
  38. def get_panel_by_id(self, panel_id):
  39. """
  40. Get the panel with the given id, which is the class name by default.
  41. """
  42. return self._panels[panel_id]
  43. # Handle rendering the toolbar in HTML
  44. def render_toolbar(self):
  45. """
  46. Renders the overall Toolbar with panels inside.
  47. """
  48. if not self.should_render_panels():
  49. self.store()
  50. try:
  51. context = {'toolbar': self}
  52. return render_to_string('debug_toolbar/base.html', context)
  53. except TemplateSyntaxError:
  54. if not apps.is_installed('django.contrib.staticfiles'):
  55. raise ImproperlyConfigured(
  56. "The debug toolbar requires the staticfiles contrib app. "
  57. "Add 'django.contrib.staticfiles' to INSTALLED_APPS and "
  58. "define STATIC_URL in your settings.")
  59. else:
  60. raise
  61. # Handle storing toolbars in memory and fetching them later on
  62. _store = OrderedDict()
  63. def should_render_panels(self):
  64. render_panels = self.config['RENDER_PANELS']
  65. if render_panels is None:
  66. render_panels = self.request.META['wsgi.multiprocess']
  67. return render_panels
  68. def store(self):
  69. self.store_id = uuid.uuid4().hex
  70. cls = type(self)
  71. cls._store[self.store_id] = self
  72. for _ in range(len(cls._store) - self.config['RESULTS_CACHE_SIZE']):
  73. try:
  74. # collections.OrderedDict
  75. cls._store.popitem(last=False)
  76. except TypeError:
  77. # django.utils.datastructures.SortedDict
  78. del cls._store[cls._store.keyOrder[0]]
  79. @classmethod
  80. def fetch(cls, store_id):
  81. return cls._store.get(store_id)
  82. # Manually implement class-level caching of panel classes and url patterns
  83. # because it's more obvious than going through an abstraction.
  84. _panel_classes = None
  85. @classmethod
  86. def get_panel_classes(cls):
  87. if cls._panel_classes is None:
  88. # Load panels in a temporary variable for thread safety.
  89. panel_classes = [
  90. import_string(panel_path)
  91. for panel_path in dt_settings.get_panels()
  92. ]
  93. cls._panel_classes = panel_classes
  94. return cls._panel_classes
  95. _urlpatterns = None
  96. @classmethod
  97. def get_urls(cls):
  98. if cls._urlpatterns is None:
  99. from . import views
  100. # Load URLs in a temporary variable for thread safety.
  101. # Global URLs
  102. urlpatterns = [
  103. url(r'^render_panel/$', views.render_panel, name='render_panel'),
  104. ]
  105. # Per-panel URLs
  106. for panel_class in cls.get_panel_classes():
  107. urlpatterns += panel_class.get_urls()
  108. cls._urlpatterns = urlpatterns
  109. return cls._urlpatterns
  110. app_name = 'djdt'
  111. urlpatterns = DebugToolbar.get_urls()