request: Move realm from the request to ZulipRequestNotes.

This commit is contained in:
PIG208 2021-07-09 23:16:26 +08:00 committed by Tim Abbott
parent 5167a93229
commit 8eb2c3ffdb
4 changed files with 23 additions and 10 deletions

View File

@ -17,6 +17,7 @@ from version import (
from zerver.lib.exceptions import InvalidSubdomainError
from zerver.lib.realm_description import get_realm_rendered_description, get_realm_text_description
from zerver.lib.realm_icon import get_realm_icon_url
from zerver.lib.request import get_request_notes
from zerver.lib.send_email import FromAddress
from zerver.lib.subdomains import get_subdomain
from zerver.models import Realm, UserProfile, get_realm
@ -50,19 +51,22 @@ def common_context(user: UserProfile) -> Dict[str, Any]:
def get_realm_from_request(request: HttpRequest) -> Optional[Realm]:
request_notes = get_request_notes(request)
if hasattr(request, "user") and hasattr(request.user, "realm"):
return request.user.realm
if not hasattr(request, "realm"):
# We cache the realm object from this function on the request,
if not request_notes.has_fetched_realm:
# We cache the realm object from this function on the request data,
# so that functions that call get_realm_from_request don't
# need to do duplicate queries on the same realm while
# processing a single request.
subdomain = get_subdomain(request)
request_notes = get_request_notes(request)
try:
request.realm = get_realm(subdomain)
request_notes.realm = get_realm(subdomain)
except Realm.DoesNotExist:
request.realm = None
return request.realm
request_notes.realm = None
request_notes.has_fetched_realm = True
return request_notes.realm
def get_valid_realm_from_request(request: HttpRequest) -> Realm:

View File

@ -577,10 +577,12 @@ class HostDomainMiddleware(MiddlewareMixin):
subdomain = get_subdomain(request)
if subdomain != Realm.SUBDOMAIN_FOR_ROOT_DOMAIN:
request_notes = get_request_notes(request)
try:
request.realm = get_realm(subdomain)
request_notes.realm = get_realm(subdomain)
except Realm.DoesNotExist:
return render(request, "zerver/invalid_realm.html", status=404)
request_notes.has_fetched_realm = True
return None

View File

@ -6,6 +6,7 @@ from django.template import loader
from zerver.lib.avatar import get_gravatar_url
from zerver.lib.exceptions import JsonableError
from zerver.lib.request import get_request_notes
from zerver.lib.response import json_success
from zerver.lib.streams import access_web_public_stream
from zerver.lib.timestamp import datetime_to_timestamp
@ -29,7 +30,9 @@ def archive(request: HttpRequest, stream_id: int, topic_name: str) -> HttpRespon
)
try:
stream = access_web_public_stream(stream_id, request.realm)
realm = get_request_notes(request).realm
assert realm is not None
stream = access_web_public_stream(stream_id, realm)
except JsonableError:
return get_response([], False, "")
@ -75,7 +78,9 @@ def archive(request: HttpRequest, stream_id: int, topic_name: str) -> HttpRespon
def get_web_public_topics_backend(request: HttpRequest, stream_id: int) -> HttpResponse:
try:
stream = access_web_public_stream(stream_id, request.realm)
realm = get_request_notes(request).realm
assert realm is not None
stream = access_web_public_stream(stream_id, realm)
except JsonableError:
return json_success(dict(topics=[]))

View File

@ -10,6 +10,7 @@ from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sentry_sdk.utils import capture_internal_exceptions
from version import ZULIP_VERSION
from zerver.lib.request import get_request_notes
from .config import PRODUCTION, STAGING
@ -50,10 +51,11 @@ def add_context(event: "Event", hint: "Hint") -> Optional["Event"]:
request = get_current_request()
if request:
request_notes = get_request_notes(request)
if hasattr(request, "client"):
event["tags"]["client"] = request.client.name
if hasattr(request, "realm"):
event["tags"].setdefault("realm", request.realm.string_id)
if request_notes.realm is not None:
event["tags"].setdefault("realm", request_notes.realm.string_id)
return event