Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
python: Skip unnecessary decode before BeautifulSoup parsing.
Anders Kaseorg7c4f68d9cfee
diff --git a/zerver/lib/cache.py b/zerver/lib/cache.pyindex 894a0a8959d..f017419a561 100644--- a/zerver/lib/cache.py+++ b/zerver/lib/cache.py@@ -635,7 +635,7 @@ def to_dict_cache_key_id(message_id: int) -> str:def to_dict_cache_key(message: 'Message', realm_id: Optional[int]=None) -> str:return to_dict_cache_key_id(message.id)-def open_graph_description_cache_key(content: Any, request: HttpRequest) -> str:+def open_graph_description_cache_key(content: bytes, request: HttpRequest) -> str:return 'open_graph_description_path:{}'.format(make_safe_digest(request.META['PATH_INFO']))def flush_message(sender: Any, **kwargs: Any) -> None:diff --git a/zerver/lib/html_to_text.py b/zerver/lib/html_to_text.pyindex 0cd1e9c8198..369f742db00 100644--- a/zerver/lib/html_to_text.py+++ b/zerver/lib/html_to_text.py@@ -1,4 +1,4 @@-from typing import Mapping+from typing import Mapping, Unionfrom bs4 import BeautifulSoupfrom django.http import HttpRequest@@ -7,7 +7,7 @@ from django.utils.html import escapefrom zerver.lib.cache import cache_with_key, open_graph_description_cache_key-def html_to_text(content: str, tags: Mapping[str, str] = {'p': ' | '}) -> str:+def html_to_text(content: Union[str, bytes], tags: Mapping[str, str] = {'p': ' | '}) -> str:bs = BeautifulSoup(content, features='lxml')# Skip any admonition (warning) blocks, since they're# usually something about users needing to be an@@ -35,5 +35,4 @@ def html_to_text(content: str, tags: Mapping[str, str] = {'p': ' | '}) -> str:@cache_with_key(open_graph_description_cache_key, timeout=3600*24)def get_content_description(content: bytes, request: HttpRequest) -> str:- str_content = content.decode("utf-8")- return html_to_text(str_content)+ return html_to_text(content)diff --git a/zerver/tests/test_middleware.py b/zerver/tests/test_middleware.pyindex a0e5895d157..51eb680adf1 100644--- a/zerver/tests/test_middleware.py+++ b/zerver/tests/test_middleware.py@@ -54,8 +54,7 @@ class OpenGraphTest(ZulipTestCase):status_code: int=200) -> None:response = self.client_get(path)self.assertEqual(response.status_code, status_code)- decoded = response.content.decode('utf-8')- bs = BeautifulSoup(decoded, features='lxml')+ bs = BeautifulSoup(response.content, features='lxml')open_graph_title = bs.select_one('meta[property="og:title"]').get('content')self.assertEqual(open_graph_title, title)@@ -157,8 +156,7 @@ class OpenGraphTest(ZulipTestCase):response = self.client_get('/login/')self.assertEqual(response.status_code, 200)- decoded = response.content.decode('utf-8')- bs = BeautifulSoup(decoded, features='lxml')+ bs = BeautifulSoup(response.content, features='lxml')open_graph_image = bs.select_one('meta[property="og:image"]').get('content')self.assertEqual(open_graph_image, f'{realm.uri}{realm_icon}')@@ -171,8 +169,7 @@ class OpenGraphTest(ZulipTestCase):response = self.client_get('/login/')self.assertEqual(response.status_code, 200)- decoded = response.content.decode('utf-8')- bs = BeautifulSoup(decoded, features='lxml')+ bs = BeautifulSoup(response.content, features='lxml')open_graph_image = bs.select_one('meta[property="og:image"]').get('content')self.assertEqual(open_graph_image, icon_url)@@ -180,8 +177,7 @@ class OpenGraphTest(ZulipTestCase):response = self.client_get('/api/', subdomain='')self.assertEqual(response.status_code, 200)- decoded = response.content.decode('utf-8')- bs = BeautifulSoup(decoded, features='lxml')+ bs = BeautifulSoup(response.content, features='lxml')open_graph_url = bs.select_one('meta[property="og:url"]').get('content')self.assertTrue(open_graph_url.endswith('/api/'))