refactor: Remove global argument.current_realm.

This commit is contained in:
Steve Howell
2018-11-07 14:48:08 +00:00
committed by Tim Abbott
parent e1113c7011
commit fa6f642c9c
3 changed files with 17 additions and 18 deletions

View File

@@ -166,12 +166,11 @@ def rewrite_local_links_to_relative(link: str) -> str:
return link return link
def url_embed_preview_enabled_for_realm(message: Optional[Message]=None) -> bool: def url_embed_preview_enabled_for_realm(message: Optional[Message]=None,
realm: Optional[Realm]=None) -> bool:
if not settings.INLINE_URL_EMBED_PREVIEW: if not settings.INLINE_URL_EMBED_PREVIEW:
return False return False
realm = arguments.current_realm
if realm is None: if realm is None:
if message is not None: if message is not None:
realm = message.get_realm() realm = message.get_realm()
@@ -184,12 +183,11 @@ def url_embed_preview_enabled_for_realm(message: Optional[Message]=None) -> bool
return realm.inline_url_embed_preview return realm.inline_url_embed_preview
def image_preview_enabled_for_realm(message: Optional[Message]=None) -> bool: def image_preview_enabled_for_realm(message: Optional[Message]=None,
realm: Optional[Realm]=None) -> bool:
if not settings.INLINE_IMAGE_PREVIEW: if not settings.INLINE_IMAGE_PREVIEW:
return False return False
realm = arguments.current_realm
if realm is None: if realm is None:
if message is not None: if message is not None:
realm = message.get_realm() realm = message.get_realm()
@@ -564,8 +562,14 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
return url return url
def image_preview_enabled(self) -> bool:
return image_preview_enabled_for_realm(
self.markdown.zulip_message,
self.markdown.zulip_realm,
)
def is_image(self, url: str) -> bool: def is_image(self, url: str) -> bool:
if not image_preview_enabled_for_realm(self.markdown.zulip_message): if not self.image_preview_enabled():
return False return False
parsed_url = urllib.parse.urlparse(url) parsed_url = urllib.parse.urlparse(url)
# List from http://support.google.com/chromeos/bin/answer.py?hl=en&answer=183093 # List from http://support.google.com/chromeos/bin/answer.py?hl=en&answer=183093
@@ -620,7 +624,7 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
return None return None
def youtube_id(self, url: str) -> Optional[str]: def youtube_id(self, url: str) -> Optional[str]:
if not image_preview_enabled_for_realm(): if not self.image_preview_enabled():
return None return None
# Youtube video id extraction regular expression from http://pastebin.com/KyKAFv1s # Youtube video id extraction regular expression from http://pastebin.com/KyKAFv1s
# If it matches, match.group(2) is the video id. # If it matches, match.group(2) is the video id.
@@ -640,7 +644,7 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
return None return None
def vimeo_id(self, url: str) -> Optional[str]: def vimeo_id(self, url: str) -> Optional[str]:
if not image_preview_enabled_for_realm(): if not self.image_preview_enabled():
return None return None
#(http|https)?:\/\/(www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|)(\d+)(?:|\/\?) #(http|https)?:\/\/(www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|)(\d+)(?:|\/\?)
# If it matches, match.group('id') is the video id. # If it matches, match.group('id') is the video id.
@@ -971,7 +975,8 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
if arguments.db_data and arguments.db_data['sent_by_bot']: if arguments.db_data and arguments.db_data['sent_by_bot']:
continue continue
if not url_embed_preview_enabled_for_realm(self.markdown.zulip_message): if not url_embed_preview_enabled_for_realm(self.markdown.zulip_message,
self.markdown.zulip_realm):
continue continue
try: try:
@@ -2022,7 +2027,7 @@ def do_convert(content: str,
# Filters such as UserMentionPattern need a message. # Filters such as UserMentionPattern need a message.
_md_engine.zulip_message = message _md_engine.zulip_message = message
arguments.current_realm = message_realm _md_engine.zulip_realm = message_realm
# Pre-fetch data from the DB that is used in the bugdown thread # Pre-fetch data from the DB that is used in the bugdown thread
if message is not None: if message is not None:
@@ -2088,7 +2093,7 @@ def do_convert(content: str,
raise BugdownRenderingException() raise BugdownRenderingException()
finally: finally:
_md_engine.zulip_message = None _md_engine.zulip_message = None
arguments.current_realm = None _md_engine.zulip_realm = None
arguments.db_data = None arguments.db_data = None
bugdown_time_start = 0.0 bugdown_time_start = 0.0

View File

@@ -1,9 +1,5 @@
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from zerver.models import Message, Realm
current_realm = None # type: Optional[Realm]
# We avoid doing DB queries in our markdown thread to avoid the overhead of # We avoid doing DB queries in our markdown thread to avoid the overhead of
# opening a new DB connection. These connections tend to live longer than the # opening a new DB connection. These connections tend to live longer than the
# threads themselves, as well. # threads themselves, as well.

View File

@@ -448,7 +448,6 @@ class BugdownTest(ZulipTestCase):
sender_user_profile = self.example_user('othello') sender_user_profile = self.example_user('othello')
message = Message(sender=sender_user_profile, sending_client=get_client("test")) message = Message(sender=sender_user_profile, sending_client=get_client("test"))
realm = message.get_realm() realm = message.get_realm()
arguments.current_realm = realm
ret = bugdown.image_preview_enabled_for_realm() ret = bugdown.image_preview_enabled_for_realm()
self.assertEqual(ret, realm.inline_image_preview) self.assertEqual(ret, realm.inline_image_preview)
@@ -461,7 +460,6 @@ class BugdownTest(ZulipTestCase):
sender_user_profile = self.example_user('othello') sender_user_profile = self.example_user('othello')
message = copy.deepcopy(Message(sender=sender_user_profile, sending_client=get_client("test"))) message = copy.deepcopy(Message(sender=sender_user_profile, sending_client=get_client("test")))
realm = message.get_realm() realm = message.get_realm()
arguments.current_realm = realm
ret = bugdown.url_embed_preview_enabled_for_realm() ret = bugdown.url_embed_preview_enabled_for_realm()
self.assertEqual(ret, False) self.assertEqual(ret, False)