2017-11-16 00:43:27 +01:00
|
|
|
import datetime
|
|
|
|
import time
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2017-08-07 17:40:28 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2017-04-15 03:29:56 +02:00
|
|
|
from django.utils.timezone import utc as timezone_utc
|
2013-08-13 23:30:57 +02:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
|
|
|
from zerver.models import Message, Recipient, Stream
|
2013-08-13 23:30:57 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-08-07 17:40:28 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = "Dump messages from public streams of a realm"
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2017-05-07 17:14:53 +02:00
|
|
|
default_cutoff = time.time() - 60 * 60 * 24 * 30 # 30 days.
|
2017-08-07 17:40:28 +02:00
|
|
|
self.add_realm_args(parser, True)
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('--since',
|
|
|
|
dest='since',
|
|
|
|
type=int,
|
|
|
|
default=default_cutoff,
|
|
|
|
help='The time in epoch since from which to start the dump.')
|
2013-08-13 23:30:57 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-08-07 17:40:28 +02:00
|
|
|
realm = self.get_realm(options)
|
2013-08-13 23:30:57 +02:00
|
|
|
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])
|
2017-04-15 03:29:56 +02:00
|
|
|
cutoff = datetime.datetime.fromtimestamp(options["since"], tz=timezone_utc)
|
2019-08-28 02:43:19 +02:00
|
|
|
messages = Message.objects.filter(date_sent__gt=cutoff, recipient__in=recipients)
|
2013-08-13 23:30:57 +02:00
|
|
|
|
|
|
|
for message in messages:
|
2015-11-01 17:11:06 +01:00
|
|
|
print(message.to_dict(False))
|