2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2016-01-24 03:56:05 +01:00
|
|
|
from __future__ import division
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2017-02-25 21:02:13 +01:00
|
|
|
from django.utils import timezone
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2016-12-09 20:18:52 +01:00
|
|
|
from zerver.models import Message, UserProfile, Stream, Recipient, UserPresence, \
|
2016-11-16 06:04:43 +01:00
|
|
|
Subscription, get_huddle, Realm, UserMessage, RealmAlias, \
|
|
|
|
clear_database, get_client, get_user_profile_by_id, \
|
|
|
|
email_to_username
|
2017-01-22 07:07:29 +01:00
|
|
|
from zerver.lib.actions import STREAM_ASSIGNMENT_COLORS, do_send_messages, \
|
2016-11-16 06:04:43 +01:00
|
|
|
do_change_is_admin
|
2012-09-21 16:10:36 +02:00
|
|
|
from django.conf import settings
|
2017-03-13 20:23:47 +01:00
|
|
|
from zerver.lib.bulk_create import bulk_create_clients, \
|
|
|
|
bulk_create_streams, bulk_create_users, bulk_create_huddles
|
2017-01-04 05:30:48 +01:00
|
|
|
from zerver.models import DefaultStream, get_stream, get_realm
|
2012-08-28 18:45:10 +02:00
|
|
|
|
|
|
|
import random
|
2012-10-22 19:47:27 +02:00
|
|
|
import os
|
2012-09-04 20:26:45 +02:00
|
|
|
from optparse import make_option
|
2015-11-01 17:15:05 +01:00
|
|
|
from six.moves import range
|
2016-12-08 05:06:51 +01:00
|
|
|
from typing import Any, Callable, Dict, List, Iterable, Mapping, Sequence, Set, Tuple, Text
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-11-08 21:49:04 +01:00
|
|
|
settings.TORNADO_SERVER = None
|
2017-02-27 05:46:17 +01:00
|
|
|
# Disable using memcached caches to avoid 'unsupported pickle
|
|
|
|
# protocol' errors if `populate_db` is run with a different Python
|
|
|
|
# from `run-dev.py`.
|
|
|
|
settings.CACHES['default'] = {
|
|
|
|
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
|
|
|
|
}
|
2012-10-12 21:27:19 +02:00
|
|
|
|
2016-12-15 17:25:59 +01:00
|
|
|
def create_users(realm, name_list, bot_type=None):
|
|
|
|
# type: (Realm, Iterable[Tuple[Text, Text]], int) -> None
|
2016-12-08 05:06:51 +01:00
|
|
|
user_set = set() # type: Set[Tuple[Text, Text, Text, bool]]
|
2012-10-23 23:29:56 +02:00
|
|
|
for full_name, email in name_list:
|
2013-07-18 18:48:56 +02:00
|
|
|
short_name = email_to_username(email)
|
2012-10-23 23:29:56 +02:00
|
|
|
user_set.add((email, full_name, short_name, True))
|
2016-08-10 03:05:26 +02:00
|
|
|
tos_version = settings.TOS_VERSION if bot_type is None else None
|
2016-12-15 17:25:59 +01:00
|
|
|
bulk_create_users(realm, user_set, bot_type=bot_type, tos_version=tos_version)
|
2012-10-23 23:29:56 +02:00
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
class Command(BaseCommand):
|
2012-08-28 18:45:10 +02:00
|
|
|
help = "Populate a test database"
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (CommandParser) -> None
|
|
|
|
parser.add_argument('-n', '--num-messages',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='num_messages',
|
|
|
|
type=int,
|
|
|
|
default=600,
|
|
|
|
help='The number of messages to create.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--extra-users',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='extra_users',
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
help='The number of extra users to create')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2017-02-11 02:32:54 +01:00
|
|
|
parser.add_argument('--extra-bots',
|
|
|
|
dest='extra_bots',
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
help='The number of extra bots to create')
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('--huddles',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='num_huddles',
|
|
|
|
type=int,
|
|
|
|
default=3,
|
|
|
|
help='The number of huddles to create.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--personals',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='num_personals',
|
|
|
|
type=int,
|
|
|
|
default=6,
|
|
|
|
help='The number of personal pairs to create.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--threads',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='threads',
|
|
|
|
type=int,
|
|
|
|
default=10,
|
|
|
|
help='The number of threads to use.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--percent-huddles',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='percent_huddles',
|
|
|
|
type=float,
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be huddles.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--percent-personals',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='percent_personals',
|
|
|
|
type=float,
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be personals.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--stickyness',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='stickyness',
|
|
|
|
type=float,
|
|
|
|
default=20,
|
|
|
|
help='The percent of messages to repeat recent folks.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--nodelete',
|
2016-12-03 00:04:17 +01:00
|
|
|
action="store_false",
|
|
|
|
default=True,
|
|
|
|
dest='delete',
|
|
|
|
help='Whether to delete all the existing messages.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--test-suite',
|
2016-12-03 00:04:17 +01:00
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
help='Whether to delete all the existing messages.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
def handle(self, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (**Any) -> None
|
2012-09-05 18:38:35 +02:00
|
|
|
if options["percent_huddles"] + options["percent_personals"] > 100:
|
|
|
|
self.stderr.write("Error! More than 100% of messages allocated.\n")
|
|
|
|
return
|
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
2012-10-29 19:43:00 +01:00
|
|
|
# Start by clearing all the data in our database
|
|
|
|
clear_database()
|
2012-09-05 21:49:56 +02:00
|
|
|
|
2012-10-23 23:29:56 +02:00
|
|
|
# Create our two default realms
|
2016-09-28 00:03:13 +02:00
|
|
|
# Could in theory be done via zerver.lib.actions.do_create_realm, but
|
|
|
|
# welcome-bot (needed for do_create_realm) hasn't been created yet
|
2016-10-28 21:43:47 +02:00
|
|
|
zulip_realm = Realm.objects.create(
|
|
|
|
string_id="zulip", name="Zulip Dev", restricted_to_domain=True,
|
|
|
|
invite_required=False, org_type=Realm.CORPORATE, domain="zulip.com")
|
2016-09-28 00:03:13 +02:00
|
|
|
RealmAlias.objects.create(realm=zulip_realm, domain="zulip.com")
|
2015-08-20 03:54:07 +02:00
|
|
|
if options["test_suite"]:
|
2016-10-28 21:43:47 +02:00
|
|
|
mit_realm = Realm.objects.create(
|
2017-03-04 09:19:37 +01:00
|
|
|
string_id="zephyr", name="MIT", restricted_to_domain=True,
|
2016-10-28 21:43:47 +02:00
|
|
|
invite_required=False, org_type=Realm.CORPORATE, domain="mit.edu")
|
2016-09-28 00:03:13 +02:00
|
|
|
RealmAlias.objects.create(realm=mit_realm, domain="mit.edu")
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Create test Users (UserProfiles are automatically created,
|
|
|
|
# as are subscriptions to the ability to receive personals).
|
2016-11-18 21:46:47 +01:00
|
|
|
names = [
|
|
|
|
("Zoe", "ZOE@zulip.com"),
|
|
|
|
("Othello, the Moor of Venice", "othello@zulip.com"),
|
|
|
|
("Iago", "iago@zulip.com"),
|
|
|
|
("Prospero from The Tempest", "prospero@zulip.com"),
|
|
|
|
("Cordelia Lear", "cordelia@zulip.com"),
|
|
|
|
("King Hamlet", "hamlet@zulip.com"),
|
|
|
|
("aaron", "AARON@zulip.com"),
|
|
|
|
]
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["extra_users"]):
|
2013-07-24 20:56:42 +02:00
|
|
|
names.append(('Extra User %d' % (i,), 'extrauser%d@zulip.com' % (i,)))
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(zulip_realm, names)
|
2015-08-20 03:53:48 +02:00
|
|
|
iago = UserProfile.objects.get(email="iago@zulip.com")
|
|
|
|
do_change_is_admin(iago, True)
|
2012-10-10 23:17:43 +02:00
|
|
|
# Create public streams.
|
2012-10-23 23:29:56 +02:00
|
|
|
stream_list = ["Verona", "Denmark", "Scotland", "Venice", "Rome"]
|
2016-12-08 00:02:21 +01:00
|
|
|
stream_dict = {
|
|
|
|
"Verona": {"description": "A city in Italy", "invite_only": False},
|
|
|
|
"Denmark": {"description": "A Scandinavian country", "invite_only": False},
|
|
|
|
"Scotland": {"description": "Located in the United Kingdom", "invite_only": False},
|
|
|
|
"Venice": {"description": "A northeastern Italian city", "invite_only": False},
|
|
|
|
"Rome": {"description": "Yet another Italian city", "invite_only": False}
|
|
|
|
} # type: Dict[Text, Dict[Text, Any]]
|
|
|
|
|
2016-12-15 17:07:45 +01:00
|
|
|
bulk_create_streams(zulip_realm, stream_dict)
|
2016-08-03 23:37:12 +02:00
|
|
|
recipient_streams = [Stream.objects.get(name=name, realm=zulip_realm).id
|
|
|
|
for name in stream_list] # type: List[int]
|
2016-04-13 04:49:13 +02:00
|
|
|
# Create subscriptions to streams. The following
|
|
|
|
# algorithm will give each of the users a different but
|
|
|
|
# deterministic subset of the streams (given a fixed list
|
|
|
|
# of users).
|
2016-06-27 17:09:43 +02:00
|
|
|
subscriptions_to_add = [] # type: List[Subscription]
|
2016-06-27 16:42:00 +02:00
|
|
|
profiles = UserProfile.objects.select_related().all().order_by("email") # type: Sequence[UserProfile]
|
2012-09-10 20:38:29 +02:00
|
|
|
for i, profile in enumerate(profiles):
|
2012-10-10 23:17:43 +02:00
|
|
|
# Subscribe to some streams.
|
2012-10-23 23:29:56 +02:00
|
|
|
for type_id in recipient_streams[:int(len(recipient_streams) *
|
|
|
|
float(i)/len(profiles)) + 1]:
|
|
|
|
r = Recipient.objects.get(type=Recipient.STREAM, type_id=type_id)
|
2016-08-03 23:37:12 +02:00
|
|
|
s = Subscription(
|
|
|
|
recipient=r,
|
|
|
|
user_profile=profile,
|
|
|
|
color=STREAM_ASSIGNMENT_COLORS[i % len(STREAM_ASSIGNMENT_COLORS)])
|
|
|
|
|
2012-10-23 23:29:56 +02:00
|
|
|
subscriptions_to_add.append(s)
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_add)
|
2012-09-10 20:38:29 +02:00
|
|
|
else:
|
2017-01-04 05:30:48 +01:00
|
|
|
zulip_realm = get_realm("zulip")
|
2012-10-10 23:17:43 +02:00
|
|
|
recipient_streams = [klass.type_id for klass in
|
2012-10-10 22:57:21 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.STREAM)]
|
2012-09-10 20:38:29 +02:00
|
|
|
|
|
|
|
# Extract a list of all users
|
2016-12-09 20:18:52 +01:00
|
|
|
user_profiles = list(UserProfile.objects.all()) # type: List[UserProfile]
|
|
|
|
|
|
|
|
if not options["test_suite"]:
|
|
|
|
# Populate users with some bar data
|
|
|
|
for user in user_profiles:
|
2017-02-11 08:50:09 +01:00
|
|
|
status = UserPresence.ACTIVE # type: int
|
2017-02-25 21:02:13 +01:00
|
|
|
date = timezone.now()
|
2016-12-09 20:18:52 +01:00
|
|
|
client = get_client("website")
|
2017-02-11 08:50:09 +01:00
|
|
|
if user.full_name[0] <= 'H':
|
|
|
|
client = get_client("ZulipAndroid")
|
2016-12-09 20:18:52 +01:00
|
|
|
UserPresence.objects.get_or_create(user_profile=user, client=client, timestamp=date, status=status)
|
|
|
|
|
|
|
|
user_profiles_ids = [user_profile.id for user_profile in user_profiles]
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-05 18:38:35 +02:00
|
|
|
# Create several initial huddles
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["num_huddles"]):
|
2016-12-09 20:18:52 +01:00
|
|
|
get_huddle(random.sample(user_profiles_ids, random.randint(3, 4)))
|
2012-09-05 18:38:35 +02:00
|
|
|
|
|
|
|
# Create several initial pairs for personals
|
2016-12-09 20:18:52 +01:00
|
|
|
personals_pairs = [random.sample(user_profiles_ids, 2)
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["num_personals"])]
|
2012-09-14 17:52:38 +02:00
|
|
|
|
2012-09-14 22:52:09 +02:00
|
|
|
threads = options["threads"]
|
2016-06-27 16:42:00 +02:00
|
|
|
jobs = [] # type: List[Tuple[int, List[List[int]], Dict[str, Any], Callable[[str], int]]]
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(threads):
|
2016-01-24 03:56:05 +01:00
|
|
|
count = options["num_messages"] // threads
|
2012-10-03 21:07:40 +02:00
|
|
|
if i < options["num_messages"] % threads:
|
2012-09-14 18:31:11 +02:00
|
|
|
count += 1
|
|
|
|
jobs.append((count, personals_pairs, options, self.stdout.write))
|
2013-11-01 20:51:09 +01:00
|
|
|
|
|
|
|
for job in jobs:
|
|
|
|
send_messages(job)
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
2012-11-28 23:09:11 +01:00
|
|
|
# Create the "website" and "API" clients; if we don't, the
|
2013-07-29 23:03:31 +02:00
|
|
|
# default values in zerver/decorators.py will not work
|
2012-11-28 23:09:11 +01:00
|
|
|
# with the Django test suite.
|
|
|
|
get_client("website")
|
|
|
|
get_client("API")
|
|
|
|
|
2015-08-19 05:43:13 +02:00
|
|
|
if options["test_suite"]:
|
|
|
|
# Create test users; the MIT ones are needed to test
|
|
|
|
# the Zephyr mirroring codepaths.
|
|
|
|
testsuite_mit_users = [
|
|
|
|
("Fred Sipb (MIT)", "sipbtest@mit.edu"),
|
|
|
|
("Athena Consulting Exchange User (MIT)", "starnine@mit.edu"),
|
|
|
|
("Esp Classroom (MIT)", "espuser@mit.edu"),
|
2017-01-24 06:34:26 +01:00
|
|
|
]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(mit_realm, testsuite_mit_users)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
|
|
|
# These bots are directly referenced from code and thus
|
|
|
|
# are needed for the test suite.
|
2013-11-14 16:59:10 +01:00
|
|
|
all_realm_bots = [(bot['name'], bot['email_template'] % (settings.INTERNAL_BOT_DOMAIN,))
|
|
|
|
for bot in settings.INTERNAL_BOTS]
|
2013-10-31 18:33:19 +01:00
|
|
|
zulip_realm_bots = [
|
2013-07-24 20:23:35 +02:00
|
|
|
("Zulip New User Bot", "new-user-bot@zulip.com"),
|
2013-07-24 20:23:35 +02:00
|
|
|
("Zulip Error Bot", "error-bot@zulip.com"),
|
2016-05-22 18:46:49 +02:00
|
|
|
("Zulip Default Bot", "default-bot@zulip.com"),
|
2017-01-24 06:34:26 +01:00
|
|
|
]
|
2017-02-11 02:32:54 +01:00
|
|
|
for i in range(options["extra_bots"]):
|
|
|
|
zulip_realm_bots.append(('Extra Bot %d' % (i,), 'extrabot%d@zulip.com' % (i,)))
|
2013-10-31 18:33:19 +01:00
|
|
|
zulip_realm_bots.extend(all_realm_bots)
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(zulip_realm, zulip_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2016-05-19 23:11:13 +02:00
|
|
|
zulip_webhook_bots = [
|
|
|
|
("Zulip Webhook Bot", "webhook-bot@zulip.com"),
|
|
|
|
]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(zulip_realm, zulip_webhook_bots, bot_type=UserProfile.INCOMING_WEBHOOK_BOT)
|
2016-05-19 23:11:13 +02:00
|
|
|
|
2017-01-27 00:06:55 +01:00
|
|
|
create_simple_community_realm()
|
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
if not options["test_suite"]:
|
2016-02-08 03:59:38 +01:00
|
|
|
# Initialize the email gateway bot as an API Super User
|
|
|
|
email_gateway_bot = UserProfile.objects.get(email__iexact=settings.EMAIL_GATEWAY_BOT)
|
|
|
|
email_gateway_bot.is_api_super_user = True
|
|
|
|
email_gateway_bot.save()
|
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
# To keep the messages.json fixtures file for the test
|
|
|
|
# suite fast, don't add these users and subscriptions
|
|
|
|
# when running populate_db for the test suite
|
|
|
|
|
2016-12-08 00:02:21 +01:00
|
|
|
zulip_stream_dict = {
|
|
|
|
"devel": {"description": "For developing", "invite_only": False},
|
|
|
|
"all": {"description": "For everything", "invite_only": False},
|
|
|
|
"announce": {"description": "For announcements", "invite_only": False},
|
|
|
|
"design": {"description": "For design", "invite_only": False},
|
|
|
|
"support": {"description": "For support", "invite_only": False},
|
|
|
|
"social": {"description": "For socializing", "invite_only": False},
|
|
|
|
"test": {"description": "For testing", "invite_only": False},
|
|
|
|
"errors": {"description": "For errors", "invite_only": False},
|
|
|
|
"sales": {"description": "For sales discussion", "invite_only": False}
|
|
|
|
} # type: Dict[Text, Dict[Text, Any]]
|
2016-12-15 17:07:45 +01:00
|
|
|
bulk_create_streams(zulip_realm, zulip_stream_dict)
|
2016-12-16 07:40:15 +01:00
|
|
|
# Now that we've created the notifications stream, configure it properly.
|
|
|
|
zulip_realm.notifications_stream = get_stream("announce", zulip_realm)
|
|
|
|
zulip_realm.save(update_fields=['notifications_stream'])
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2013-07-08 23:24:26 +02:00
|
|
|
# Add a few default streams
|
2016-12-08 00:02:21 +01:00
|
|
|
for default_stream_name in ["design", "devel", "social", "support"]:
|
|
|
|
DefaultStream.objects.create(realm=zulip_realm,
|
|
|
|
stream=get_stream(default_stream_name, zulip_realm))
|
2013-07-08 23:24:26 +02:00
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
# Now subscribe everyone to these streams
|
|
|
|
subscriptions_to_add = []
|
2013-08-06 17:40:44 +02:00
|
|
|
profiles = UserProfile.objects.select_related().filter(realm=zulip_realm)
|
2016-12-08 00:02:21 +01:00
|
|
|
for i, stream_name in enumerate(zulip_stream_dict):
|
2016-08-03 23:37:12 +02:00
|
|
|
stream = Stream.objects.get(name=stream_name, realm=zulip_realm)
|
2013-01-08 21:59:52 +01:00
|
|
|
recipient = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id)
|
|
|
|
for profile in profiles:
|
|
|
|
# Subscribe to some streams.
|
2016-08-03 23:37:12 +02:00
|
|
|
s = Subscription(
|
|
|
|
recipient=recipient,
|
|
|
|
user_profile=profile,
|
|
|
|
color=STREAM_ASSIGNMENT_COLORS[i % len(STREAM_ASSIGNMENT_COLORS)])
|
2013-01-08 21:59:52 +01:00
|
|
|
subscriptions_to_add.append(s)
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_add)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
|
|
|
# These bots are not needed by the test suite
|
2013-08-06 22:19:25 +02:00
|
|
|
internal_zulip_users_nosubs = [
|
2013-07-24 20:19:19 +02:00
|
|
|
("Zulip Commit Bot", "commit-bot@zulip.com"),
|
2013-07-24 20:05:33 +02:00
|
|
|
("Zulip Trac Bot", "trac-bot@zulip.com"),
|
2013-07-24 20:03:00 +02:00
|
|
|
("Zulip Nagios Bot", "nagios-bot@zulip.com"),
|
2017-01-24 06:34:26 +01:00
|
|
|
]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(zulip_realm, internal_zulip_users_nosubs, bot_type=UserProfile.DEFAULT_BOT)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2016-11-02 23:09:49 +01:00
|
|
|
zulip_cross_realm_bots = [
|
|
|
|
("Zulip Feedback Bot", "feedback@zulip.com"),
|
2017-01-24 06:34:26 +01:00
|
|
|
]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(zulip_realm, zulip_cross_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
|
2016-11-02 23:09:49 +01:00
|
|
|
|
2013-03-07 17:12:35 +01:00
|
|
|
# Mark all messages as read
|
2013-11-01 20:51:09 +01:00
|
|
|
UserMessage.objects.all().update(flags=UserMessage.flags.read)
|
2013-03-07 17:12:35 +01:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
self.stdout.write("Successfully populated test database.\n")
|
2012-10-02 22:58:13 +02:00
|
|
|
|
2016-01-23 01:55:47 +01:00
|
|
|
recipient_hash = {} # type: Dict[int, Recipient]
|
2012-09-14 22:43:54 +02:00
|
|
|
def get_recipient_by_id(rid):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (int) -> Recipient
|
2012-09-14 22:43:54 +02:00
|
|
|
if rid in recipient_hash:
|
|
|
|
return recipient_hash[rid]
|
|
|
|
return Recipient.objects.get(id=rid)
|
|
|
|
|
2012-10-03 21:07:40 +02:00
|
|
|
# Create some test messages, including:
|
2012-10-10 23:17:43 +02:00
|
|
|
# - multiple streams
|
2012-10-11 00:01:39 +02:00
|
|
|
# - multiple subjects per stream
|
2012-09-14 18:31:11 +02:00
|
|
|
# - multiple huddles
|
|
|
|
# - multiple personals converastions
|
2012-10-11 00:01:39 +02:00
|
|
|
# - multiple messages per subject
|
2012-09-14 18:31:11 +02:00
|
|
|
# - both single and multi-line content
|
2012-10-03 21:07:40 +02:00
|
|
|
def send_messages(data):
|
2016-06-27 16:50:44 +02:00
|
|
|
# type: (Tuple[int, Sequence[Sequence[int]], Mapping[str, Any], Callable[[str], Any]]) -> int
|
2012-10-03 21:05:48 +02:00
|
|
|
(tot_messages, personals_pairs, options, output) = data
|
2012-10-22 19:47:27 +02:00
|
|
|
random.seed(os.getpid())
|
2015-10-14 22:31:08 +02:00
|
|
|
texts = open("zilencer/management/commands/test_messages.txt", "r").readlines()
|
2012-09-27 21:00:10 +02:00
|
|
|
offset = random.randint(0, len(texts))
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-10-10 23:17:43 +02:00
|
|
|
recipient_streams = [klass.id for klass in
|
2016-06-27 16:42:00 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.STREAM)] # type: List[int]
|
|
|
|
recipient_huddles = [h.id for h in Recipient.objects.filter(type=Recipient.HUDDLE)] # type: List[int]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
huddle_members = {} # type: Dict[int, List[int]]
|
2012-09-14 18:31:11 +02:00
|
|
|
for h in recipient_huddles:
|
2012-10-22 20:15:25 +02:00
|
|
|
huddle_members[h] = [s.user_profile.id for s in
|
2012-09-14 22:22:23 +02:00
|
|
|
Subscription.objects.filter(recipient_id=h)]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
num_messages = 0
|
2012-09-14 18:31:11 +02:00
|
|
|
random_max = 1000000
|
2016-01-26 02:19:31 +01:00
|
|
|
recipients = {} # type: Dict[int, Tuple[int, int, Dict[str, Any]]]
|
2012-10-03 21:05:48 +02:00
|
|
|
while num_messages < tot_messages:
|
2016-01-26 02:19:31 +01:00
|
|
|
saved_data = {} # type: Dict[str, Any]
|
2012-10-03 21:05:48 +02:00
|
|
|
message = Message()
|
2012-10-19 21:30:42 +02:00
|
|
|
message.sending_client = get_client('populate_db')
|
2012-09-14 18:31:11 +02:00
|
|
|
length = random.randint(1, 5)
|
2012-09-26 20:41:54 +02:00
|
|
|
lines = (t.strip() for t in texts[offset: offset + length])
|
2012-10-03 21:05:48 +02:00
|
|
|
message.content = '\n'.join(lines)
|
2012-09-14 18:31:11 +02:00
|
|
|
offset += length
|
|
|
|
offset = offset % len(texts)
|
|
|
|
|
|
|
|
randkey = random.randint(1, random_max)
|
2012-10-03 21:05:48 +02:00
|
|
|
if (num_messages > 0 and
|
2016-12-03 18:19:09 +01:00
|
|
|
random.randint(1, random_max) * 100. / random_max < options["stickyness"]):
|
2012-09-14 18:31:11 +02:00
|
|
|
# Use an old recipient
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type, recipient_id, saved_data = recipients[num_messages - 1]
|
|
|
|
if message_type == Recipient.PERSONAL:
|
2016-01-27 22:14:21 +01:00
|
|
|
personals_pair = saved_data['personals_pair']
|
2012-09-14 18:31:11 +02:00
|
|
|
random.shuffle(personals_pair)
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message_type == Recipient.STREAM:
|
2016-01-27 22:14:21 +01:00
|
|
|
message.subject = saved_data['subject']
|
2012-10-03 21:05:48 +02:00
|
|
|
message.recipient = get_recipient_by_id(recipient_id)
|
|
|
|
elif message_type == Recipient.HUDDLE:
|
|
|
|
message.recipient = get_recipient_by_id(recipient_id)
|
2012-09-14 18:31:11 +02:00
|
|
|
elif (randkey <= random_max * options["percent_huddles"] / 100.):
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type = Recipient.HUDDLE
|
|
|
|
message.recipient = get_recipient_by_id(random.choice(recipient_huddles))
|
2012-09-14 18:31:11 +02:00
|
|
|
elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.):
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type = Recipient.PERSONAL
|
2012-09-14 18:31:11 +02:00
|
|
|
personals_pair = random.choice(personals_pairs)
|
|
|
|
random.shuffle(personals_pair)
|
|
|
|
elif (randkey <= random_max * 1.0):
|
2012-10-10 22:57:21 +02:00
|
|
|
message_type = Recipient.STREAM
|
2012-10-10 23:17:43 +02:00
|
|
|
message.recipient = get_recipient_by_id(random.choice(recipient_streams))
|
2012-10-03 21:05:48 +02:00
|
|
|
|
|
|
|
if message_type == Recipient.HUDDLE:
|
|
|
|
sender_id = random.choice(huddle_members[message.recipient.id])
|
|
|
|
message.sender = get_user_profile_by_id(sender_id)
|
|
|
|
elif message_type == Recipient.PERSONAL:
|
|
|
|
message.recipient = Recipient.objects.get(type=Recipient.PERSONAL,
|
2016-11-30 14:17:35 +01:00
|
|
|
type_id=personals_pair[0])
|
2012-10-03 21:05:48 +02:00
|
|
|
message.sender = get_user_profile_by_id(personals_pair[1])
|
2016-01-27 22:14:21 +01:00
|
|
|
saved_data['personals_pair'] = personals_pair
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message_type == Recipient.STREAM:
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = Stream.objects.get(id=message.recipient.type_id)
|
2012-10-10 23:17:43 +02:00
|
|
|
# Pick a random subscriber to the stream
|
2012-10-03 21:05:48 +02:00
|
|
|
message.sender = random.choice(Subscription.objects.filter(
|
2017-01-24 07:06:13 +01:00
|
|
|
recipient=message.recipient)).user_profile
|
2016-12-08 05:06:51 +01:00
|
|
|
message.subject = stream.name + Text(random.randint(1, 3))
|
2016-01-27 22:14:21 +01:00
|
|
|
saved_data['subject'] = message.subject
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2017-02-25 21:02:13 +01:00
|
|
|
message.pub_date = timezone.now()
|
2017-01-22 07:07:29 +01:00
|
|
|
do_send_messages([{'message': message}])
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2016-01-26 02:19:31 +01:00
|
|
|
recipients[num_messages] = (message_type, message.recipient.id, saved_data)
|
2012-10-03 21:05:48 +02:00
|
|
|
num_messages += 1
|
|
|
|
return tot_messages
|
2017-01-27 00:06:55 +01:00
|
|
|
|
|
|
|
def create_simple_community_realm():
|
|
|
|
# type: () -> None
|
|
|
|
simple_realm = Realm.objects.create(
|
|
|
|
string_id="simple", name="Simple Realm", restricted_to_domain=False,
|
|
|
|
invite_required=False, org_type=Realm.COMMUNITY, domain="simple.com")
|
|
|
|
|
|
|
|
names = [
|
|
|
|
("alice", "alice@example.com"),
|
|
|
|
("bob", "bob@foo.edu"),
|
|
|
|
("cindy", "cindy@foo.tv"),
|
|
|
|
]
|
|
|
|
create_users(simple_realm, names)
|
|
|
|
|
|
|
|
user_profiles = UserProfile.objects.filter(realm__string_id='simple')
|
|
|
|
create_user_presences(user_profiles)
|
|
|
|
|
|
|
|
def create_user_presences(user_profiles):
|
|
|
|
# type: (Iterable[UserProfile]) -> None
|
|
|
|
for user in user_profiles:
|
|
|
|
status = 1 # type: int
|
2017-02-25 21:02:13 +01:00
|
|
|
date = timezone.now()
|
2017-01-27 00:06:55 +01:00
|
|
|
client = get_client("website")
|
|
|
|
UserPresence.objects.get_or_create(
|
|
|
|
user_profile=user,
|
|
|
|
client=client,
|
|
|
|
timestamp=date,
|
|
|
|
status=status)
|