Skip to content

Commit

Permalink
ref: Make react_config available in context (#81654)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
BYK authored Dec 6, 2024
1 parent ed6d3df commit 595e932
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/sentry/middleware/customer_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def _resolve_redirect_url(request, activeorg):

path = reverse(result.url_name or result.func, kwargs=kwargs)
qs = _query_string(request)
redirect_url = f"{redirect_url}{path}{qs}"
return redirect_url
return f"{redirect_url}{path}{qs}"


class CustomerDomainMiddleware:
Expand Down
3 changes: 1 addition & 2 deletions src/sentry/templates/sentry/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{% load sentry_assets %}
{% load sentry_features %}
{% load sentry_helpers %}
{% load sentry_react %}
{% load sentry_status %}
{% get_sentry_version %}
<!DOCTYPE html>
Expand Down Expand Up @@ -41,7 +40,7 @@
{% block initial_data %}
{% script %}
<script>
window.__initialData = {% get_react_config %};
window.__initialData = {{ react_config|to_json }};
</script>
{% endscript %}
{% endblock %}
Expand Down
3 changes: 1 addition & 2 deletions src/sentry/web/frontend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ 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)
return context
return csrf(request)

def respond(
self, template: str, context: dict[str, Any] | None = None, status: int = 200
Expand Down
6 changes: 5 additions & 1 deletion src/sentry/web/frontend/js_sdk_loader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import time
from typing import NotRequired, TypedDict
from typing import Any, NotRequired, TypedDict

from django.conf import settings
from django.http import HttpRequest, HttpResponse
Expand Down Expand Up @@ -55,6 +55,10 @@ class JavaScriptSdkLoader(BaseView):
def determine_active_organization(self, request: HttpRequest, organization_slug=None) -> None:
pass

# Same as above
def get_context_data(self, request: HttpRequest, **kwargs) -> dict[str, Any]:
return {}

def _get_loader_config(
self, key: ProjectKey | None, sdk_version: Version | None
) -> LoaderInternalConfig:
Expand Down
5 changes: 4 additions & 1 deletion src/sentry/web/frontend/react_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from sentry.users.services.user.model import RpcUser
from sentry.utils.http import is_using_customer_domain, query_string
from sentry.web.client_config import get_client_config
from sentry.web.frontend.base import BaseView, ControlSiloOrganizationView
from sentry.web.helpers import render_to_response

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

def handle_react(self, request: Request, **kwargs) -> HttpResponse:
org_context = getattr(self, "active_organization", None)
context = {
"CSRF_COOKIE_NAME": settings.CSRF_COOKIE_NAME,
"meta_tags": [
Expand All @@ -97,7 +99,8 @@ def handle_react(self, request: Request, **kwargs) -> HttpResponse:
# Rendering the layout requires serializing the active organization.
# Since we already have it here from the OrganizationMixin, we can
# save some work and render it faster.
"org_context": getattr(self, "active_organization", None),
"org_context": org_context,
"react_config": get_client_config(request, org_context),
}

# Force a new CSRF token to be generated and set in user's
Expand Down
2 changes: 1 addition & 1 deletion tests/sentry/web/frontend/test_js_sdk_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def test_headers(self, mock_load_version_from_file):
assert "sdk-loader" in resp["Surrogate-Key"]
assert "Content-Encoding" not in resp
assert "Set-Cookie" not in resp
assert "Vary" not in resp
assert "Vary" not in resp, f"Found Vary header: {resp['Vary']}"

def test_absolute_url(self):
assert (
Expand Down
5 changes: 4 additions & 1 deletion tests/sentry/web/frontend/test_react_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,14 @@ def _run_customer_domain_elevated_privileges(self, is_superuser: bool, is_staff:
assert response.redirect_chain == [
(f"http://{other_org.slug}.testserver/issues/", 302)
]
assert self.client.session["activeorg"] == other_org.slug
else:
assert response.redirect_chain == [
(f"http://{other_org.slug}.testserver/auth/login/{other_org.slug}/", 302)
]

if is_superuser or is_staff:
assert self.client.session["activeorg"] == other_org.slug
else:
assert "activeorg" not in self.client.session

# Accessing org without customer domain as superuser and/or staff.
Expand Down
4 changes: 3 additions & 1 deletion tests/sentry/web/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,13 @@ def _run_test_with_privileges(self, is_superuser: bool, is_staff: bool):
assert response.redirect_chain == [
(f"http://{other_org.slug}.testserver/issues/", 302)
]
assert self.client.session["activeorg"] == other_org.slug
else:
assert response.redirect_chain == [
(f"http://{other_org.slug}.testserver/auth/login/{other_org.slug}/", 302)
]
if is_superuser or is_staff:
assert self.client.session["activeorg"] == other_org.slug
else:
assert "activeorg" not in self.client.session

# lastOrganization is set
Expand Down

0 comments on commit 595e932

Please sign in to comment.