import: Inline save_message_rendered_content().

This function requires a message object, whereas
we want to work with JSON data to avoid necessary
queries when we import data.  Inlining the function
sets us up for a subsequent refactoring.

We change the way we deal with theoretical return
values of `None` to use an assertion; otherwise,
we would have to loosen up a bunch of mypy types
from `str` to `Optional[str]`.  It's not clear `None`
is even possible--we've moved toward throwing exceptions
there instead of silently failing.
This commit is contained in:
Steve Howell 2018-11-02 10:27:30 +00:00 committed by Tim Abbott
parent 6b6001c46c
commit 92a7f04149
1 changed files with 17 additions and 8 deletions

View File

@ -19,7 +19,7 @@ from zerver.lib.bulk_create import bulk_create_users
from zerver.lib.timestamp import datetime_to_timestamp
from zerver.lib.export import DATE_FIELDS, realm_tables, \
Record, TableData, TableName, Field, Path
from zerver.lib.message import save_message_rendered_content
from zerver.lib.message import render_markdown
from zerver.lib.bugdown import version as bugdown_version
from zerver.lib.upload import random_name, sanitize_name, \
S3UploadBackend, LocalUploadBackend, guess_type
@ -219,14 +219,23 @@ def fix_message_rendered_content(data: TableData, field: TableName) -> None:
continue
try:
rendered_content = save_message_rendered_content(message_object, message['content']) # type: Optional[str]
content = message['content']
realm = message_object.get_realm()
rendered_content = render_markdown(
message=message_object,
content=content,
realm=realm,
)
assert(rendered_content is not None)
message_object.rendered_content = rendered_content
message_object.rendered_content_version = bugdown_version
message_object.save_rendered_content()
except Exception:
rendered_content = None
if rendered_content is None:
# This can happen with two possible causes:
# * rendering markdown failing with the exception being caught in bugdown
# * The explicit None clause from an exception escaping
# This generally happens with two possible causes:
# * rendering markdown throwing an uncaught exception
# * rendering markdown failing with the exception being
# caught in bugdown (which then returns None, causing the the
# rendered_content assert above to fire).
logging.warning("Error in markdown rendering for message ID %s; continuing" % (message['id']))
def current_table_ids(data: TableData, table: TableName) -> List[int]: