From 03c856ce10364cd26feed35a27183776a6fac602 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 19 Oct 2013 09:28:59 -0400 Subject: [PATCH] Add create-stream management command. The current version should only be used for testing; for example, if you want to create a bunch of streams for stress testing, you can run this in a loop. (imported from commit ec51a431fb9679fc18379e4c6ecdba66bc75a395) --- zerver/lib/actions.py | 12 ++++++++ zerver/management/commands/create-stream.py | 32 +++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 zerver/management/commands/create-stream.py diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 89faef6e53..d7170ca95e 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -343,6 +343,18 @@ def do_send_messages(messages): # intermingle sending zephyr messages with other messages. return already_sent_ids + [message['message'].id for message in messages] +def do_create_stream(realm, stream_name): + # This is used by a management command now, mostly to facilitate testing. It + # doesn't simulate every single aspect of creating a subscription; for example, + # we don't send Zulips to users to tell them they have been subscribed. + stream = Stream() + stream.realm = realm + stream.name = stream_name + stream.save() + Recipient.objects.create(type_id=stream.id, type=Recipient.STREAM) + subscribers = get_active_user_profiles_by_realm(realm).filter(is_bot=False) + bulk_add_subscriptions([stream], subscribers) + def create_stream_if_needed(realm, stream_name, invite_only=False): (stream, created) = Stream.objects.get_or_create( realm=realm, name__iexact=stream_name, diff --git a/zerver/management/commands/create-stream.py b/zerver/management/commands/create-stream.py new file mode 100644 index 0000000000..e838b78f0a --- /dev/null +++ b/zerver/management/commands/create-stream.py @@ -0,0 +1,32 @@ +from __future__ import absolute_import + +from django.core.management.base import BaseCommand + +from zerver.lib.actions import do_create_stream +from zerver.models import Realm, get_realm + +import sys + +class Command(BaseCommand): + help = """Create a stream, and subscribe all active users (excluding bots). + +This should be used for TESTING only, unless you understand the limitations of +the command. + +Usage: python manage.py create-stream """ + + def handle(self, *args, **options): + if len(args) != 2: + print "Please provide a domain and the stream name." + exit(1) + + domain, stream_name = args + encoding = sys.getfilesystemencoding() + + try: + realm = get_realm(domain) + except Realm.DoesNotExist: + print "Unknown domain %s" % (domain,) + exit(1) + + do_create_stream(realm, stream_name.decode(encoding))