From c4f8abbd30ca7e3290c77b9b286ed361b60234f3 Mon Sep 17 00:00:00 2001 From: Rhea Parekh Date: Fri, 17 Aug 2018 22:26:00 +0530 Subject: [PATCH] import: Build Message with the model class. --- zerver/data_import/gitter.py | 21 +++++++++------------ zerver/data_import/slack.py | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/zerver/data_import/gitter.py b/zerver/data_import/gitter.py index 4ddc572062..b55f92aa17 100644 --- a/zerver/data_import/gitter.py +++ b/zerver/data_import/gitter.py @@ -13,7 +13,7 @@ from django.forms.models import model_to_dict from django.utils.timezone import now as timezone_now from typing import Any, Dict, List, Tuple -from zerver.models import Realm, UserProfile, Recipient +from zerver.models import Realm, UserProfile, Recipient, Message from zerver.lib.export import MESSAGE_BATCH_CHUNK_SIZE from zerver.data_import.import_util import ZerverFieldsT, build_zerver_realm, \ build_avatar, build_subscription, build_recipient, build_usermessages, \ @@ -184,22 +184,19 @@ def convert_gitter_workspace_messages(gitter_data: GitterDataT, output_dir: str, user_short_name_to_full_name) rendered_content = None - zulip_message = dict( - sending_client=1, + zulip_message = Message( rendered_content_version=1, # This is Zulip-specific - has_image=False, subject='imported from gitter', pub_date=float(message_time), id=message_id, - has_attachment=False, - edit_history=None, - sender=user_map[message['fromUser']['id']], content=message['text'], - rendered_content=rendered_content, - recipient=recipient_id, - last_edit_time=None, - has_link=False) - zerver_message.append(zulip_message) + rendered_content=rendered_content) + zulip_message_dict = model_to_dict(zulip_message, + exclude=['recipient', 'sender', 'sending_client']) + zulip_message_dict['recipient'] = recipient_id + zulip_message_dict['sender'] = user_map[message['fromUser']['id']] + zulip_message_dict['sending_client'] = 1 + zerver_message.append(zulip_message_dict) usermessage_id = build_usermessages( zerver_usermessage, usermessage_id, zerver_subscription, diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py index efd01ad380..c9c1edbd80 100755 --- a/zerver/data_import/slack.py +++ b/zerver/data_import/slack.py @@ -17,7 +17,8 @@ from django.utils.timezone import now as timezone_now from django.forms.models import model_to_dict from typing import Any, Dict, List, Optional, Tuple from zerver.forms import check_subdomain_available -from zerver.models import Reaction, RealmEmoji, Realm, UserProfile, Recipient +from zerver.models import Reaction, RealmEmoji, Realm, UserProfile, Recipient, \ + Message from zerver.data_import.slack_message_conversion import convert_to_zulip_markdown, \ get_user_full_name from zerver.data_import.import_util import ZerverFieldsT, build_zerver_realm, \ @@ -624,8 +625,7 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT], content = '[%s](%s)' % (file_name, fileinfo['url_private']) # construct message - zulip_message = dict( - sending_client=1, + zulip_message = Message( rendered_content_version=1, # This is Zulip-specific has_image=has_image, subject='imported from slack', # This is Zulip-specific @@ -633,14 +633,16 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT], id=message_id, has_attachment=has_attachment, # attachment will be posted in the subsequent message; # this is how Slack does it, i.e. less like email - edit_history=None, - sender=added_users[user], # map slack id to zulip id content=content, rendered_content=rendered_content, # slack doesn't cache this - recipient=recipient_id, - last_edit_time=None, has_link=has_link) - zerver_message.append(zulip_message) + + zulip_message_dict = model_to_dict(zulip_message, + exclude=['recipient', 'sender', 'sending_client']) + zulip_message_dict['recipient'] = recipient_id + zulip_message_dict['sender'] = added_users[user] # map slack id to zulip id + zulip_message_dict['sending_client'] = 1 + zerver_message.append(zulip_message_dict) # construct usermessages usermessage_id_count = build_usermessages(