retention: Clean up move_messages_to_archive() for more clarity.

This commit is contained in:
Mateusz Mandera 2019-05-28 11:38:53 +02:00 committed by Tim Abbott
parent f0b72a81f3
commit 2ca650be4d
1 changed files with 16 additions and 17 deletions

View File

@ -171,30 +171,29 @@ def move_messages_to_archive(message_ids: List[int]) -> None:
messages = list(Message.objects.filter(id__in=message_ids).values()) messages = list(Message.objects.filter(id__in=message_ids).values())
if not messages: if not messages:
raise Message.DoesNotExist raise Message.DoesNotExist
arc_messages = []
for message in messages: ArchivedMessage.objects.bulk_create([ArchivedMessage(**message) for message in messages])
arc_message = ArchivedMessage(**message)
arc_messages.append(arc_message)
ArchivedMessage.objects.bulk_create(arc_messages)
# Move user_messages to the archive. # Move user_messages to the archive.
user_messages = UserMessage.objects.filter( user_messages = UserMessage.objects.filter(
message_id__in=message_ids).exclude(id__in=ArchivedUserMessage.objects.all()) message_id__in=message_ids).exclude(id__in=ArchivedUserMessage.objects.all()).values()
archiving_messages = [] user_messages_ids = [user_message['id'] for user_message in user_messages]
for user_message in user_messages.values(): ArchivedUserMessage.objects.bulk_create(
archiving_messages.append(ArchivedUserMessage(**user_message)) [ArchivedUserMessage(**user_message) for user_message in user_messages]
ArchivedUserMessage.objects.bulk_create(archiving_messages) )
# Move attachments to archive # Move attachments to archive
attachments = Attachment.objects.filter(messages__id__in=message_ids).exclude( attachments = Attachment.objects.filter(messages__id__in=message_ids).exclude(
id__in=ArchivedAttachment.objects.all()).distinct() id__in=ArchivedAttachment.objects.all()).distinct().values()
archiving_attachments = [] ArchivedAttachment.objects.bulk_create(
for attachment in attachments.values(): [ArchivedAttachment(**attachment) for attachment in attachments]
archiving_attachments.append(ArchivedAttachment(**attachment)) )
ArchivedAttachment.objects.bulk_create(archiving_attachments)
move_attachment_message_to_archive_by_message(message_ids) move_attachment_message_to_archive_by_message(message_ids)
# Remove data from main tables # Remove data from main tables
Message.objects.filter(id__in=message_ids).delete() Message.objects.filter(id__in=message_ids).delete()
user_messages.filter(id__in=ArchivedUserMessage.objects.all(), UserMessage.objects.filter(id__in=user_messages_ids, message_id__isnull=True).delete()
message_id__isnull=True).delete()
archived_attachments = ArchivedAttachment.objects.filter(messages__id__in=message_ids).distinct() archived_attachments = ArchivedAttachment.objects.filter(messages__id__in=message_ids).distinct()
Attachment.objects.filter(messages__isnull=True, id__in=archived_attachments).delete() Attachment.objects.filter(messages__isnull=True, id__in=archived_attachments).delete()