Skip to content

Commit a44c91d

Browse files
Remove signed_data_view decorator to support url type checking. (#1658)
* Remove signed_data_view decorator to support url type checking. The package django-urlconfchecks was erroring on the toolbar's URLs because signed_data_view was injecting an extra parameter for the views. The alternative to the decorator isn't terrible so let's use that instead. Eventually this can be shortened with the walrus operator when py37 support is dropped. * Use django-urlconfchecks in a URL to avoid adding it to docs' words list.
1 parent 80d6063 commit a44c91d

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

debug_toolbar/decorators.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
22

3-
from django.http import Http404, HttpResponseBadRequest
3+
from django.http import Http404
44

55

66
def require_show_toolbar(view):
@@ -15,21 +15,3 @@ def inner(request, *args, **kwargs):
1515
return view(request, *args, **kwargs)
1616

1717
return inner
18-
19-
20-
def signed_data_view(view):
21-
"""Decorator that handles unpacking a signed data form"""
22-
23-
@functools.wraps(view)
24-
def inner(request, *args, **kwargs):
25-
from debug_toolbar.forms import SignedDataForm
26-
27-
data = request.GET if request.method == "GET" else request.POST
28-
signed_form = SignedDataForm(data)
29-
if signed_form.is_valid():
30-
return view(
31-
request, *args, verified_data=signed_form.verified_data(), **kwargs
32-
)
33-
return HttpResponseBadRequest("Invalid signature")
34-
35-
return inner

debug_toolbar/forms.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class PanelForm(forms.Form):
2121
panel_form = PanelForm(signed_form.verified_data)
2222
if panel_form.is_valid():
2323
# Success
24-
Or wrap the FBV with ``debug_toolbar.decorators.signed_data_view``
2524
"""
2625

2726
salt = "django_debug_toolbar"

debug_toolbar/panels/sql/views.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@
22
from django.template.loader import render_to_string
33
from django.views.decorators.csrf import csrf_exempt
44

5-
from debug_toolbar.decorators import require_show_toolbar, signed_data_view
5+
from debug_toolbar.decorators import require_show_toolbar
6+
from debug_toolbar.forms import SignedDataForm
67
from debug_toolbar.panels.sql.forms import SQLSelectForm
78

89

10+
def get_signed_data(request):
11+
"""Unpack a signed data form, if invalid returns None"""
12+
data = request.GET if request.method == "GET" else request.POST
13+
signed_form = SignedDataForm(data)
14+
if signed_form.is_valid():
15+
return signed_form.verified_data()
16+
return None
17+
18+
919
@csrf_exempt
1020
@require_show_toolbar
11-
@signed_data_view
12-
def sql_select(request, verified_data):
21+
def sql_select(request):
1322
"""Returns the output of the SQL SELECT statement"""
23+
verified_data = get_signed_data(request)
24+
if not verified_data:
25+
return HttpResponseBadRequest("Invalid signature")
1426
form = SQLSelectForm(verified_data)
1527

1628
if form.is_valid():
@@ -35,9 +47,11 @@ def sql_select(request, verified_data):
3547

3648
@csrf_exempt
3749
@require_show_toolbar
38-
@signed_data_view
39-
def sql_explain(request, verified_data):
50+
def sql_explain(request):
4051
"""Returns the output of the SQL EXPLAIN on the given query"""
52+
verified_data = get_signed_data(request)
53+
if not verified_data:
54+
return HttpResponseBadRequest("Invalid signature")
4155
form = SQLSelectForm(verified_data)
4256

4357
if form.is_valid():
@@ -71,9 +85,11 @@ def sql_explain(request, verified_data):
7185

7286
@csrf_exempt
7387
@require_show_toolbar
74-
@signed_data_view
75-
def sql_profile(request, verified_data):
88+
def sql_profile(request):
7689
"""Returns the output of running the SQL and getting the profiling statistics"""
90+
verified_data = get_signed_data(request)
91+
if not verified_data:
92+
return HttpResponseBadRequest("Invalid signature")
7793
form = SQLSelectForm(verified_data)
7894

7995
if form.is_valid():

docs/changes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Change log
22
==========
33

4+
Pending
5+
-------
6+
7+
* Remove decorator ``signed_data_view`` as it was causing issues with
8+
`django-urlconfchecks <https://github.com/AliSayyah/django-urlconfchecks/>`__.
9+
410
3.5.0 (2022-06-23)
511
------------------
612

0 commit comments

Comments
 (0)