From 5014e2dd86c73179e01289923fe1aeb4eb325a73 Mon Sep 17 00:00:00 2001 From: Friedel Wolff Date: Thu, 1 Aug 2024 09:45:24 +0200 Subject: [PATCH 1/5] Allow separate toggling of DEBUG_TOOLBAR --- app/app/settings.py | 10 ++++++---- app/app/urls.py | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/app/settings.py b/app/app/settings.py index db46e5d6..dd70f369 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -29,6 +29,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = bool(os.getenv("DEBUG", "")) +DEBUG_TOOLBAR = DEBUG # to toggle separately if we want to ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "").split() USE_X_FORWARDED_HOST = bool(os.getenv("USE_X_FORWARDED_HOST", "")) @@ -50,13 +51,14 @@ "accounts", "django_filters", ] - -# Add django-extensions to the installed apps if DEBUG is True if DEBUG: INSTALLED_APPS += [ "django_extensions", - "debug_toolbar", ] + if DEBUG_TOOLBAR: + INSTALLED_APPS += [ + "debug_toolbar", + ] AUTH_USER_MODEL = "users.CustomUser" @@ -74,7 +76,7 @@ ] # Add debug toolbar middleware -if DEBUG: +if DEBUG and DEBUG_TOOLBAR: MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware") ROOT_URLCONF = "app.urls" diff --git a/app/app/urls.py b/app/app/urls.py index 00a8f37f..d5dbdcbc 100644 --- a/app/app/urls.py +++ b/app/app/urls.py @@ -49,4 +49,5 @@ if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) - urlpatterns.append(path("__debug__/", include("debug_toolbar.urls"))) + if settings.DEBUG_TOOLBAR: + urlpatterns.append(path("__debug__/", include("debug_toolbar.urls"))) From 99148b201dfdf2173c3d4429a16a39092e48d2cc Mon Sep 17 00:00:00 2001 From: Friedel Wolff Date: Thu, 1 Aug 2024 11:49:36 +0200 Subject: [PATCH 2/5] Rework DEBUG_TOOLBAR_CONFIG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... by removing it. The setting for SHOW_TOOLBAR_CALLBACK seems to have been an attempt to work around issues when running inside docker. Debug toolbar tries to handle this case, but it doesn't work sufficiently (at least in my case). The documentation has an opinion on this matter: Do not use DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": lambda request: DEBUG} in your project’s settings.py file. This commit is longer than I'd like, but the setting of INTERNAL_IPS seems to really fix it, and solves the fact that the debug context processor wasn't working either. With this fixed, {% debug %} works in templates. INTERNAL_IPS should best contain IP addresses rather than hostnames. IS_RUNNING_TESTS... no idea why we would want the toolbar while running tests. --- app/app/settings.py | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/app/app/settings.py b/app/app/settings.py index dd70f369..dbcf4790 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -118,16 +118,43 @@ } } -# toolbar settings if DEBUG: - DEBUG_TOOLBAR_CONFIG = { - "IS_RUNNING_TESTS": False, - "SHOW_TOOLBAR_CALLBACK": lambda request: DEBUG, - } + # Some things rely on the setting INTERNAL_IPS: + # - debug_toolbar.middleware.show_toolbar + # - django.template.context_processors.debug + # See https://docs.djangoproject.com/en/stable/ref/settings/#internal-ips + # Inside a docker container, it isn't trivial to get the IP address of the + # Docker host that will appear in REMOTE_ADDR. The following seems to work + # for now to add support for a range of IP addresses without having to put + # a huge list in INTERNAL_IPS, e.g. with + # map(str, ipaddress.ip_network('172.0.0.0/24')) + # If this can't resolve the name "host.docker.internal", we assume that the + # browser will contact localhost. + import socket + + try: + host_ip = socket.gethostbyname("host.docker.internal") + except socket.gaierror: + # presumably not in docker + host_ip = None + + import ipaddress + + # Based on https://code.djangoproject.com/ticket/3237#comment:12 + class CIDRList(list): + def __init__(self, addresses): + """Create a new ip_network object for each address range provided.""" + self.networks = [ipaddress.ip_network(address, strict=False) for address in addresses] + + def __contains__(self, address): + """Check if the given address is contained in any of the networks.""" + return any([ipaddress.ip_address(address) in network for network in self.networks]) + + if host_ip: + INTERNAL_IPS = CIDRList([f"{host_ip}/8"]) + else: + INTERNAL_IPS = ["127.0.0.1"] - INTERNAL_IPS = [ - "host.docker.internal", - ] # Email settings EMAIL_HOST = os.environ.get("EMAIL_HOST") From a4e27ddaf6037577e2ffa065e7fba902bbdfe811 Mon Sep 17 00:00:00 2001 From: Friedel Wolff Date: Thu, 1 Aug 2024 11:50:05 +0200 Subject: [PATCH 3/5] Insert DebugToolbarMiddleware at correct place --- app/app/settings.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/app/settings.py b/app/app/settings.py index dbcf4790..439040dc 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -65,6 +65,8 @@ MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "whitenoise.middleware.WhiteNoiseMiddleware", + # DebugToolbarMiddleware should go here if enabled. Done below. See + # https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#add-the-middleware "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", @@ -77,7 +79,7 @@ # Add debug toolbar middleware if DEBUG and DEBUG_TOOLBAR: - MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware") + MIDDLEWARE.insert(2, "debug_toolbar.middleware.DebugToolbarMiddleware") ROOT_URLCONF = "app.urls" From 18b8fca0763dc8471d00ba487a8b1108ff68329c Mon Sep 17 00:00:00 2001 From: Friedel Wolff Date: Thu, 1 Aug 2024 11:50:22 +0200 Subject: [PATCH 4/5] Insert LocaleMiddleware at correct place --- app/app/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/app/settings.py b/app/app/settings.py index 439040dc..514f6e5a 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -68,13 +68,13 @@ # DebugToolbarMiddleware should go here if enabled. Done below. See # https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#add-the-middleware "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.locale.LocaleMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", "simple_history.middleware.HistoryRequestMiddleware", - "django.middleware.locale.LocaleMiddleware", ] # Add debug toolbar middleware From ea17e4b9912725da1f79f244d15548ad6d0e2391 Mon Sep 17 00:00:00 2001 From: Friedel Wolff Date: Thu, 1 Aug 2024 11:55:24 +0200 Subject: [PATCH 5/5] Rename LOGGING_FOLDER_DEFAULT -> LOGGING_DIR --- app/app/settings.py | 6 ++---- app/general/tests/test_logging.py | 5 ++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/app/settings.py b/app/app/settings.py index 514f6e5a..d071fb8a 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -236,7 +236,7 @@ def __contains__(self, address): DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" -LOGGING_FOLDER_DEFAULT = os.path.abspath(os.path.join("/logging/")) +LOGGING_DIR = Path("/logging") # Internationalization @@ -283,9 +283,7 @@ def __contains__(self, address): "file": { "level": os.environ.get("LOGGING_HANDLERS_LEVEL", "WARNING"), "class": "logging.FileHandler", - "filename": os.path.join( - LOGGING_FOLDER_DEFAULT, os.environ.get("LOGGING_FILE", "debug.log") - ), + "filename": LOGGING_DIR / os.environ.get("LOGGING_FILE", "debug.log"), "formatter": "verbose", }, }, diff --git a/app/general/tests/test_logging.py b/app/general/tests/test_logging.py index 00926204..b24d6ccb 100644 --- a/app/general/tests/test_logging.py +++ b/app/general/tests/test_logging.py @@ -1,15 +1,14 @@ import logging import os -import sys from django.test import TestCase class LoggingTest(TestCase): def setUp(self): - LOGGING_FOLDER_DEFAULT = os.path.abspath(os.path.join("/logging/")) + LOGGING_DIR = "/logging" self.logger = logging.getLogger("django") - self.log_file = os.path.join(LOGGING_FOLDER_DEFAULT, "debug.log") + self.log_file = os.path.join(LOGGING_DIR, "debug.log") def test_log_file_created(self): """Test if the log file is created."""