mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 04:53:36 +00:00
context_processors: Avoid useless duplicate queries for realm object.
We have a few code paths that call get_realm_from_request multiple times on the same request (e.g. the login page), once inside the view function and once inside the common context processor code. This change saves a useless duplicate database query in those code paths.
This commit is contained in:
@@ -38,8 +38,14 @@ def common_context(user: UserProfile) -> Dict[str, Any]:
|
||||
def get_realm_from_request(request: HttpRequest) -> Optional[Realm]:
|
||||
if hasattr(request, "user") and hasattr(request.user, "realm"):
|
||||
return request.user.realm
|
||||
subdomain = get_subdomain(request)
|
||||
return get_realm(subdomain)
|
||||
if not hasattr(request, "realm"):
|
||||
# We cache the realm object from this function on the request,
|
||||
# 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.realm = get_realm(subdomain)
|
||||
return request.realm
|
||||
|
||||
def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
|
||||
"""Context available to all Zulip Jinja2 templates that have a request
|
||||
|
||||
Reference in New Issue
Block a user