context_processors: Cache the realm description in default context.

This commit is contained in:
Hemanth V. Alluri 2019-03-18 13:12:13 +05:30 committed by Tim Abbott
parent 6e8e7feffc
commit 261166f96d
4 changed files with 17 additions and 6 deletions

View File

@ -14,10 +14,10 @@ from zproject.backends import (
SOCIAL_AUTH_BACKENDS, SOCIAL_AUTH_BACKENDS,
) )
from zerver.decorator import get_client_name from zerver.decorator import get_client_name
from zerver.lib.bugdown import convert as bugdown_convert
from zerver.lib.send_email import FromAddress from zerver.lib.send_email import FromAddress
from zerver.lib.subdomains import get_subdomain from zerver.lib.subdomains import get_subdomain
from zerver.lib.realm_icon import get_realm_icon_url from zerver.lib.realm_icon import get_realm_icon_url
from zerver.lib.realm_description import get_realm_rendered_description
from version import ZULIP_VERSION, LATEST_RELEASE_VERSION, \ from version import ZULIP_VERSION, LATEST_RELEASE_VERSION, \
LATEST_RELEASE_ANNOUNCEMENT, LATEST_MAJOR_VERSION LATEST_RELEASE_ANNOUNCEMENT, LATEST_MAJOR_VERSION
@ -69,9 +69,7 @@ def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
realm_uri = realm.uri realm_uri = realm.uri
realm_name = realm.name realm_name = realm.name
realm_icon = get_realm_icon_url(realm) realm_icon = get_realm_icon_url(realm)
realm_description_raw = realm.description or "The coolest place in the universe." realm_description = get_realm_rendered_description(realm)
realm_description = bugdown_convert(realm_description_raw, message_realm=realm,
no_previews=True)
realm_invite_required = realm.invite_required realm_invite_required = realm.invite_required
realm_plan_type = realm.plan_type realm_plan_type = realm.plan_type

View File

@ -435,6 +435,7 @@ def flush_realm(sender: Any, **kwargs: Any) -> None:
cache_delete(realm_alert_words_cache_key(realm)) cache_delete(realm_alert_words_cache_key(realm))
cache_delete(realm_alert_words_automaton_cache_key(realm)) cache_delete(realm_alert_words_automaton_cache_key(realm))
cache_delete(active_non_guest_user_ids_cache_key(realm.id)) cache_delete(active_non_guest_user_ids_cache_key(realm.id))
cache_delete(realm_rendered_description_cache_key(realm))
def realm_alert_words_cache_key(realm: 'Realm') -> str: def realm_alert_words_cache_key(realm: 'Realm') -> str:
return "realm_alert_words:%s" % (realm.string_id,) return "realm_alert_words:%s" % (realm.string_id,)
@ -442,6 +443,9 @@ def realm_alert_words_cache_key(realm: 'Realm') -> str:
def realm_alert_words_automaton_cache_key(realm: 'Realm') -> str: def realm_alert_words_automaton_cache_key(realm: 'Realm') -> str:
return "realm_alert_words_automaton:%s" % (realm.string_id,) return "realm_alert_words_automaton:%s" % (realm.string_id,)
def realm_rendered_description_cache_key(realm: 'Realm') -> str:
return "realm_rendered_description:%s" % (realm.string_id,)
# Called by models.py to flush the stream cache whenever we save a stream # Called by models.py to flush the stream cache whenever we save a stream
# object. # object.
def flush_stream(sender: Any, **kwargs: Any) -> None: def flush_stream(sender: Any, **kwargs: Any) -> None:

View File

@ -0,0 +1,9 @@
from zerver.models import Realm
from zerver.lib.cache import cache_with_key, realm_rendered_description_cache_key
from zerver.lib.bugdown import convert as bugdown_convert
@cache_with_key(realm_rendered_description_cache_key, timeout=3600*24*7)
def get_realm_rendered_description(realm: Realm) -> str:
realm_description_raw = realm.description or "The coolest place in the universe."
return bugdown_convert(realm_description_raw, message_realm=realm,
no_previews=True)

View File

@ -228,7 +228,7 @@ class HomeTest(ZulipTestCase):
result = self._get_home_page(stream='Denmark') result = self._get_home_page(stream='Denmark')
self.assert_length(queries, 43) self.assert_length(queries, 43)
self.assert_length(cache_mock.call_args_list, 7) self.assert_length(cache_mock.call_args_list, 8)
html = result.content.decode('utf-8') html = result.content.decode('utf-8')
@ -292,7 +292,7 @@ class HomeTest(ZulipTestCase):
with patch('zerver.lib.cache.cache_set') as cache_mock: with patch('zerver.lib.cache.cache_set') as cache_mock:
result = self._get_home_page() result = self._get_home_page()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assert_length(cache_mock.call_args_list, 6) self.assert_length(cache_mock.call_args_list, 7)
self.assert_length(queries, 40) self.assert_length(queries, 40)
@slow("Creates and subscribes 10 users in a loop. Should use bulk queries.") @slow("Creates and subscribes 10 users in a loop. Should use bulk queries.")