Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'djdt' is not a registered namespace #1405 #1889

Merged
merged 30 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5b2b969
changing the default position of the toolbar to the top half instead …
VeldaKiara Jan 23, 2024
2edf71e
updated the change to the changes.rst file
VeldaKiara Jan 23, 2024
3312069
fixing the position of the toolbar button to the upper top
VeldaKiara Jan 24, 2024
83e9739
solution to djdt registered namespace,update docs, system checks and …
VeldaKiara Feb 27, 2024
1193e0c
Merge branch 'main' into main
VeldaKiara Feb 27, 2024
3affaea
renaming the check to RUNNING_TESTS
VeldaKiara Feb 28, 2024
36cf020
renaming the check to RUNNING_TESTS
VeldaKiara Feb 28, 2024
8bf25e9
removing test in the if statements
VeldaKiara Feb 29, 2024
46f64c4
Add basic test to example app and example_test to make commands.
tim-schilling Feb 29, 2024
5941710
Update check for toolbar and tests.
tim-schilling Feb 29, 2024
df02d33
update check using with, combine the four tests to one, update defaul…
VeldaKiara Mar 8, 2024
18eaab5
update installation files and remove patch from namespace check
VeldaKiara Mar 11, 2024
2c21976
fix additional text on step 3 on example app
VeldaKiara Mar 11, 2024
e944857
Merge branch 'main' into main
VeldaKiara Mar 11, 2024
7f5da54
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 11, 2024
713c4cf
Remove .vscode/settings.json from tracking
VeldaKiara Mar 11, 2024
65bf1cb
remove unnecessary files
VeldaKiara Mar 11, 2024
c7ad698
Remove vscode.settings.json
VeldaKiara Mar 11, 2024
18ead4e
simple fix
VeldaKiara Mar 11, 2024
aef39b2
Merge branch 'main' into main
VeldaKiara Mar 25, 2024
a515cc1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 25, 2024
4820a9a
fix the assertion error
VeldaKiara Apr 26, 2024
424bf02
Delete .vscode/settings.json
tim-schilling Apr 26, 2024
c620b7c
Remove space from docs make file
tim-schilling Apr 26, 2024
effccff
Clean-up the changelog
tim-schilling Apr 26, 2024
902665c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 26, 2024
3cb2f7c
Add docs for the IS_RUNNING_TESTS setting
tim-schilling Apr 26, 2024
a182401
Apply suggestions from code review
tim-schilling Apr 26, 2024
9d906fb
Change the code for the toolbar testing error to E001
tim-schilling Apr 26, 2024
0e90884
Reduce number of .settings calls and document config update.
tim-schilling Apr 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion debug_toolbar/apps.py
@@ -1,9 +1,10 @@
import inspect
import mimetypes
import sys

from django.apps import AppConfig
from django.conf import settings
from django.core.checks import Warning, register
from django.core.checks import Error, Warning, register
from django.middleware.gzip import GZipMiddleware
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -206,3 +207,23 @@ def js_mimetype_check(app_configs, **kwargs):
)
]
return []


@register()
def debug_toolbar_namespace_check(app_configs, **kwargs):
VeldaKiara marked this conversation as resolved.
Show resolved Hide resolved
"""
Check that the Django Debug Toolbar is registered as a namespace.
"""
if (
settings.DEBUG is False
and dt_settings.get_config()["DEBUG_TOOLBAR_CONFIG"] == "test" in sys.argv
VeldaKiara marked this conversation as resolved.
Show resolved Hide resolved
):
return [
Error(
"django debug toolbar is not a registered namespace ",
hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration and run the tests.",
id="debug_toolbar.W008",
)
]
else:
return []
2 changes: 2 additions & 0 deletions debug_toolbar/settings.py
@@ -1,3 +1,4 @@
import sys
import warnings
from functools import lru_cache

Expand Down Expand Up @@ -42,6 +43,7 @@
"SQL_WARNING_THRESHOLD": 500, # milliseconds
"OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request",
"TOOLBAR_LANGUAGE": None,
"DEBUG_TOOLBAR_CONFIG": "test" in sys.argv,
VeldaKiara marked this conversation as resolved.
Show resolved Hide resolved
"UPDATE_ON_FETCH": False,
}

Expand Down
1 change: 1 addition & 0 deletions docs/Makefile
Expand Up @@ -8,6 +8,7 @@ SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build


# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
Expand Down
3 changes: 3 additions & 0 deletions docs/changes.rst
Expand Up @@ -29,10 +29,13 @@ Pending
<https://astral.sh/blog/the-ruff-formatter>`__.
* Changed the default position of the toolbar from top to the upper top
position.
* Fixed the bug causing ``'djdt' is not a registered namespace`` and updated
docs to help in initial configuration.
* Added the setting, ``UPDATE_ON_FETCH`` to control whether the
toolbar automatically updates to the latest AJAX request or not.
It defaults to ``False``.


4.2.0 (2023-08-10)
------------------

