From 7fcb1c58c62e249d7e2b8f8a0a07a7b6f91dd0f9 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Wed, 4 Dec 2024 13:06:55 +0000 Subject: [PATCH] ref: Make react_config available in context This patch refactors a 10-year-old hack where we used a template tag to produce the initial data JSON, making the contents of that blob unavailable to any Django template. With this, all views extending from base gets a `react_config` context object so we can do things like sniffing user's language or theme preferences if available and modify the HTML output based on that. Related #81611 --- src/sentry/templates/sentry/base-react.html | 2 ++ src/sentry/templates/sentry/layout.html | 3 +-- src/sentry/templatetags/sentry_react.py | 13 ------------- src/sentry/web/frontend/base.py | 5 ++++- 4 files changed, 7 insertions(+), 16 deletions(-) delete mode 100644 src/sentry/templatetags/sentry_react.py diff --git a/src/sentry/templates/sentry/base-react.html b/src/sentry/templates/sentry/base-react.html index d8c0ffd686f3b1..2d8e12ab7c4509 100644 --- a/src/sentry/templates/sentry/base-react.html +++ b/src/sentry/templates/sentry/base-react.html @@ -2,6 +2,8 @@ {% load sentry_api sentry_assets sentry_helpers %} +{% block wrapperclass %}theme-{% user_theme %}{% endblock %} + {% block body %}
diff --git a/src/sentry/templates/sentry/layout.html b/src/sentry/templates/sentry/layout.html index 17a2783c12755e..24c08d227f49dd 100644 --- a/src/sentry/templates/sentry/layout.html +++ b/src/sentry/templates/sentry/layout.html @@ -3,7 +3,6 @@ {% load sentry_assets %} {% load sentry_features %} {% load sentry_helpers %} -{% load sentry_react %} {% load sentry_status %} {% get_sentry_version %} @@ -41,7 +40,7 @@ {% block initial_data %} {% script %} {% endscript %} {% endblock %} diff --git a/src/sentry/templatetags/sentry_react.py b/src/sentry/templatetags/sentry_react.py deleted file mode 100644 index a1f91609b5e2bc..00000000000000 --- a/src/sentry/templatetags/sentry_react.py +++ /dev/null @@ -1,13 +0,0 @@ -from django import template - -from sentry.utils import json -from sentry.web.client_config import get_client_config - -register = template.Library() - - -@register.simple_tag(takes_context=True) -def get_react_config(context): - context = get_client_config(context.get("request", None), context.get("org_context")) - - return json.dumps_htmlsafe(context) diff --git a/src/sentry/web/frontend/base.py b/src/sentry/web/frontend/base.py index 7eb53b1c374ac9..d260d5ff778c7d 100644 --- a/src/sentry/web/frontend/base.py +++ b/src/sentry/web/frontend/base.py @@ -48,6 +48,7 @@ from sentry.utils.audit import create_audit_entry from sentry.utils.auth import construct_link_with_query, is_valid_redirect from sentry.utils.http import absolute_uri, is_using_customer_domain, origin_from_request +from sentry.web.client_config import get_client_config from sentry.web.frontend.generic import FOREVER_CACHE from sentry.web.helpers import render_to_response from sudo.views import redirect_to_sudo @@ -467,7 +468,9 @@ def get_not_2fa_compliant_url(self, request: HttpRequest, *args: Any, **kwargs: return reverse("sentry-account-settings-security") def get_context_data(self, request: HttpRequest, **kwargs: Any) -> dict[str, Any]: - context = csrf(request) + context = {"react_config": get_client_config(request, self.active_organization)} | csrf( + request + ) return context def respond(