2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2012-11-08 00:48:43 +01:00
|
|
|
from django.utils.timezone import utc, now
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-10-26 17:10:48 +02:00
|
|
|
from django.contrib.sites.models import Site
|
2012-10-19 21:30:42 +02:00
|
|
|
from zephyr.models import Message, UserProfile, Stream, Recipient, Client, \
|
2013-03-29 21:16:51 +01:00
|
|
|
Subscription, Huddle, get_huddle, Realm, UserMessage, \
|
2013-03-26 18:51:55 +01:00
|
|
|
get_huddle_hash, clear_database, get_client, get_user_profile_by_id
|
2013-03-29 18:36:27 +01:00
|
|
|
from zephyr.lib.actions import do_send_message, set_default_streams, \
|
|
|
|
do_activate_user, do_change_password
|
2012-09-14 18:31:11 +02:00
|
|
|
from zephyr.lib.parallel import run_parallel
|
2012-12-03 18:45:21 +01:00
|
|
|
from django.db import transaction, connection
|
2012-09-21 16:10:36 +02:00
|
|
|
from django.conf import settings
|
2013-03-27 15:58:23 +01:00
|
|
|
from zephyr.lib.bulk_create import bulk_create_realms, \
|
2013-01-10 21:50:09 +01:00
|
|
|
bulk_create_streams, bulk_create_users, bulk_create_huddles, \
|
|
|
|
bulk_create_clients
|
2013-01-10 22:49:07 +01:00
|
|
|
from zephyr.lib.timestamp import timestamp_to_datetime
|
2012-12-11 19:25:31 +01:00
|
|
|
from zephyr.models import MAX_MESSAGE_LENGTH
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2012-08-28 18:45:10 +02:00
|
|
|
import datetime
|
|
|
|
import random
|
2013-01-14 20:09:25 +01:00
|
|
|
import glob
|
2012-10-18 03:02:21 +02:00
|
|
|
import sys
|
2012-10-22 19:47:27 +02:00
|
|
|
import os
|
2013-01-14 20:09:25 +01:00
|
|
|
from os import path
|
2012-09-04 20:26:45 +02:00
|
|
|
from optparse import make_option
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-11-08 21:49:04 +01:00
|
|
|
settings.TORNADO_SERVER = None
|
2012-10-12 21:27:19 +02:00
|
|
|
|
2012-10-23 23:29:56 +02:00
|
|
|
def create_users(realms, name_list):
|
|
|
|
user_set = set()
|
|
|
|
for full_name, email in name_list:
|
2012-09-21 16:40:46 +02:00
|
|
|
(short_name, domain) = email.split("@")
|
2012-10-23 23:29:56 +02:00
|
|
|
user_set.add((email, full_name, short_name, True))
|
|
|
|
bulk_create_users(realms, user_set)
|
|
|
|
|
|
|
|
def create_streams(realms, realm, stream_list):
|
|
|
|
stream_set = set()
|
|
|
|
for stream_name in stream_list:
|
|
|
|
stream_set.add((realm.domain, stream_name))
|
|
|
|
bulk_create_streams(realms, stream_set)
|
2012-09-07 17:54:00 +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"
|
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
option_list = BaseCommand.option_list + (
|
2012-10-03 21:07:40 +02:00
|
|
|
make_option('-n', '--num-messages',
|
|
|
|
dest='num_messages',
|
2012-09-04 20:26:45 +02:00
|
|
|
type='int',
|
2012-09-14 18:31:11 +02:00
|
|
|
default=600,
|
2012-10-10 16:29:48 +02:00
|
|
|
help='The number of messages to create.'),
|
2012-09-20 06:19:37 +02:00
|
|
|
make_option('--extra-users',
|
|
|
|
dest='extra_users',
|
|
|
|
type='int',
|
|
|
|
default=0,
|
|
|
|
help='The number of extra users to create'),
|
2012-09-05 18:38:35 +02:00
|
|
|
make_option('--huddles',
|
|
|
|
dest='num_huddles',
|
|
|
|
type='int',
|
|
|
|
default=3,
|
|
|
|
help='The number of huddles to create.'),
|
|
|
|
make_option('--personals',
|
|
|
|
dest='num_personals',
|
|
|
|
type='int',
|
|
|
|
default=6,
|
|
|
|
help='The number of personal pairs to create.'),
|
2012-09-14 22:52:09 +02:00
|
|
|
make_option('--threads',
|
|
|
|
dest='threads',
|
|
|
|
type='int',
|
|
|
|
default=10,
|
|
|
|
help='The number of threads to use.'),
|
2012-09-05 18:38:35 +02:00
|
|
|
make_option('--percent-huddles',
|
|
|
|
dest='percent_huddles',
|
|
|
|
type='float',
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be huddles.'),
|
|
|
|
make_option('--percent-personals',
|
|
|
|
dest='percent_personals',
|
|
|
|
type='float',
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be personals.'),
|
2012-09-05 19:38:48 +02:00
|
|
|
make_option('--stickyness',
|
|
|
|
dest='stickyness',
|
|
|
|
type='float',
|
|
|
|
default=20,
|
|
|
|
help='The percent of messages to repeat recent folks.'),
|
2012-09-10 20:38:29 +02:00
|
|
|
make_option('--nodelete',
|
|
|
|
action="store_false",
|
|
|
|
default=True,
|
|
|
|
dest='delete',
|
|
|
|
help='Whether to delete all the existing messages.'),
|
2013-01-08 21:59:52 +01:00
|
|
|
make_option('--test-suite',
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
help='Whether to delete all the existing messages.'),
|
2012-10-03 21:07:40 +02:00
|
|
|
make_option('--replay-old-messages',
|
2012-09-27 19:58:42 +02:00
|
|
|
action="store_true",
|
|
|
|
default=False,
|
2012-10-03 21:07:40 +02:00
|
|
|
dest='replay_old_messages',
|
2012-09-27 19:58:42 +02:00
|
|
|
help='Whether to replace the log of old messages.'),
|
2012-09-04 20:26:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, **options):
|
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
|
2012-10-19 23:40:44 +02:00
|
|
|
humbug_realm = Realm.objects.create(domain="humbughq.com")
|
2013-01-08 21:38:47 +01:00
|
|
|
Realm.objects.create(domain="mit.edu")
|
2012-10-23 23:29:56 +02:00
|
|
|
realms = {}
|
|
|
|
for realm in Realm.objects.all():
|
|
|
|
realms[realm.domain] = realm
|
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).
|
2012-09-21 00:03:43 +02:00
|
|
|
names = [("Othello, the Moor of Venice", "othello@humbughq.com"), ("Iago", "iago@humbughq.com"),
|
|
|
|
("Prospero from The Tempest", "prospero@humbughq.com"),
|
|
|
|
("Cordelia Lear", "cordelia@humbughq.com"), ("King Hamlet", "hamlet@humbughq.com")]
|
2012-09-20 17:22:39 +02:00
|
|
|
for i in xrange(options["extra_users"]):
|
2012-11-23 20:02:47 +01:00
|
|
|
names.append(('Extra User %d' % (i,), 'extrauser%d@humbughq.com' % (i,)))
|
2012-10-23 23:29:56 +02:00
|
|
|
create_users(realms, names)
|
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"]
|
|
|
|
create_streams(realms, humbug_realm, stream_list)
|
|
|
|
recipient_streams = [recipient.type_id for recipient in
|
2012-10-10 22:57:21 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.STREAM)]
|
2012-10-10 23:17:43 +02:00
|
|
|
# Create subscriptions to streams
|
2012-10-23 23:29:56 +02:00
|
|
|
subscriptions_to_add = []
|
|
|
|
profiles = UserProfile.objects.select_related().all()
|
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)
|
|
|
|
s = Subscription(recipient=r, user_profile=profile)
|
|
|
|
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:
|
2012-09-21 00:03:43 +02:00
|
|
|
humbug_realm = Realm.objects.get(domain="humbughq.com")
|
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
|
2013-03-29 19:45:50 +01:00
|
|
|
user_profiles = [user_profile.id for user_profile in UserProfile.objects.all()]
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-05 18:38:35 +02:00
|
|
|
# Create several initial huddles
|
2012-09-20 17:22:39 +02:00
|
|
|
for i in xrange(options["num_huddles"]):
|
2013-03-29 19:45:50 +01:00
|
|
|
get_huddle(random.sample(user_profiles, random.randint(3, 4)))
|
2012-09-05 18:38:35 +02:00
|
|
|
|
|
|
|
# Create several initial pairs for personals
|
2013-03-29 19:45:50 +01:00
|
|
|
personals_pairs = [random.sample(user_profiles, 2)
|
2012-09-20 17:22:39 +02:00
|
|
|
for i in xrange(options["num_personals"])]
|
2012-09-14 17:52:38 +02:00
|
|
|
|
2012-09-14 22:52:09 +02:00
|
|
|
threads = options["threads"]
|
2012-09-14 18:31:11 +02:00
|
|
|
jobs = []
|
2012-09-20 17:22:39 +02:00
|
|
|
for i in xrange(threads):
|
2012-10-03 21:07:40 +02:00
|
|
|
count = options["num_messages"] / threads
|
|
|
|
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))
|
2012-10-03 21:07:40 +02:00
|
|
|
for status, job in run_parallel(send_messages, jobs, threads=threads):
|
2012-09-14 18:31:11 +02:00
|
|
|
pass
|
2012-12-03 18:45:21 +01:00
|
|
|
# Get a new database connection, after our parallel jobs
|
|
|
|
# closed the original one
|
|
|
|
connection.close()
|
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
|
|
|
|
# default values in zephyr/decorators.py will not work
|
|
|
|
# with the Django test suite.
|
|
|
|
get_client("website")
|
|
|
|
get_client("API")
|
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
# Create internal users; first the ones who are referenced
|
|
|
|
# directly by the test suite; 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"),
|
|
|
|
]
|
|
|
|
create_users(realms, testsuite_mit_users)
|
|
|
|
|
|
|
|
# These bots are directly referenced from code and thus
|
|
|
|
# are needed for the test suite.
|
|
|
|
hardcoded_humbug_users_nosubs = [
|
|
|
|
("Humbug New User Bot", "humbug+signups@humbughq.com"),
|
|
|
|
("Humbug Error Bot", "humbug+errors@humbughq.com"),
|
2013-01-14 19:39:24 +01:00
|
|
|
("Humbug Notification Bot", "humbug+notifications@humbughq.com"),
|
2013-02-14 03:53:36 +01:00
|
|
|
("Humbug Tutorial Bot", "humbug+tutorial@humbughq.com"),
|
2013-01-08 21:59:52 +01:00
|
|
|
]
|
|
|
|
create_users(realms, hardcoded_humbug_users_nosubs)
|
|
|
|
|
|
|
|
if not options["test_suite"]:
|
|
|
|
# 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
|
|
|
|
|
|
|
|
internal_mit_users = []
|
|
|
|
create_users(realms, internal_mit_users)
|
|
|
|
|
2013-06-12 16:46:25 +02:00
|
|
|
create_users(realms, settings.INTERNAL_HUMBUG_USERS)
|
2013-05-10 20:30:40 +02:00
|
|
|
humbug_stream_list = ["devel", "all", "humbug", "design", "support", "social", "test",
|
|
|
|
"errors"]
|
2013-01-08 21:59:52 +01:00
|
|
|
create_streams(realms, humbug_realm, humbug_stream_list)
|
|
|
|
|
|
|
|
# Now subscribe everyone to these streams
|
|
|
|
subscriptions_to_add = []
|
|
|
|
profiles = UserProfile.objects.select_related().filter(realm=humbug_realm)
|
|
|
|
for cls in humbug_stream_list:
|
|
|
|
stream = Stream.objects.get(name=cls, realm=humbug_realm)
|
|
|
|
recipient = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id)
|
|
|
|
for profile in profiles:
|
|
|
|
# Subscribe to some streams.
|
|
|
|
s = Subscription(recipient=recipient, user_profile=profile)
|
|
|
|
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
|
|
|
|
internal_humbug_users_nosubs = [
|
|
|
|
("Humbug Commit Bot", "humbug+commits@humbughq.com"),
|
|
|
|
("Humbug Trac Bot", "humbug+trac@humbughq.com"),
|
|
|
|
("Humbug Nagios Bot", "humbug+nagios@humbughq.com"),
|
|
|
|
("Humbug Feedback Bot", "feedback@humbughq.com"),
|
|
|
|
]
|
|
|
|
create_users(realms, internal_humbug_users_nosubs)
|
|
|
|
|
2013-03-07 17:12:35 +01:00
|
|
|
# Mark all messages as read
|
|
|
|
with transaction.commit_on_success():
|
2013-04-04 17:22:58 +02: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-03 21:07:40 +02:00
|
|
|
if options["replay_old_messages"]:
|
|
|
|
restore_saved_messages()
|
2012-12-13 19:42:03 +01:00
|
|
|
connection.close()
|
2012-10-02 22:58:13 +02:00
|
|
|
|
2012-09-14 22:43:54 +02:00
|
|
|
recipient_hash = {}
|
|
|
|
def get_recipient_by_id(rid):
|
|
|
|
if rid in recipient_hash:
|
|
|
|
return recipient_hash[rid]
|
|
|
|
return Recipient.objects.get(id=rid)
|
|
|
|
|
2012-10-03 21:07:40 +02:00
|
|
|
def restore_saved_messages():
|
2012-10-20 18:02:58 +02:00
|
|
|
old_messages = []
|
2012-10-20 21:43:13 +02:00
|
|
|
duplicate_suppression_hash = {}
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2012-12-03 17:46:48 +01:00
|
|
|
stream_dict = {}
|
2012-10-20 18:02:58 +02:00
|
|
|
user_set = set()
|
2013-03-28 20:43:34 +01:00
|
|
|
email_set = set([u.email for u in UserProfile.objects.all()])
|
2012-10-20 18:02:58 +02:00
|
|
|
realm_set = set()
|
2012-10-19 21:30:42 +02:00
|
|
|
# Initial client_set is nonempty temporarily because we don't have
|
|
|
|
# clients in logs at all right now -- later we can start with nothing.
|
|
|
|
client_set = set(["populate_db", "website", "zephyr_mirror"])
|
2012-10-20 18:02:58 +02:00
|
|
|
huddle_user_set = set()
|
|
|
|
# First, determine all the objects our messages will need.
|
|
|
|
print datetime.datetime.now(), "Creating realms/streams/etc..."
|
2012-12-13 15:14:59 +01:00
|
|
|
def process_line(line):
|
2012-10-20 21:43:13 +02:00
|
|
|
old_message_json = line.strip()
|
|
|
|
|
|
|
|
# Due to populate_db's shakespeare mode, we have a lot of
|
|
|
|
# duplicate messages in our log that only differ in their
|
|
|
|
# logged ID numbers (same timestamp, content, etc.). With
|
|
|
|
# sqlite, bulk creating those messages won't work properly: in
|
|
|
|
# particular, the first 100 messages will actually only result
|
|
|
|
# in 20 rows ending up in the target table, which screws up
|
|
|
|
# the below accounting where for handling changing
|
|
|
|
# subscriptions, we assume that the Nth row populate_db
|
|
|
|
# created goes with the Nth non-subscription row of the input
|
|
|
|
# So suppress the duplicates when using sqlite.
|
|
|
|
if "sqlite" in settings.DATABASES["default"]["ENGINE"]:
|
2013-06-18 23:55:55 +02:00
|
|
|
tmp_message = ujson.loads(old_message_json)
|
2012-10-20 21:43:13 +02:00
|
|
|
tmp_message['id'] = '1'
|
2013-06-18 23:55:55 +02:00
|
|
|
duplicate_suppression_key = ujson.dumps(tmp_message)
|
2012-10-20 21:43:13 +02:00
|
|
|
if duplicate_suppression_key in duplicate_suppression_hash:
|
2012-12-13 15:14:59 +01:00
|
|
|
return
|
2012-10-20 21:43:13 +02:00
|
|
|
duplicate_suppression_hash[duplicate_suppression_key] = True
|
|
|
|
|
2013-06-18 23:55:55 +02:00
|
|
|
old_message = ujson.loads(old_message_json)
|
2012-10-25 20:19:34 +02:00
|
|
|
message_type = old_message["type"]
|
2012-10-21 01:42:56 +02:00
|
|
|
|
2012-12-03 17:46:48 +01:00
|
|
|
# Lower case emails and domains; it will screw up
|
|
|
|
# deduplication if we don't
|
2012-12-04 20:58:45 +01:00
|
|
|
def fix_email(email):
|
|
|
|
return email.strip().lower()
|
|
|
|
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type in ["stream", "huddle", "personal"]:
|
|
|
|
old_message["sender_email"] = fix_email(old_message["sender_email"])
|
2012-12-11 19:25:31 +01:00
|
|
|
# Fix the length on too-long messages before we start processing them
|
|
|
|
if len(old_message["content"]) > MAX_MESSAGE_LENGTH:
|
|
|
|
old_message["content"] = "[ This message was deleted because it was too long ]"
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type in ["subscription_added", "subscription_removed"]:
|
2012-10-21 01:42:56 +02:00
|
|
|
old_message["domain"] = old_message["domain"].lower()
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["user"] = fix_email(old_message["user"])
|
2012-12-13 02:50:34 +01:00
|
|
|
elif message_type == "subscription_property":
|
|
|
|
old_message["user"] = fix_email(old_message["user"])
|
2013-05-17 16:36:38 +02:00
|
|
|
elif message_type == "user_email_changed":
|
|
|
|
old_message["old_email"] = fix_email(old_message["old_email"])
|
|
|
|
old_message["new_email"] = fix_email(old_message["new_email"])
|
2012-10-25 21:39:34 +02:00
|
|
|
elif message_type.startswith("user_"):
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["user"] = fix_email(old_message["user"])
|
2012-11-23 21:23:41 +01:00
|
|
|
elif message_type.startswith("enable_"):
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["user"] = fix_email(old_message["user"])
|
2012-10-21 01:42:56 +02:00
|
|
|
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type == 'personal':
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["recipient"][0]["email"] = fix_email(old_message["recipient"][0]["email"])
|
2012-10-25 20:19:34 +02:00
|
|
|
elif message_type == "huddle":
|
2012-10-21 01:42:56 +02:00
|
|
|
for i in xrange(len(old_message["recipient"])):
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["recipient"][i]["email"] = fix_email(old_message["recipient"][i]["email"])
|
2012-10-21 01:42:56 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
old_messages.append(old_message)
|
2012-10-18 03:02:21 +02:00
|
|
|
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type in ["subscription_added", "subscription_removed"]:
|
2012-12-03 17:46:48 +01:00
|
|
|
stream_name = old_message["name"].strip()
|
|
|
|
canon_stream_name = stream_name.lower()
|
|
|
|
if canon_stream_name not in stream_dict:
|
|
|
|
stream_dict[(old_message["domain"], canon_stream_name)] = \
|
|
|
|
(old_message["domain"], stream_name)
|
2012-12-04 21:07:33 +01:00
|
|
|
elif message_type == "user_created":
|
|
|
|
user_set.add((old_message["user"], old_message["full_name"], old_message["short_name"], False))
|
2012-12-04 21:32:58 +01:00
|
|
|
elif message_type == "realm_created":
|
|
|
|
realm_set.add(old_message["domain"])
|
2012-12-05 20:00:59 +01:00
|
|
|
|
|
|
|
if message_type not in ["stream", "huddle", "personal"]:
|
2012-12-13 15:14:59 +01:00
|
|
|
return
|
2012-12-05 20:00:59 +01:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
sender_email = old_message["sender_email"]
|
2012-11-27 18:26:51 +01:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
domain = sender_email.split('@')[1]
|
|
|
|
realm_set.add(domain)
|
|
|
|
|
|
|
|
if old_message["sender_email"] not in email_set:
|
|
|
|
user_set.add((old_message["sender_email"],
|
|
|
|
old_message["sender_full_name"],
|
2012-10-23 22:39:40 +02:00
|
|
|
old_message["sender_short_name"],
|
|
|
|
False))
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2012-10-19 21:30:42 +02:00
|
|
|
if 'sending_client' in old_message:
|
|
|
|
client_set.add(old_message['sending_client'])
|
|
|
|
|
2012-10-25 20:19:34 +02:00
|
|
|
if message_type == 'stream':
|
2012-12-03 17:46:48 +01:00
|
|
|
stream_name = old_message["recipient"].strip()
|
|
|
|
canon_stream_name = stream_name.lower()
|
|
|
|
if canon_stream_name not in stream_dict:
|
|
|
|
stream_dict[(domain, canon_stream_name)] = (domain, stream_name)
|
2012-10-25 20:19:34 +02:00
|
|
|
elif message_type == 'personal':
|
2012-10-20 18:02:58 +02:00
|
|
|
u = old_message["recipient"][0]
|
|
|
|
if u["email"] not in email_set:
|
2012-10-23 22:39:40 +02:00
|
|
|
user_set.add((u["email"], u["full_name"], u["short_name"], False))
|
2012-10-20 18:02:58 +02:00
|
|
|
email_set.add(u["email"])
|
2012-10-25 20:19:34 +02:00
|
|
|
elif message_type == 'huddle':
|
2012-10-20 18:02:58 +02:00
|
|
|
for u in old_message["recipient"]:
|
2012-10-23 22:39:40 +02:00
|
|
|
user_set.add((u["email"], u["full_name"], u["short_name"], False))
|
2012-10-20 18:02:58 +02:00
|
|
|
if u["email"] not in email_set:
|
2012-10-23 22:39:40 +02:00
|
|
|
user_set.add((u["email"], u["full_name"], u["short_name"], False))
|
2012-10-20 18:02:58 +02:00
|
|
|
email_set.add(u["email"])
|
2012-11-08 23:13:09 +01:00
|
|
|
huddle_user_set.add(tuple(sorted(set(u["email"] for u in old_message["recipient"]))))
|
2012-10-21 01:48:05 +02:00
|
|
|
else:
|
2012-10-31 16:40:41 +01:00
|
|
|
raise ValueError('Bad message type')
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2013-01-14 20:09:25 +01:00
|
|
|
event_glob = path.join(settings.EVENT_LOG_DIR, 'events.*')
|
|
|
|
for filename in sorted(glob.glob(event_glob)):
|
|
|
|
with file(filename, "r") as message_log:
|
|
|
|
for line in message_log.readlines():
|
|
|
|
process_line(line)
|
2012-12-13 15:14:59 +01:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
stream_recipients = {}
|
|
|
|
user_recipients = {}
|
|
|
|
huddle_recipients = {}
|
|
|
|
|
|
|
|
# Then, create the objects our messages need.
|
|
|
|
print datetime.datetime.now(), "Creating realms..."
|
|
|
|
bulk_create_realms(realm_set)
|
|
|
|
|
|
|
|
realms = {}
|
|
|
|
for realm in Realm.objects.all():
|
|
|
|
realms[realm.domain] = realm
|
|
|
|
|
2012-10-19 21:30:42 +02:00
|
|
|
print datetime.datetime.now(), "Creating clients..."
|
|
|
|
bulk_create_clients(client_set)
|
|
|
|
|
|
|
|
clients = {}
|
|
|
|
for client in Client.objects.all():
|
|
|
|
clients[client.name] = client
|
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
print datetime.datetime.now(), "Creating streams..."
|
2012-12-03 17:46:48 +01:00
|
|
|
bulk_create_streams(realms, stream_dict.values())
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
streams = {}
|
|
|
|
for stream in Stream.objects.all():
|
|
|
|
streams[stream.id] = stream
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.STREAM):
|
|
|
|
stream_recipients[(streams[recipient.type_id].realm_id,
|
2012-10-21 01:54:29 +02:00
|
|
|
streams[recipient.type_id].name.lower())] = recipient
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
print datetime.datetime.now(), "Creating users..."
|
|
|
|
bulk_create_users(realms, user_set)
|
|
|
|
|
|
|
|
users = {}
|
|
|
|
users_by_id = {}
|
|
|
|
for user_profile in UserProfile.objects.select_related().all():
|
2013-03-28 20:43:34 +01:00
|
|
|
users[user_profile.email] = user_profile
|
2012-10-20 18:02:58 +02:00
|
|
|
users_by_id[user_profile.id] = user_profile
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.PERSONAL):
|
2013-03-28 20:43:34 +01:00
|
|
|
user_recipients[users_by_id[recipient.type_id].email] = recipient
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
print datetime.datetime.now(), "Creating huddles..."
|
|
|
|
bulk_create_huddles(users, huddle_user_set)
|
|
|
|
|
|
|
|
huddles_by_id = {}
|
|
|
|
for huddle in Huddle.objects.all():
|
|
|
|
huddles_by_id[huddle.id] = huddle
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.HUDDLE):
|
|
|
|
huddle_recipients[huddles_by_id[recipient.type_id].huddle_hash] = recipient
|
|
|
|
|
|
|
|
# TODO: Add a special entry type in the log that is a subscription
|
|
|
|
# change and import those as we go to make subscription changes
|
|
|
|
# take effect!
|
|
|
|
print datetime.datetime.now(), "Importing subscriptions..."
|
|
|
|
subscribers = {}
|
|
|
|
for s in Subscription.objects.select_related().all():
|
|
|
|
if s.active:
|
2012-10-22 20:15:25 +02:00
|
|
|
subscribers.setdefault(s.recipient.id, set()).add(s.user_profile.id)
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
# Then create all the messages, without talking to the DB!
|
|
|
|
print datetime.datetime.now(), "Importing messages, part 1..."
|
2012-10-20 21:43:13 +02:00
|
|
|
first_message_id = None
|
|
|
|
if Message.objects.exists():
|
|
|
|
first_message_id = Message.objects.all().order_by("-id")[0].id + 1
|
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
messages_to_create = []
|
|
|
|
for idx, old_message in enumerate(old_messages):
|
2012-12-06 00:08:21 +01:00
|
|
|
message_type = old_message["type"]
|
|
|
|
if message_type not in ["stream", "huddle", "personal"]:
|
2012-10-20 21:43:13 +02:00
|
|
|
continue
|
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
message = Message()
|
2012-09-29 00:33:49 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
sender_email = old_message["sender_email"]
|
2012-10-20 18:02:58 +02:00
|
|
|
domain = sender_email.split('@')[1]
|
|
|
|
realm = realms[domain]
|
2012-09-29 00:33:49 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
message.sender = users[sender_email]
|
|
|
|
type_hash = {"stream": Recipient.STREAM,
|
|
|
|
"huddle": Recipient.HUDDLE,
|
|
|
|
"personal": Recipient.PERSONAL}
|
2012-10-19 21:30:42 +02:00
|
|
|
|
|
|
|
if 'sending_client' in old_message:
|
|
|
|
message.sending_client = clients[old_message['sending_client']]
|
|
|
|
elif sender_email in ["othello@humbughq.com", "iago@humbughq.com", "prospero@humbughq.com",
|
|
|
|
"cordelia@humbughq.com", "hamlet@humbughq.com"]:
|
|
|
|
message.sending_client = clients['populate_db']
|
|
|
|
elif realm.domain == "humbughq.com":
|
|
|
|
message.sending_client = clients["website"]
|
|
|
|
elif realm.domain == "mit.edu":
|
|
|
|
message.sending_client = clients['zephyr_mirror']
|
|
|
|
else:
|
|
|
|
message.sending_client = clients['populate_db']
|
|
|
|
|
2012-12-06 00:08:21 +01:00
|
|
|
message.type = type_hash[message_type]
|
2012-10-03 21:05:48 +02:00
|
|
|
message.content = old_message["content"]
|
2012-10-11 00:01:39 +02:00
|
|
|
message.subject = old_message["subject"]
|
2012-12-05 19:53:23 +01:00
|
|
|
message.pub_date = timestamp_to_datetime(old_message["timestamp"])
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
if message.type == Recipient.PERSONAL:
|
2012-10-23 03:11:39 +02:00
|
|
|
message.recipient = user_recipients[old_message["recipient"][0]["email"]]
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message.type == Recipient.STREAM:
|
2012-10-20 18:02:58 +02:00
|
|
|
message.recipient = stream_recipients[(realm.id,
|
2012-12-03 17:46:48 +01:00
|
|
|
old_message["recipient"].lower())]
|
2012-10-03 21:05:48 +02:00
|
|
|
elif message.type == Recipient.HUDDLE:
|
2012-10-20 18:02:58 +02:00
|
|
|
huddle_hash = get_huddle_hash([users[u["email"]].id
|
|
|
|
for u in old_message["recipient"]])
|
|
|
|
message.recipient = huddle_recipients[huddle_hash]
|
2012-09-27 19:58:42 +02:00
|
|
|
else:
|
2012-10-31 16:40:41 +01:00
|
|
|
raise ValueError('Bad message type')
|
2012-10-20 18:02:58 +02:00
|
|
|
messages_to_create.append(message)
|
|
|
|
|
|
|
|
print datetime.datetime.now(), "Importing messages, part 2..."
|
2013-03-27 15:58:23 +01:00
|
|
|
Message.objects.bulk_create(messages_to_create)
|
2012-12-13 15:14:59 +01:00
|
|
|
messages_to_create = []
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
# Finally, create all the UserMessage objects
|
|
|
|
print datetime.datetime.now(), "Importing usermessages, part 1..."
|
2012-10-23 04:01:55 +02:00
|
|
|
personal_recipients = {}
|
|
|
|
for r in Recipient.objects.filter(type = Recipient.PERSONAL):
|
|
|
|
personal_recipients[r.id] = True
|
|
|
|
|
|
|
|
all_messages = Message.objects.all()
|
2012-10-20 18:02:58 +02:00
|
|
|
user_messages_to_create = []
|
|
|
|
|
2012-10-20 21:43:13 +02:00
|
|
|
messages_by_id = {}
|
2012-10-20 18:02:58 +02:00
|
|
|
for message in all_messages:
|
2012-10-20 21:43:13 +02:00
|
|
|
messages_by_id[message.id] = message
|
|
|
|
|
|
|
|
if first_message_id is None:
|
|
|
|
first_message_id = min(messages_by_id.keys())
|
|
|
|
|
2012-12-13 01:30:50 +01:00
|
|
|
tot_user_messages = 0
|
2012-10-20 23:31:48 +02:00
|
|
|
pending_subs = {}
|
2012-10-20 21:43:13 +02:00
|
|
|
current_message_id = first_message_id
|
2012-12-13 02:50:34 +01:00
|
|
|
pending_colors = {}
|
2012-10-20 21:43:13 +02:00
|
|
|
for old_message in old_messages:
|
2012-12-06 00:08:21 +01:00
|
|
|
message_type = old_message["type"]
|
|
|
|
if message_type == 'subscription_added':
|
2012-12-03 17:46:48 +01:00
|
|
|
stream_key = (realms[old_message["domain"]].id, old_message["name"].strip().lower())
|
2012-10-20 21:43:13 +02:00
|
|
|
subscribers.setdefault(stream_recipients[stream_key].id,
|
|
|
|
set()).add(users[old_message["user"]].id)
|
2012-10-20 23:31:48 +02:00
|
|
|
pending_subs[(stream_recipients[stream_key].id,
|
|
|
|
users[old_message["user"]].id)] = True
|
2012-10-20 21:43:13 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "subscription_removed":
|
2012-12-03 17:46:48 +01:00
|
|
|
stream_key = (realms[old_message["domain"]].id, old_message["name"].strip().lower())
|
2012-11-07 17:14:43 +01:00
|
|
|
user_id = users[old_message["user"]].id
|
|
|
|
subscribers.setdefault(stream_recipients[stream_key].id, set())
|
|
|
|
try:
|
|
|
|
subscribers[stream_recipients[stream_key].id].remove(user_id)
|
|
|
|
except KeyError:
|
|
|
|
print "Error unsubscribing %s from %s: not subscribed" % (
|
|
|
|
old_message["user"], old_message["name"])
|
2012-10-20 23:31:48 +02:00
|
|
|
pending_subs[(stream_recipients[stream_key].id,
|
|
|
|
users[old_message["user"]].id)] = False
|
2012-10-20 21:43:13 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "user_activated" or message_type == "user_created":
|
2012-10-25 21:39:34 +02:00
|
|
|
# These are rare, so just handle them the slow way
|
2013-03-08 19:58:18 +01:00
|
|
|
user_profile = users[old_message["user"]]
|
2012-12-05 19:53:23 +01:00
|
|
|
join_date = timestamp_to_datetime(old_message['timestamp'])
|
2013-03-08 19:58:18 +01:00
|
|
|
do_activate_user(user_profile, log=False, join_date=join_date)
|
2012-10-25 20:19:55 +02:00
|
|
|
# Update the cache of users to show this user as activated
|
2013-03-08 19:58:18 +01:00
|
|
|
users_by_id[user_profile.id] = user_profile
|
|
|
|
users[old_message["user"]] = user_profile
|
2012-10-25 20:19:55 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "user_change_password":
|
2012-10-25 21:39:34 +02:00
|
|
|
# Just handle these the slow way
|
2013-03-29 18:36:27 +01:00
|
|
|
user_profile = users[old_message["user"]]
|
2013-05-17 16:41:01 +02:00
|
|
|
do_change_password(user_profile, old_message["pwhash"], log=False,
|
2013-03-29 18:36:27 +01:00
|
|
|
hashed_password=True)
|
2012-10-25 21:39:34 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "user_change_full_name":
|
2012-10-25 21:39:34 +02:00
|
|
|
# Just handle these the slow way
|
2013-03-28 20:20:31 +01:00
|
|
|
user_profile = users[old_message["user"]]
|
2012-10-25 21:39:34 +02:00
|
|
|
user_profile.full_name = old_message["full_name"]
|
|
|
|
user_profile.save()
|
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "enable_desktop_notifications_changed":
|
2012-11-23 21:23:41 +01:00
|
|
|
# Just handle these the slow way
|
2013-03-28 20:20:31 +01:00
|
|
|
user_profile = users[old_message["user"]]
|
2012-12-05 23:58:21 +01:00
|
|
|
user_profile.enable_desktop_notifications = (old_message["enable_desktop_notifications"] != "false")
|
2012-11-23 21:23:41 +01:00
|
|
|
user_profile.save()
|
|
|
|
continue
|
2013-05-03 21:49:01 +02:00
|
|
|
elif message_type == "enable_sounds_changed":
|
|
|
|
user_profile = users[old_message["user"]]
|
|
|
|
user_profile.enable_sounds = (old_message["enable_sounds"] != "false")
|
2013-05-07 23:19:52 +02:00
|
|
|
elif message_type == "enable_offline_email_notifications_changed":
|
|
|
|
user_profile = users[old_message["user"]]
|
|
|
|
user_profile.enable_offline_email_notifications = (old_message["enable_offline_email_notifications"] != "false")
|
2013-05-03 21:49:01 +02:00
|
|
|
user_profile.save()
|
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "default_streams":
|
2012-11-27 18:26:51 +01:00
|
|
|
set_default_streams(Realm.objects.get(domain=old_message["domain"]),
|
|
|
|
old_message["streams"])
|
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "subscription_property":
|
2012-12-13 02:50:34 +01:00
|
|
|
property_name = old_message.get("property")
|
2013-04-08 18:01:01 +02:00
|
|
|
if property_name == "stream_color" or property_name == "color":
|
2013-05-17 16:50:31 +02:00
|
|
|
color = old_message.get("color", old_message.get("value"))
|
2012-12-13 02:50:34 +01:00
|
|
|
pending_colors[(old_message["user"],
|
2013-04-08 18:01:01 +02:00
|
|
|
old_message["stream_name"].lower())] = color
|
2013-05-20 21:33:17 +02:00
|
|
|
elif property_name in ["in_home_view", "notifications"]:
|
2013-05-17 17:21:53 +02:00
|
|
|
# TODO: Handle this
|
|
|
|
continue
|
2012-12-13 02:50:34 +01:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown property %s" % (property_name,))
|
2012-12-03 00:19:00 +01:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "realm_created":
|
2013-05-17 17:21:53 +02:00
|
|
|
# No action required
|
|
|
|
continue
|
|
|
|
elif message_type in ["user_email_changed", "update_onboarding", "update_message"]:
|
|
|
|
# TODO: Handle these
|
2012-12-04 21:32:58 +01:00
|
|
|
continue
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type not in ["stream", "huddle", "personal"]:
|
|
|
|
raise RuntimeError("Unexpected message type %s" % (message_type,))
|
2012-10-20 21:43:13 +02:00
|
|
|
|
|
|
|
message = messages_by_id[current_message_id]
|
|
|
|
current_message_id += 1
|
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
if message.recipient_id not in subscribers:
|
|
|
|
# Nobody received this message -- probably due to our
|
|
|
|
# subscriptions being out-of-date.
|
|
|
|
continue
|
2012-11-08 23:16:26 +01:00
|
|
|
|
|
|
|
recipient_user_ids = set()
|
2012-10-20 18:02:58 +02:00
|
|
|
for user_profile_id in subscribers[message.recipient_id]:
|
2012-11-08 23:16:26 +01:00
|
|
|
recipient_user_ids.add(user_profile_id)
|
2012-10-23 04:01:55 +02:00
|
|
|
if message.recipient_id in personal_recipients:
|
2012-10-23 03:11:39 +02:00
|
|
|
# Include the sender in huddle recipients
|
2012-11-08 23:16:26 +01:00
|
|
|
recipient_user_ids.add(message.sender_id)
|
|
|
|
|
|
|
|
for user_profile_id in recipient_user_ids:
|
2013-03-28 20:47:22 +01:00
|
|
|
if users_by_id[user_profile_id].is_active:
|
2012-11-08 23:16:26 +01:00
|
|
|
um = UserMessage(user_profile_id=user_profile_id,
|
2012-10-23 22:39:40 +02:00
|
|
|
message=message)
|
|
|
|
user_messages_to_create.append(um)
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2012-12-13 01:30:50 +01:00
|
|
|
if len(user_messages_to_create) > 100000:
|
|
|
|
tot_user_messages += len(user_messages_to_create)
|
2013-03-27 15:58:23 +01:00
|
|
|
UserMessage.objects.bulk_create(user_messages_to_create)
|
2012-12-13 01:30:50 +01:00
|
|
|
user_messages_to_create = []
|
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
print datetime.datetime.now(), "Importing usermessages, part 2..."
|
2012-12-13 01:30:50 +01:00
|
|
|
tot_user_messages += len(user_messages_to_create)
|
2013-03-27 15:58:23 +01:00
|
|
|
UserMessage.objects.bulk_create(user_messages_to_create)
|
2012-10-20 23:31:48 +02:00
|
|
|
|
|
|
|
print datetime.datetime.now(), "Finalizing subscriptions..."
|
|
|
|
current_subs = {}
|
|
|
|
current_subs_obj = {}
|
|
|
|
for s in Subscription.objects.select_related().all():
|
2012-10-22 20:15:25 +02:00
|
|
|
current_subs[(s.recipient_id, s.user_profile_id)] = s.active
|
|
|
|
current_subs_obj[(s.recipient_id, s.user_profile_id)] = s
|
2012-10-20 23:31:48 +02:00
|
|
|
|
|
|
|
subscriptions_to_add = []
|
|
|
|
subscriptions_to_change = []
|
|
|
|
for pending_sub in pending_subs.keys():
|
|
|
|
(recipient_id, user_profile_id) = pending_sub
|
|
|
|
current_state = current_subs.get(pending_sub)
|
|
|
|
if pending_subs[pending_sub] == current_state:
|
|
|
|
# Already correct in the database
|
|
|
|
continue
|
|
|
|
elif current_state is not None:
|
|
|
|
subscriptions_to_change.append((pending_sub, pending_subs[pending_sub]))
|
|
|
|
continue
|
|
|
|
|
|
|
|
s = Subscription(recipient_id=recipient_id,
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile_id=user_profile_id,
|
2012-10-20 23:31:48 +02:00
|
|
|
active=pending_subs[pending_sub])
|
|
|
|
subscriptions_to_add.append(s)
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_add)
|
2012-10-20 23:31:48 +02:00
|
|
|
with transaction.commit_on_success():
|
|
|
|
for (sub, active) in subscriptions_to_change:
|
|
|
|
current_subs_obj[sub].active = active
|
|
|
|
current_subs_obj[sub].save()
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2012-12-13 02:50:34 +01:00
|
|
|
subs = {}
|
|
|
|
for sub in Subscription.objects.all():
|
|
|
|
subs[(sub.user_profile_id, sub.recipient_id)] = sub
|
|
|
|
|
2013-03-29 21:16:51 +01:00
|
|
|
# TODO: do restore of subscription colors -- we're currently not
|
|
|
|
# logging changes so there's little point in having the code :(
|
2012-12-13 02:50:34 +01:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
print datetime.datetime.now(), "Finished importing %s messages (%s usermessages)" % \
|
|
|
|
(len(all_messages), tot_user_messages)
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-12-04 23:50:02 +01:00
|
|
|
site = Site.objects.get_current()
|
|
|
|
site.domain = 'humbughq.com'
|
|
|
|
site.save()
|
|
|
|
|
|
|
|
print datetime.datetime.now(), "Filling in user pointers..."
|
|
|
|
|
|
|
|
# Set restored pointers to the very latest messages
|
|
|
|
with transaction.commit_on_success():
|
2012-12-11 22:48:36 +01:00
|
|
|
for user_profile in UserProfile.objects.all():
|
2012-12-04 23:50:02 +01:00
|
|
|
try:
|
2012-12-11 22:48:36 +01:00
|
|
|
top = UserMessage.objects.filter(
|
|
|
|
user_profile_id=user_profile.id).order_by("-message")[0]
|
2012-12-04 23:50:02 +01:00
|
|
|
user_profile.pointer = top.message_id
|
|
|
|
except IndexError:
|
|
|
|
user_profile.pointer = -1
|
|
|
|
user_profile.save()
|
|
|
|
|
|
|
|
print datetime.datetime.now(), "Done replaying old messages"
|
2012-09-27 19:58:42 +02:00
|
|
|
|
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):
|
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())
|
2012-12-03 18:45:21 +01:00
|
|
|
# Close the database connection, so that we get a new one that
|
|
|
|
# isn't shared with the other threads
|
2012-09-14 18:31:11 +02:00
|
|
|
connection.close()
|
2012-10-03 21:07:40 +02:00
|
|
|
texts = file("zephyr/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
|
2012-10-10 22:57:21 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.STREAM)]
|
2012-09-14 22:22:23 +02:00
|
|
|
recipient_huddles = [h.id for h in Recipient.objects.filter(type=Recipient.HUDDLE)]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
|
|
|
huddle_members = {}
|
|
|
|
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
|
|
|
|
recipients = {}
|
2012-10-03 21:05:48 +02:00
|
|
|
while num_messages < tot_messages:
|
2012-09-14 18:31:11 +02:00
|
|
|
with transaction.commit_on_success():
|
|
|
|
saved_data = ''
|
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
|
2012-09-14 18:31:11 +02:00
|
|
|
random.randint(1, random_max) * 100. / random_max < options["stickyness"]):
|
|
|
|
# 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:
|
2012-09-14 18:31:11 +02:00
|
|
|
personals_pair = saved_data
|
|
|
|
random.shuffle(personals_pair)
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message_type == Recipient.STREAM:
|
2012-10-11 00:01:39 +02:00
|
|
|
message.subject = saved_data
|
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,
|
2012-09-14 18:31:11 +02:00
|
|
|
type_id=personals_pair[0])
|
2012-10-03 21:05:48 +02:00
|
|
|
message.sender = get_user_profile_by_id(personals_pair[1])
|
2012-09-14 18:31:11 +02:00
|
|
|
saved_data = 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(
|
2012-10-22 20:15:25 +02:00
|
|
|
recipient=message.recipient)).user_profile
|
2012-10-11 00:01:39 +02:00
|
|
|
message.subject = stream.name + str(random.randint(1, 3))
|
|
|
|
saved_data = message.subject
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-11-08 00:48:43 +01:00
|
|
|
message.pub_date = now()
|
2012-10-03 21:05:48 +02:00
|
|
|
do_send_message(message)
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
recipients[num_messages] = [message_type, message.recipient.id, saved_data]
|
|
|
|
num_messages += 1
|
2012-12-13 19:42:03 +01:00
|
|
|
connection.close()
|
2012-10-03 21:05:48 +02:00
|
|
|
return tot_messages
|