Expand Down
31 changes: 29 additions & 2 deletions docs/installation.rst
Expand Up @@ -99,7 +99,34 @@ Add django-debug-toolbar's URLs to your project's URLconf:
This example uses the ``__debug__`` prefix, but you can use any prefix that
doesn't clash with your application's URLs.

5. Add the Middleware
5. Configure django-debug-toolbar
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Add the following configuration for django-debug-toolbar in your setting:

.. code-block:: python

import sys

DEBUG = bool({"runserver"}.intersection(sys.argv))
VeldaKiara marked this conversation as resolved.
Show resolved Hide resolved

DEBUG_TOOLBAR_CONFIG = "test" in sys.argv

DEBUG_TOOLBAR = DEBUG and not DEBUG_TOOLBAR_CONFIG


To explicitly check the ``settings.DEBUG_TOOLBAR_CONFIG``variable before
including the URLs, add the following code in your ``urls`` file.

.. code-block:: python

if settings.DEBUG_TOOLBAR_CONFIG:

import debug_toolbar

urlpatterns += [path('__debug__/', include(debug_toolbar.urls))]

6. Add the Middleware
^^^^^^^^^^^^^^^^^^^^^

The Debug Toolbar is mostly implemented in a middleware. Add it to your
Expand All @@ -122,7 +149,7 @@ The Debug Toolbar is mostly implemented in a middleware. Add it to your

.. _internal-ips:

6. Configure Internal IPs
7. Configure Internal IPs
^^^^^^^^^^^^^^^^^^^^^^^^^

The Debug Toolbar is shown only if your IP address is listed in Django’s
Expand Down
12 changes: 11 additions & 1 deletion example/settings.py
@@ -1,15 +1,25 @@
"""Django settings for example project."""

import os
import sys

import environ

BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production

env = environ.Env()
environ.Env.read_env()

SECRET_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

DEBUG = True
# DEBUG = True

DEBUG = env("DEBUG", default=bool({"runserver"}.intersection(sys.argv)))
DEBUG_TOOLBAR_CONFIG = env("DEBUG_TOOLBAR_CONFIG", default="test" in sys.argv)
DEBUG_TOOLBAR = DEBUG and not DEBUG_TOOLBAR_CONFIG

INTERNAL_IPS = ["127.0.0.1", "::1"]

Expand Down
82 changes: 81 additions & 1 deletion tests/test_checks.py
@@ -1,12 +1,16 @@
import os
import sys
import unittest
from unittest.mock import patch

import django
from django.conf import settings
from django.core.checks import Warning, run_checks
from django.core.checks import Error, Warning, run_checks
from django.test import SimpleTestCase, override_settings

from debug_toolbar import settings as dt_settings
from debug_toolbar.apps import debug_toolbar_namespace_check

PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static")


Expand Down Expand Up @@ -258,3 +262,79 @@ def test_check_w007_invalid(self, mocked_guess_type):
)
],
)

# @patch("debug_toolbar.apps.debug_toolbar_namespace_check", [])
# def test_check_w008_valid(self):
# self.assertEqual(
# run_checks(),
# [
# Error(
# "debug toolbar is not a registered namespace",
# hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration",
# id="debug_toolbar.W008",
# )
# ],
# )


def test_check_w008_valid(self):
settings.DEBUG = True
dt_settings.get_config()["DEBUG_TOOLBAR_CONFIG"] = "test"
errors = debug_toolbar_namespace_check(None)
self.assertEqual(
errors,
[
Error(
"debug toolbar is not a registered namespace",
hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration",
id="debug_toolbar.W008",
)
],
)

@patch("debug_toolbar.apps.debug_toolbar_namespace_check", [])
def test_debug_toolbar_namespace_check_with_debug_false_and_config_not_test(
self, mocked_get_config
):
mocked_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "not_test"}
settings.DEBUG = False
errors = debug_toolbar_namespace_check(None)
self.assertEqual(len(errors), 0)

@patch("debug_toolbar.apps.debug_toolbar_namespace_check", [])
def test_debug_toolbar_namespace_check_with_debug_true_and_config_test(
self, mocked_get_config
):
mocked_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "test"}
settings.DEBUG = True
sys.argv = ["manage.py", "test"]
errors = debug_toolbar_namespace_check(None)
self.assertEqual(len(errors), 0)

@patch("debug_toolbar.apps.debug_toolbar_namespace_check", [])
def test_debug_toolbar_namespace_check_with_debug_true_and_config_not_test(
self, mock_get_config
):
mock_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "not_test"}
settings.DEBUG = True
sys.argv = ["manage.py", "test"]
errors = debug_toolbar_namespace_check(None)
self.assertEqual(len(errors), 0)

@patch("debug_toolbar.apps.debug_toolbar_namespace_check", [])
def test_debug_toolbar_namespace_check_with_debug_false_and_config_test(
self, mock_get_config
):
mock_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "test"}
settings.DEBUG = False
errors = debug_toolbar_namespace_check(None)
self.assertEqual(
errors,
[
Error(
"django debug toolbar is not a registered namespace ",
hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration and run the tests.",
id="debug_toolbar.W008",
)
],
)