From 3b9f8f21ef2b714b6dbad53a0df23496bc97094d Mon Sep 17 00:00:00 2001 From: FroggyFlox Date: Tue, 17 Dec 2024 21:56:12 -0500 Subject: [PATCH 1/4] Initial setup and config of Django Debug Toolbar #2939 Add django-debug-toolbar package to the optional `dev` dependency group Add `--dev` flag to build.sh to run `poetry install` with that flag Add DjDTB to settings.py Add DjDTB urls --- build.sh | 14 +++++++++++++- poetry.lock | 17 ++++++++++++++++- pyproject.toml | 1 + src/rockstor/settings.py | 15 +++++++++++++++ src/rockstor/urls.py | 3 ++- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/build.sh b/build.sh index fecf72ba8..370158000 100755 --- a/build.sh +++ b/build.sh @@ -2,6 +2,11 @@ # exit on error set -o errexit +DEV_MODE=0 +if [ "$1" = "--dev" ]; then + DEV_MODE=1 +fi + # Install Poetry, a dependency management, packaging, and build system. # Uninstall legacy/transitional Poetry version of 1.1.15 PATH="/root/.local/bin:$PATH" # ensure legacy path. @@ -48,7 +53,14 @@ env > poetry-install.txt poetry --version >> poetry-install.txt poetry self show plugins >> poetry-install.txt # /usr/local/bin/poetry -> /opt/pipx/venvs/poetry -poetry install -vvv --no-interaction --no-ansi >> poetry-install.txt 2>&1 + +if [ $DEV_MODE -eq 1 ]; then + echo "Install djdt." + poetry install -vvv --no-interaction --no-ansi --with dev >> poetry-install-dev.txt 2>&1 +else + echo "Normal install." + poetry install -vvv --no-interaction --no-ansi >> poetry-install.txt 2>&1 +fi echo # Source package version from pyproject.toml's (version = "5.0.14") via `poetry version` output: diff --git a/poetry.lock b/poetry.lock index 9da6e3d49..c3e54ec9a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -407,6 +407,21 @@ tzdata = {version = "*", markers = "sys_platform == \"win32\""} argon2 = ["argon2-cffi (>=19.1.0)"] bcrypt = ["bcrypt"] +[[package]] +name = "django-debug-toolbar" +version = "4.4.6" +description = "A configurable set of panels that display various debug information about the current request/response." +optional = false +python-versions = ">=3.8" +files = [ + {file = "django_debug_toolbar-4.4.6-py3-none-any.whl", hash = "sha256:3beb671c9ec44ffb817fad2780667f172bd1c067dbcabad6268ce39a81335f45"}, + {file = "django_debug_toolbar-4.4.6.tar.gz", hash = "sha256:36e421cb908c2f0675e07f9f41e3d1d8618dc386392ec82d23bcfcd5d29c7044"}, +] + +[package.dependencies] +django = ">=4.2.9" +sqlparse = ">=0.2" + [[package]] name = "django-oauth-toolkit" version = "2.4.0" @@ -1416,4 +1431,4 @@ testing = ["coverage[toml]", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = "~3.11" -content-hash = "0c21eddc7cfacd5630b92eedc3aaf632e9ff8636a034e3f671bdd7911321836c" +content-hash = "c77696aa47193490bc669d04c12dd14251851f45a4a238e7c233188d14d6ca3e" diff --git a/pyproject.toml b/pyproject.toml index caaa5fb2e..3f0a65c73 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,6 +95,7 @@ optional = true [tool.poetry.group.dev.dependencies] black = "*" +django-debug-toolbar = "^4.4.6" [tool.poetry.scripts] # https://python-poetry.org/docs/pyproject#scripts diff --git a/src/rockstor/settings.py b/src/rockstor/settings.py index b6658904c..afee8c75a 100644 --- a/src/rockstor/settings.py +++ b/src/rockstor/settings.py @@ -162,6 +162,7 @@ ] MIDDLEWARE = ( + "debug_toolbar.middleware.DebugToolbarMiddleware", # New in 1.8, 1.11 newly sets Content-Length header. # 'django.middleware.common.CommonMiddleware', "django.contrib.sessions.middleware.SessionMiddleware", @@ -200,6 +201,7 @@ "smart_manager", "oauth2_provider", "huey.contrib.djhuey", + "debug_toolbar", ) # https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STORAGES @@ -470,3 +472,16 @@ # Note that the following will capture the build os version. # For live updates (running system) we call distro.version() directly in code. OS_DISTRO_VERSION = distro.version() # 3, 15.0 ,20181107 + +# Django Debug Toolbar-related settings and confrguration +INTERNAL_IPS = [ + "127.0.0.1", # It seems requests always come from this when using DRF +] + +# https://django-debug-toolbar.readthedocs.io/en/stable/configuration.html +DEBUG_TOOLBAR_CONFIG = { + # Update to the latest AJAX request + # Without this, we can only catch the initial request + # which is not helpful in most of our cases + "UPDATE_ON_FETCH": True +} diff --git a/src/rockstor/urls.py b/src/rockstor/urls.py index c103437bb..0a8c03fb1 100644 --- a/src/rockstor/urls.py +++ b/src/rockstor/urls.py @@ -18,6 +18,7 @@ from django.urls import include, re_path from django.views.static import serve from django.conf import settings +from debug_toolbar.toolbar import debug_toolbar_urls from smart_manager.views import ( BaseServiceView, @@ -149,4 +150,4 @@ re_path( r"^api/update-subscriptions/", include("storageadmin.urls.update_subscription") ), -] +] + debug_toolbar_urls() From 02f8ac071041b07bff0eaf8090bf533a2cf6ec56 Mon Sep 17 00:00:00 2001 From: FroggyFlox Date: Tue, 24 Dec 2024 15:05:52 -0500 Subject: [PATCH 2/4] Run DjDT only when DEBUG is set and not running tests #2939 --- src/rockstor/settings.py | 42 ++++++++++++++++++++++++++-------------- src/rockstor/urls.py | 10 ++++++++-- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/rockstor/settings.py b/src/rockstor/settings.py index afee8c75a..232e503e1 100644 --- a/src/rockstor/settings.py +++ b/src/rockstor/settings.py @@ -18,6 +18,8 @@ # Django settings for Rockstor project. import os +import sys + import distro import keyring from huey import SqliteHuey @@ -162,7 +164,7 @@ ] MIDDLEWARE = ( - "debug_toolbar.middleware.DebugToolbarMiddleware", + # "debug_toolbar.middleware.DebugToolbarMiddleware", # New in 1.8, 1.11 newly sets Content-Length header. # 'django.middleware.common.CommonMiddleware', "django.contrib.sessions.middleware.SessionMiddleware", @@ -201,7 +203,7 @@ "smart_manager", "oauth2_provider", "huey.contrib.djhuey", - "debug_toolbar", + # "debug_toolbar", ) # https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STORAGES @@ -473,15 +475,27 @@ # For live updates (running system) we call distro.version() directly in code. OS_DISTRO_VERSION = distro.version() # 3, 15.0 ,20181107 -# Django Debug Toolbar-related settings and confrguration -INTERNAL_IPS = [ - "127.0.0.1", # It seems requests always come from this when using DRF -] - -# https://django-debug-toolbar.readthedocs.io/en/stable/configuration.html -DEBUG_TOOLBAR_CONFIG = { - # Update to the latest AJAX request - # Without this, we can only catch the initial request - # which is not helpful in most of our cases - "UPDATE_ON_FETCH": True -} +# DJANGO DEBUG TOOLBAR settings and configuration +# recommended NOT to use the toolbar when running tests +# so enable it only when NOT running tests or NOT DEBUG +TESTING = "test" in sys.argv +if (not TESTING) and DEBUG: + INSTALLED_APPS = [ + *INSTALLED_APPS, + "debug_toolbar", + ] + MIDDLEWARE = [ + "debug_toolbar.middleware.DebugToolbarMiddleware", + *MIDDLEWARE, + ] + INTERNAL_IPS = [ + "127.0.0.1", # It seems requests always come from this when using DRF + ] + + # https://django-debug-toolbar.readthedocs.io/en/stable/configuration.html + DEBUG_TOOLBAR_CONFIG = { + # Update to the latest AJAX request + # Without this, we can only see the initial request in the toolbar panel + # Previous queries can still be seen and inspected in the "History" panel, though + "UPDATE_ON_FETCH": True + } diff --git a/src/rockstor/urls.py b/src/rockstor/urls.py index 0a8c03fb1..7542c35d4 100644 --- a/src/rockstor/urls.py +++ b/src/rockstor/urls.py @@ -18,7 +18,6 @@ from django.urls import include, re_path from django.views.static import serve from django.conf import settings -from debug_toolbar.toolbar import debug_toolbar_urls from smart_manager.views import ( BaseServiceView, @@ -150,4 +149,11 @@ re_path( r"^api/update-subscriptions/", include("storageadmin.urls.update_subscription") ), -] + debug_toolbar_urls() +] + +if (not settings.TESTING) and settings.DEBUG: + from debug_toolbar.toolbar import debug_toolbar_urls + + urlpatterns = [ + *urlpatterns, + ] + debug_toolbar_urls() From 116b6eb3bca752a0a6f6ced74068c24125b6f744 Mon Sep 17 00:00:00 2001 From: FroggyFlox Date: Tue, 24 Dec 2024 15:09:56 -0500 Subject: [PATCH 3/4] Minor cleanups #2939 --- src/rockstor/settings.py | 4 +--- src/rockstor/urls.py | 3 +++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/rockstor/settings.py b/src/rockstor/settings.py index 232e503e1..d38b2aa56 100644 --- a/src/rockstor/settings.py +++ b/src/rockstor/settings.py @@ -164,7 +164,6 @@ ] MIDDLEWARE = ( - # "debug_toolbar.middleware.DebugToolbarMiddleware", # New in 1.8, 1.11 newly sets Content-Length header. # 'django.middleware.common.CommonMiddleware', "django.contrib.sessions.middleware.SessionMiddleware", @@ -203,7 +202,6 @@ "smart_manager", "oauth2_provider", "huey.contrib.djhuey", - # "debug_toolbar", ) # https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STORAGES @@ -477,7 +475,7 @@ # DJANGO DEBUG TOOLBAR settings and configuration # recommended NOT to use the toolbar when running tests -# so enable it only when NOT running tests or NOT DEBUG +# so enable it only when in DEBUG mode and NOT running tests TESTING = "test" in sys.argv if (not TESTING) and DEBUG: INSTALLED_APPS = [ diff --git a/src/rockstor/urls.py b/src/rockstor/urls.py index 7542c35d4..4f0b7270f 100644 --- a/src/rockstor/urls.py +++ b/src/rockstor/urls.py @@ -151,6 +151,9 @@ ), ] +# DJANGO DEBUG TOOLBAR +# recommended NOT to use the toolbar when running tests +# so enable it only when in DEBUG mode and NOT running tests if (not settings.TESTING) and settings.DEBUG: from debug_toolbar.toolbar import debug_toolbar_urls From b3600ca8a8cc7d13c681e77cb43debbee91b6242 Mon Sep 17 00:00:00 2001 From: FroggyFlox Date: Tue, 24 Dec 2024 18:54:09 -0500 Subject: [PATCH 4/4] Use .env as dev_mode flag #2939 Rely on the DJANGO_DEBUG env variable to set DEBUG boolean that will in turn decide the installation of the dev group dependencies (includes the Django debug toolbar) as the appearance of the toolbar in the UI. Leave commented (= unset) by default to keep the default build NOT in the dev mode. --- .env | 1 + build.sh | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.env b/.env index f719655c8..6594770cc 100644 --- a/.env +++ b/.env @@ -12,3 +12,4 @@ PASSWORD_STORE_DIR=/root/.password-store # Django DJANGO_SETTINGS_MODULE=settings +#DJANGO_DEBUG=True diff --git a/build.sh b/build.sh index 370158000..903f4bb66 100755 --- a/build.sh +++ b/build.sh @@ -2,11 +2,6 @@ # exit on error set -o errexit -DEV_MODE=0 -if [ "$1" = "--dev" ]; then - DEV_MODE=1 -fi - # Install Poetry, a dependency management, packaging, and build system. # Uninstall legacy/transitional Poetry version of 1.1.15 PATH="/root/.local/bin:$PATH" # ensure legacy path. @@ -54,8 +49,8 @@ poetry --version >> poetry-install.txt poetry self show plugins >> poetry-install.txt # /usr/local/bin/poetry -> /opt/pipx/venvs/poetry -if [ $DEV_MODE -eq 1 ]; then - echo "Install djdt." +if [ "$DJANGO_DEBUG" = "True" ]; then + echo "Install Django Debug Toolbar." poetry install -vvv --no-interaction --no-ansi --with dev >> poetry-install-dev.txt 2>&1 else echo "Normal install."