Skip to content

Commit dbbc942

Browse files
committed
Introduce SCHEMA_GRAPH_VISIBLE setting
1 parent 6be868d commit dbbc942

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ Version numbers should follow https://semver.org/spec/v2.0.0.html
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Introduced `SCHEMA_GRAPH_VISIBLE` setting as a way to control access to the
13+
`Schema` view. We will continue to default to using `DEBUG`.
14+
15+
16+
### Changed
17+
18+
- We no longer use a decorator on the `Schema` view to override `dispatch`, and
19+
now override it directly.
20+
1021

1122
## [2.0.0] - 2022-08-01
1223

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ urlpatterns += [
5151

5252
Browse to `/schema/` (assuming that's where you put it in your URLs).
5353

54-
Note: `DEBUG` mode is required, on the assumption that you don't want to leak
55-
sensitive information about your website outside of local development.
54+
You can control access to this page using the `SCHEMA_GRAPH_VISIBLE` setting,
55+
or by subclassing `schema_graph.views.Schema` and overriding `access_permitted`.
56+
By default the page is only visible when `DEBUG` is `True`,
57+
because we assume that you don't want to leak sensitive information about your
58+
website outside of local development.
5659

5760
## Support
5861

schema_graph/views.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ class Schema(TemplateView):
1111
template_name = "schema_graph/schema.html"
1212

1313
def access_permitted(self):
14-
"""When this returns True, the schema graph page is accessible."""
15-
return settings.DEBUG
14+
"""
15+
When this returns True, the schema graph page is accessible.
16+
17+
We look for the setting `SCHEMA_GRAPH_VISIBLE`, and fall back to `DEBUG`.
18+
19+
To control this on a per-request basis, override this function in a subclass.
20+
The request will be accessible using `self.request`.
21+
"""
22+
23+
return getattr(settings, "SCHEMA_GRAPH_VISIBLE", settings.DEBUG)
1624

1725
def dispatch(self, request):
1826
if not self.access_permitted():

tests/test_views.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,35 @@ def test_content():
3636
assert response.rendered_content.startswith("<!doctype html>")
3737

3838

39-
def test_debug():
40-
"""Schema should be accessible in DEBUG mode."""
39+
@pytest.mark.parametrize(
40+
"settings_dict",
41+
[
42+
# SCHEMA_GRAPH_VISIBLE takes priority over DEBUG.
43+
{"DEBUG": True, "SCHEMA_GRAPH_VISIBLE": True},
44+
{"DEBUG": False, "SCHEMA_GRAPH_VISIBLE": True},
45+
{"DEBUG": True},
46+
],
47+
)
48+
def test_accessible_settings(settings_dict):
4149
view = Schema.as_view()
4250
request = create_request()
43-
with override_settings(DEBUG=True):
51+
with override_settings(**settings_dict):
4452
response = view(request)
4553
assert response.status_code == 200
4654

4755

48-
def test_no_debug():
49-
"""Schema should be inaccessible outwith DEBUG mode."""
56+
@pytest.mark.parametrize(
57+
"settings_dict",
58+
[
59+
# SCHEMA_GRAPH_VISIBLE takes priority over DEBUG.
60+
{"DEBUG": True, "SCHEMA_GRAPH_VISIBLE": False},
61+
{"DEBUG": False, "SCHEMA_GRAPH_VISIBLE": False},
62+
{"DEBUG": False},
63+
],
64+
)
65+
def test_inaccessible_settings(settings_dict):
5066
view = Schema.as_view()
5167
request = create_request()
52-
with override_settings(DEBUG=False):
68+
with override_settings(**settings_dict):
5369
with pytest.raises(Http404):
5470
view(request)

0 commit comments

Comments
 (0)