export-search: Move topic into recipient field.

This commit is contained in:
Alex Vandiver 2023-03-13 19:23:59 +00:00 committed by Tim Abbott
parent 26bc112adc
commit bde978d5b1
1 changed files with 14 additions and 6 deletions

View File

@ -4,7 +4,7 @@ from datetime import datetime, timezone
from email.headerregistry import Address from email.headerregistry import Address
from functools import lru_cache, reduce from functools import lru_cache, reduce
from operator import or_ from operator import or_
from typing import Any from typing import Any, Tuple
from django.core.management.base import CommandError from django.core.management.base import CommandError
from django.db.models import Q from django.db.models import Q
@ -17,6 +17,7 @@ from zerver.models import Message, Recipient, Stream, UserProfile, get_user_by_d
ignore_keys = [ ignore_keys = [
"realm", "realm",
"subject",
"rendered_content_version", "rendered_content_version",
"sending_client", "sending_client",
"search_tsvector", "search_tsvector",
@ -133,16 +134,23 @@ This is most often used for legal compliance.
messages_query = Message.objects.filter(limits, realm=realm).order_by("date_sent") messages_query = Message.objects.filter(limits, realm=realm).order_by("date_sent")
print(f"Exporting {len(messages_query)} messages...") print(f"Exporting {len(messages_query)} messages...")
@lru_cache(maxsize=1000)
def format_sender(full_name: str, delivery_email: str) -> str: def format_sender(full_name: str, delivery_email: str) -> str:
return str(Address(display_name=full_name, addr_spec=delivery_email)) return str(Address(display_name=full_name, addr_spec=delivery_email))
@lru_cache(maxsize=None) def format_full_recipient(recipient_id: int, subject: str) -> str:
def format_recipient(recipient_id: int) -> str: recip_str, has_subject = format_recipient(recipient_id)
if not has_subject:
return recip_str
return f"{recip_str} > {subject}"
@lru_cache(maxsize=1000)
def format_recipient(recipient_id: int) -> Tuple[str, bool]:
recipient = Recipient.objects.get(id=recipient_id) recipient = Recipient.objects.get(id=recipient_id)
if recipient.type == Recipient.STREAM: if recipient.type == Recipient.STREAM:
stream = Stream.objects.values("name").get(id=recipient.type_id) stream = Stream.objects.values("name").get(id=recipient.type_id)
return "#" + stream["name"] return "#" + stream["name"], True
users = ( users = (
UserProfile.objects.filter( UserProfile.objects.filter(
@ -152,12 +160,12 @@ This is most often used for legal compliance.
.values_list("full_name", "delivery_email") .values_list("full_name", "delivery_email")
) )
return ", ".join([format_sender(e[0], e[1]) for e in users]) return ", ".join([format_sender(e[0], e[1]) for e in users]), False
message_dicts = [] message_dicts = []
for message in messages_query: for message in messages_query:
item = model_to_dict(message) item = model_to_dict(message)
item["recipient_name"] = format_recipient(message.recipient_id) item["recipient_name"] = format_full_recipient(message.recipient_id, message.subject)
item["sender_name"] = format_sender( item["sender_name"] = format_sender(
message.sender.full_name, message.sender.delivery_email message.sender.full_name, message.sender.delivery_email
) )