Skip to content

Commit

Permalink
import: Move 'build_attachment' to import_util.
Browse files Browse the repository at this point in the history
  • Loading branch information
rheaparekh authored and timabbott committed Aug 7, 2018
1 parent b6ccc0b commit 18a4904
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
19 changes: 19 additions & 0 deletions zerver/data_import/import_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ def build_defaultstream(realm_id: int, stream_id: int,
id=defaultstream_id)
return defaultstream

def build_attachment(realm_id: int, message_id: int, attachment_id: int,
user_id: int, fileinfo: ZerverFieldsT, s3_path: str,
zerver_attachment: List[ZerverFieldsT]) -> None:
"""
This function should be passed a 'fileinfo' dictionary, which contains
information about 'size', 'created' (created time) and ['name'] (filename).
"""
attachment = dict(
owner=user_id,
messages=[message_id],
id=attachment_id,
size=fileinfo['size'],
create_time=fileinfo['created'],
is_realm_public=True,
path_id=s3_path,
realm=realm_id,
file_name=fileinfo['name'])
zerver_attachment.append(attachment)

def process_avatars(avatar_list: List[ZerverFieldsT], avatar_dir: str, realm_id: int,
threads: int, size_url_suffix: str='') -> List[ZerverFieldsT]:
"""
Expand Down
22 changes: 4 additions & 18 deletions zerver/data_import/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
get_user_full_name
from zerver.data_import.import_util import ZerverFieldsT, build_zerver_realm, \
build_avatar, build_subscription, build_recipient, build_usermessages, \
build_defaultstream, process_avatars, process_uploads, process_emojis
build_defaultstream, build_attachment, process_avatars, process_uploads, \
process_emojis
from zerver.lib.parallel import run_parallel
from zerver.lib.upload import random_name, sanitize_name
from zerver.lib.export import MESSAGE_BATCH_CHUNK_SIZE
Expand Down Expand Up @@ -616,8 +617,8 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
uploads_list)

attachment_id = attachment_id_count
build_zerver_attachment(realm_id, message_id, attachment_id, added_users[user],
fileinfo, s3_path, zerver_attachment)
build_attachment(realm_id, message_id, attachment_id, added_users[user],
fileinfo, s3_path, zerver_attachment)
attachment_id_count += 1

# For attachments with link not from slack
Expand Down Expand Up @@ -727,21 +728,6 @@ def build_uploads(user_id: int, realm_id: int, email: str, fileinfo: ZerverField
size=fileinfo['size'])
uploads_list.append(upload)

def build_zerver_attachment(realm_id: int, message_id: int, attachment_id: int,
user_id: int, fileinfo: ZerverFieldsT, s3_path: str,
zerver_attachment: List[ZerverFieldsT]) -> None:
attachment = dict(
owner=user_id,
messages=[message_id],
id=attachment_id,
size=fileinfo['size'],
create_time=fileinfo['created'],
is_realm_public=True, # is always true for stream message
path_id=s3_path,
realm=realm_id,
file_name=fileinfo['name'])
zerver_attachment.append(attachment)

def get_message_sending_user(message: ZerverFieldsT) -> Optional[str]:
if 'user' in message:
return message['user']
Expand Down
2 changes: 1 addition & 1 deletion zerver/tests/test_slack_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def test_convert_slack_workspace_messages(self, mock_get_all_messages: mock.Mock
self.assertEqual(test_reactions, reactions)

@mock.patch("zerver.data_import.slack.process_uploads", return_value = [])
@mock.patch("zerver.data_import.slack.build_zerver_attachment",
@mock.patch("zerver.data_import.slack.build_attachment",
return_value = [])
@mock.patch("zerver.data_import.slack.build_avatar_url")
@mock.patch("zerver.data_import.slack.build_avatar")
Expand Down

0 comments on commit 18a4904

Please sign in to comment.