From f82b82502cc5b7ccd7a256f8b5e5f3eef296c35f Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Tue, 20 Dec 2022 15:26:39 +0000 Subject: [PATCH] Install debug toolbar in debug mode --- activities/views/posts.py | 5 ----- core/middleware.py | 8 ++++++++ requirements.txt | 1 + takahe/settings.py | 6 +++++- takahe/urls.py | 4 +++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/activities/views/posts.py b/activities/views/posts.py index 36f8fb3..967352e 100644 --- a/activities/views/posts.py +++ b/activities/views/posts.py @@ -40,11 +40,6 @@ class Individual(TemplateView): ancestors, descendants = PostService(self.post_obj).context( self.request.identity ) - print( - self.post_obj.to_mastodon_json(), - self.post_obj.emojis.all(), - self.post_obj.emojis.usable(), - ) return { "identity": self.identity, "post": self.post_obj, diff --git a/core/middleware.py b/core/middleware.py index f8c5523..db60559 100644 --- a/core/middleware.py +++ b/core/middleware.py @@ -1,5 +1,6 @@ from time import time +from django.conf import settings from django.core.exceptions import MiddlewareNotUsed from core import sentry @@ -65,3 +66,10 @@ class SentryTaggingMiddleware: sentry.set_takahe_app("web") response = self.get_response(request) return response + + +def show_toolbar(request): + """ + Determines whether to show the debug toolbar on a given page. + """ + return settings.DEBUG and request.user.is_authenticated and request.user.admin diff --git a/requirements.txt b/requirements.txt index ae3c43d..fd89b83 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ cryptography~=38.0 dj_database_url~=1.0.0 django-cache-url~=3.4.2 django-cors-headers~=3.13.0 +django-debug-toolbar~=3.8.1 django-htmx~=1.13.0 django-ninja~=0.19.1 django-oauth-toolkit~=2.2.0 diff --git a/takahe/settings.py b/takahe/settings.py index 2789066..5225fe9 100644 --- a/takahe/settings.py +++ b/takahe/settings.py @@ -175,8 +175,9 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", - "django_htmx", "corsheaders", + "debug_toolbar", + "django_htmx", "core", "activities", "api", @@ -194,6 +195,7 @@ MIDDLEWARE = [ "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", + "debug_toolbar.middleware.DebugToolbarMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", "django_htmx.middleware.HtmxMiddleware", @@ -309,6 +311,8 @@ MEDIA_URL = SETUP.MEDIA_URL MEDIA_ROOT = SETUP.MEDIA_ROOT MAIN_DOMAIN = SETUP.MAIN_DOMAIN +if DEBUG: + DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": "core.middleware.show_toolbar"} if SETUP.USE_PROXY_HEADERS: SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") diff --git a/takahe/urls.py b/takahe/urls.py index 85504a2..3f7d77b 100644 --- a/takahe/urls.py +++ b/takahe/urls.py @@ -1,6 +1,6 @@ from django.conf import settings as djsettings from django.contrib import admin as djadmin -from django.urls import path, re_path +from django.urls import include, path, re_path from activities.views import compose, debug, explore, follows, posts, search, timelines from api.views import api_router, oauth @@ -246,6 +246,8 @@ urlpatterns = [ path(".stator/", stator.RequestRunner.as_view()), # Django admin path("djadmin/", djadmin.site.urls), + # Debug toolbar + path("__debug__/", include("debug_toolbar.urls")), # Media files re_path( r"^media/(?P.*)$",