2013-10-10 21:37:26 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-10-10 21:37:26 +02:00
|
|
|
|
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
|
2017-08-07 17:40:28 +02:00
|
|
|
from zerver.models import Message, Stream, Recipient
|
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2013-08-13 23:30:57 +02:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
|
2017-08-07 17:40:28 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = "Dump messages from public streams of a realm"
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (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
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **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)
|
2013-08-13 23:30:57 +02:00
|
|
|
messages = Message.objects.filter(pub_date__gt=cutoff, recipient__in=recipients)
|
|
|
|
|
|
|
|
for message in messages:
|
2015-11-01 17:11:06 +01:00
|
|
|
print(message.to_dict(False))
|