python: Skip unnecessary decode before BeautifulSoup parsing.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2020-10-29 17:21:40 -07:00
committed by Tim Abbott
parent 86e8d81c7f
commit 7c4f68d9cf
3 changed files with 8 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
from typing import Mapping
from typing import Mapping, Union
from bs4 import BeautifulSoup
from django.http import HttpRequest
@@ -7,7 +7,7 @@ from django.utils.html import escape
from 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)