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
|
|
|
|
|
2013-08-13 23:30:57 +02:00
|
|
|
from optparse import make_option
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2017-04-15 03:29:56 +02:00
|
|
|
from django.utils.timezone import utc as timezone_utc
|
2017-01-04 05:30:48 +01:00
|
|
|
from zerver.models import get_realm, Message, Realm, Stream, Recipient
|
2013-08-13 23:30:57 +02:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (CommandParser) -> None
|
|
|
|
default_cutoff = time.time() - 60 * 60 * 24 * 30 # 30 days.
|
2016-11-16 21:16:02 +01:00
|
|
|
parser.add_argument('--realm',
|
|
|
|
dest='string_id',
|
2016-11-03 10:22:19 +01:00
|
|
|
type=str,
|
2016-11-16 21:16:02 +01:00
|
|
|
help='The subdomain/string_id of realm whose public streams you want to dump.')
|
2013-08-13 23:30:57 +02:00
|
|
|
|
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-01-04 05:30:48 +01:00
|
|
|
realm = get_realm(options["string_id"])
|
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))
|