• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MrThearMan / undine / 17707802818

14 Sep 2025 06:58AM UTC coverage: 96.86% (-0.006%) from 96.866%
17707802818

push

github

MrThearMan
Remove unused just commands

1805 of 1907 branches covered (94.65%)

Branch coverage included in aggregate %.

29689 of 30608 relevant lines covered (97.0%)

2.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.9
/undine/integrations/debug_toolbar.py
1
from __future__ import annotations
3✔
2

3
import json
3✔
4
from typing import TYPE_CHECKING, Any
3✔
5

6
from debug_toolbar.middleware import DebugToolbarMiddleware
3✔
7
from debug_toolbar.utils import is_processable_html_response
3✔
8
from django.core.serializers.json import DjangoJSONEncoder
3✔
9
from django.http.request import MediaType
3✔
10
from django.template.loader import render_to_string
3✔
11
from django.utils.encoding import force_str
3✔
12

13
from undine.settings import undine_settings
3✔
14

15
if TYPE_CHECKING:
16
    from debug_toolbar.toolbar import DebugToolbar
17
    from django.http import HttpRequest, HttpResponse
18

19
__all__ = [
3✔
20
    "monkeypatch_middleware",
21
]
22

23

24
def monkeypatch_middleware() -> None:
3✔
25
    """Insert additional GraphiQL handling to the debug toolbar middleware."""
26
    original_postprocess = DebugToolbarMiddleware._postprocess  # noqa: SLF001
3✔
27

28
    def patched_postprocess(
3✔
29
        self: DebugToolbarMiddleware,
30
        request: HttpRequest,
31
        response: HttpResponse,
32
        toolbar: DebugToolbar,
33
    ) -> HttpResponse:
34
        response = original_postprocess(self, request, response, toolbar)
3✔
35
        if _is_graphql_view(request):
3✔
36
            handle_graphiql(request, response, toolbar)
3✔
37
        return response
3✔
38

39
    DebugToolbarMiddleware._postprocess = patched_postprocess  # noqa: SLF001
3✔
40

41

42
def handle_graphiql(request: HttpRequest, response: HttpResponse, toolbar: DebugToolbar) -> None:
3✔
43
    """Add debug toolbar data to GraphiQL view responses."""
44
    if is_processable_html_response(response):
3✔
45
        add_toolbar_update_script(response)
3✔
46
        return
3✔
47

48
    if not _is_json_response(response):
3✔
49
        return
×
50

51
    if _is_introspection_query(request):
3✔
52
        return
3✔
53

54
    add_debug_toolbar_data(response, toolbar)
3✔
55
    return
3✔
56

57

58
def add_toolbar_update_script(response: HttpResponse) -> None:
3✔
59
    """Add the JS script to the GraphiQL template for updating the toolbar after a request."""
60
    template = render_to_string("undine/graphiql_debug_toolbar_patch.html")
3✔
61
    response.write(template)
3✔
62
    if "Content-Length" in response:
3✔
63
        response["Content-Length"] = len(response.content)
×
64

65

66
def add_debug_toolbar_data(response: HttpResponse, toolbar: DebugToolbar) -> None:
3✔
67
    """
68
    Add data for the debug toolbar to the response.
69
    Will be used by '/template/undine/patch_debug_toolbar.html'.
70
    """
71
    content = force_str(response.content, encoding=response.charset)
3✔
72
    payload = json.loads(content)
3✔
73

74
    try:
3✔
75
        request_id = toolbar.request_id
3✔
76
    except AttributeError:
×
77
        #  Debug toolbar < 6.0.0 compatibility
78
        request_id = toolbar.store_id
×
79

80
    payload["debugToolbar"] = {"requestId": request_id, "panels": {}}
3✔
81

82
    for panel in reversed(toolbar.enabled_panels):
3✔
83
        payload["debugToolbar"]["panels"][panel.panel_id] = {
3✔
84
            "toolbarTitle": _call_if_callable(panel.nav_subtitle),
85
            "panelTitle": _call_if_callable(panel.title) if panel.has_content else None,
86
        }
87

88
    response.content = json.dumps(payload, cls=DjangoJSONEncoder)
3✔
89
    if "Content-Length" in response:
3✔
90
        response["Content-Length"] = len(response.content)
×
91

92

93
def _is_json_response(response: HttpResponse) -> bool:
3✔
94
    if getattr(response, "streaming", False):
3✔
95
        return False
×
96

97
    content_encoding = response.get("Content-Encoding", "")
3✔
98
    if content_encoding:
3✔
99
        return False
×
100

101
    content_type = response.get("Content-Type", "")
3✔
102
    if not content_type:
3✔
103
        return False
×
104

105
    media_type = MediaType(content_type)
3✔
106

107
    return media_type.match("application/json") or media_type.match("application/graphql-response+json")
3✔
108

109

110
def _is_introspection_query(request: HttpRequest) -> bool:
3✔
111
    try:
3✔
112
        body = json.loads(request.body)
3✔
113
    except Exception:  # noqa: BLE001
×
114
        return False
×
115

116
    return body.get("operationName") == "IntrospectionQuery"
3✔
117

118

119
def _is_graphql_view(request: HttpRequest) -> bool:
3✔
120
    if request.resolver_match is None:
3✔
121
        return False
×
122

123
    return request.resolver_match.view_name == f"undine:{undine_settings.GRAPHQL_VIEW_NAME}"
3✔
124

125

126
def _call_if_callable(obj: Any) -> Any:
3✔
127
    if callable(obj):
3✔
128
        return obj()
3✔
129
    return obj
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc