Files
zulip/zerver/management/commands/dump_messages.py
Tim Abbott 8e7ce7cc79 python: Sort migrations/management command imports with isort.
This is a preparatory commit for using isort for sorting all of our
imports, merging changes to files where we can easily review the
changes as something we're happy with.

These are also files with relatively little active development, which
means we don't expect much merge conflict risk from these changes.
2020-01-14 13:07:47 -08:00

34 lines
1.3 KiB
Python

import datetime
import time
from typing import Any
from django.core.management.base import CommandParser
from django.utils.timezone import utc as timezone_utc
from zerver.lib.management import ZulipBaseCommand
from zerver.models import Message, Recipient, Stream
class Command(ZulipBaseCommand):
help = "Dump messages from public streams of a realm"
def add_arguments(self, parser: CommandParser) -> None:
default_cutoff = time.time() - 60 * 60 * 24 * 30 # 30 days.
self.add_realm_args(parser, True)
parser.add_argument('--since',
dest='since',
type=int,
default=default_cutoff,
help='The time in epoch since from which to start the dump.')
def handle(self, *args: Any, **options: Any) -> None:
realm = self.get_realm(options)
streams = Stream.objects.filter(realm=realm, invite_only=False)
recipients = Recipient.objects.filter(
type=Recipient.STREAM, type_id__in=[stream.id for stream in streams])
cutoff = datetime.datetime.fromtimestamp(options["since"], tz=timezone_utc)
messages = Message.objects.filter(date_sent__gt=cutoff, recipient__in=recipients)
for message in messages:
print(message.to_dict(False))