Skip to content

Make show toolbar callback function async/sync compatible. #2066

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

Merged
merged 17 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
async compatible require_toolbar and tests
  • Loading branch information
salty-ivy committed Feb 25, 2025
commit e18301206ee69c1dd28bb1899424ec48ae3669fa
30 changes: 23 additions & 7 deletions debug_toolbar/decorators.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import functools

from asgiref.sync import iscoroutinefunction
from django.http import Http404
from django.utils.translation import get_language, override as language_override

from debug_toolbar import settings as dt_settings


def require_show_toolbar(view):
@functools.wraps(view)
def inner(request, *args, **kwargs):
from debug_toolbar.middleware import get_show_toolbar
"""
Async compatible decorator to restrict access to a view
based on the Debug Toolbar's visibility settings.
"""
from debug_toolbar.middleware import get_show_toolbar

if iscoroutinefunction(view):

show_toolbar = get_show_toolbar(async_mode=False)
if not show_toolbar(request):
raise Http404
@functools.wraps(view)
async def inner(request, *args, **kwargs):
show_toolbar = get_show_toolbar(async_mode=True)
if not await show_toolbar(request):
raise Http404

return view(request, *args, **kwargs)
return await view(request, *args, **kwargs)
else:

@functools.wraps(view)
def inner(request, *args, **kwargs):
show_toolbar = get_show_toolbar(async_mode=False)
if not show_toolbar(request):
raise Http404

return view(request, *args, **kwargs)

return inner

Expand Down
46 changes: 43 additions & 3 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
from unittest.mock import patch

from django.http import HttpResponse
from django.test import RequestFactory, TestCase
from django.http import Http404, HttpResponse
from django.test import AsyncRequestFactory, RequestFactory, TestCase
from django.test.utils import override_settings

from debug_toolbar.decorators import render_with_toolbar_language
from debug_toolbar.decorators import render_with_toolbar_language, require_show_toolbar


@render_with_toolbar_language
def stub_view(request):
return HttpResponse(200)


@require_show_toolbar
def stub_require_toolbar_view(request):
return HttpResponse(200)


@require_show_toolbar
async def stub_require_toolbar_async_view(request):
return HttpResponse(200)


class TestRequireToolbar(TestCase):
"""
Tests require_toolbar functionality and async compatibility.
"""

def setUp(self):
self.factory = RequestFactory()
self.async_factory = AsyncRequestFactory()

@override_settings(DEBUG=True)
def test_require_toolbar_debug_true(self):
response = stub_require_toolbar_view(self.factory.get("/"))
self.assertEqual(response.status_code, 200)

def test_require_toolbar_debug_false(self):
with self.assertRaises(Http404):
stub_require_toolbar_view(self.factory.get("/"))

# Following tests additionally tests async compatibility
# of require_toolbar decorator
@override_settings(DEBUG=True)
async def test_require_toolbar_async_debug_true(self):
response = await stub_require_toolbar_async_view(self.async_factory.get("/"))
self.assertEqual(response.status_code, 200)

async def test_require_toolbar_async_debug_false(self):
with self.assertRaises(Http404):
await stub_require_toolbar_async_view(self.async_factory.get("/"))


@override_settings(DEBUG=True, LANGUAGE_CODE="fr")
class RenderWithToolbarLanguageTestCase(TestCase):
@override_settings(DEBUG_TOOLBAR_CONFIG={"TOOLBAR_LANGUAGE": "de"})
Expand Down
Loading