Skip to content

Commit 595e932

Browse files
authored
ref: Make react_config available in context (#81654)
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
1 parent ed6d3df commit 595e932

File tree

8 files changed

+20
-11
lines changed

8 files changed

+20
-11
lines changed

Diff for: src/sentry/middleware/customer_domain.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ def _resolve_redirect_url(request, activeorg):
7474

7575
path = reverse(result.url_name or result.func, kwargs=kwargs)
7676
qs = _query_string(request)
77-
redirect_url = f"{redirect_url}{path}{qs}"
78-
return redirect_url
77+
return f"{redirect_url}{path}{qs}"
7978

8079

8180
class CustomerDomainMiddleware:

Diff for: src/sentry/templates/sentry/layout.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{% load sentry_assets %}
44
{% load sentry_features %}
55
{% load sentry_helpers %}
6-
{% load sentry_react %}
76
{% load sentry_status %}
87
{% get_sentry_version %}
98
<!DOCTYPE html>
@@ -41,7 +40,7 @@
4140
{% block initial_data %}
4241
{% script %}
4342
<script>
44-
window.__initialData = {% get_react_config %};
43+
window.__initialData = {{ react_config|to_json }};
4544
</script>
4645
{% endscript %}
4746
{% endblock %}

Diff for: src/sentry/web/frontend/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,7 @@ def get_not_2fa_compliant_url(self, request: HttpRequest, *args: Any, **kwargs:
467467
return reverse("sentry-account-settings-security")
468468

469469
def get_context_data(self, request: HttpRequest, **kwargs: Any) -> dict[str, Any]:
470-
context = csrf(request)
471-
return context
470+
return csrf(request)
472471

473472
def respond(
474473
self, template: str, context: dict[str, Any] | None = None, status: int = 200

Diff for: src/sentry/web/frontend/js_sdk_loader.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import time
4-
from typing import NotRequired, TypedDict
4+
from typing import Any, NotRequired, TypedDict
55

66
from django.conf import settings
77
from django.http import HttpRequest, HttpResponse
@@ -55,6 +55,10 @@ class JavaScriptSdkLoader(BaseView):
5555
def determine_active_organization(self, request: HttpRequest, organization_slug=None) -> None:
5656
pass
5757

58+
# Same as above
59+
def get_context_data(self, request: HttpRequest, **kwargs) -> dict[str, Any]:
60+
return {}
61+
5862
def _get_loader_config(
5963
self, key: ProjectKey | None, sdk_version: Version | None
6064
) -> LoaderInternalConfig:

Diff for: src/sentry/web/frontend/react_page.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from sentry.users.services.user.model import RpcUser
2424
from sentry.utils.http import is_using_customer_domain, query_string
25+
from sentry.web.client_config import get_client_config
2526
from sentry.web.frontend.base import BaseView, ControlSiloOrganizationView
2627
from sentry.web.helpers import render_to_response
2728

@@ -86,6 +87,7 @@ def dns_prefetch(self) -> list[str]:
8687
return domains
8788

8889
def handle_react(self, request: Request, **kwargs) -> HttpResponse:
90+
org_context = getattr(self, "active_organization", None)
8991
context = {
9092
"CSRF_COOKIE_NAME": settings.CSRF_COOKIE_NAME,
9193
"meta_tags": [
@@ -97,7 +99,8 @@ def handle_react(self, request: Request, **kwargs) -> HttpResponse:
9799
# Rendering the layout requires serializing the active organization.
98100
# Since we already have it here from the OrganizationMixin, we can
99101
# save some work and render it faster.
100-
"org_context": getattr(self, "active_organization", None),
102+
"org_context": org_context,
103+
"react_config": get_client_config(request, org_context),
101104
}
102105

103106
# Force a new CSRF token to be generated and set in user's

Diff for: tests/sentry/web/frontend/test_js_sdk_loader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def test_headers(self, mock_load_version_from_file):
339339
assert "sdk-loader" in resp["Surrogate-Key"]
340340
assert "Content-Encoding" not in resp
341341
assert "Set-Cookie" not in resp
342-
assert "Vary" not in resp
342+
assert "Vary" not in resp, f"Found Vary header: {resp['Vary']}"
343343

344344
def test_absolute_url(self):
345345
assert (

Diff for: tests/sentry/web/frontend/test_react_page.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,14 @@ def _run_customer_domain_elevated_privileges(self, is_superuser: bool, is_staff:
296296
assert response.redirect_chain == [
297297
(f"http://{other_org.slug}.testserver/issues/", 302)
298298
]
299-
assert self.client.session["activeorg"] == other_org.slug
300299
else:
301300
assert response.redirect_chain == [
302301
(f"http://{other_org.slug}.testserver/auth/login/{other_org.slug}/", 302)
303302
]
303+
304+
if is_superuser or is_staff:
305+
assert self.client.session["activeorg"] == other_org.slug
306+
else:
304307
assert "activeorg" not in self.client.session
305308

306309
# Accessing org without customer domain as superuser and/or staff.

Diff for: tests/sentry/web/test_api.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,13 @@ def _run_test_with_privileges(self, is_superuser: bool, is_staff: bool):
301301
assert response.redirect_chain == [
302302
(f"http://{other_org.slug}.testserver/issues/", 302)
303303
]
304-
assert self.client.session["activeorg"] == other_org.slug
305304
else:
306305
assert response.redirect_chain == [
307306
(f"http://{other_org.slug}.testserver/auth/login/{other_org.slug}/", 302)
308307
]
308+
if is_superuser or is_staff:
309+
assert self.client.session["activeorg"] == other_org.slug
310+
else:
309311
assert "activeorg" not in self.client.session
310312

311313
# lastOrganization is set

0 commit comments

Comments
 (0)