Files
zulip/zerver/management/commands/dump_messages.py
Rishi Gupta 2b0a7fd0ba Rename models.get_realm_by_string_id to get_realm.
Finishes the refactoring started in c1bbd8d. The goal of the refactoring is
to change the argument to get_realm from a Realm.domain to a
Realm.string_id. The steps were

* Add a new function, get_realm_by_string_id.

* Change all calls to get_realm to use get_realm_by_string_id instead.

* Remove get_realm.

* (This commit) Rename get_realm_by_string_id to get_realm.

Part of a larger migration to remove the Realm.domain field entirely.
2017-01-04 17:12:23 -08:00

39 lines
1.5 KiB
Python

from __future__ import absolute_import
from __future__ import print_function
from typing import Any
from optparse import make_option
from django.core.management.base import BaseCommand, CommandParser
from zerver.models import get_realm, Message, Realm, Stream, Recipient
import datetime
import time
class Command(BaseCommand):
def add_arguments(self, parser):
# type: (CommandParser) -> None
default_cutoff = time.time() - 60 * 60 * 24 * 30 # 30 days.
parser.add_argument('--realm',
dest='string_id',
type=str,
help='The subdomain/string_id of realm whose public streams you want to dump.')
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, **options):
# type: (*Any, **Any) -> None
realm = get_realm(options["string_id"])
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"])
messages = Message.objects.filter(pub_date__gt=cutoff, recipient__in=recipients)
for message in messages:
print(message.to_dict(False))