2012-09-04 20:26:45 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2012-08-28 23:08:38 +02:00
|
|
|
from django.utils.timezone import utc
|
2012-08-28 18:45:10 +02:00
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2012-09-05 18:38:35 +02:00
|
|
|
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, \
|
2012-09-07 19:24:54 +02:00
|
|
|
Subscription, Huddle, get_huddle, Realm, create_user_profile, UserMessage, \
|
|
|
|
create_zephyr_class
|
2012-09-13 02:29:26 +02:00
|
|
|
from zephyr.mit_subs_list import subs_list
|
2012-08-28 18:45:10 +02:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import random
|
2012-09-04 20:26:45 +02:00
|
|
|
from optparse import make_option
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-09-11 19:20:20 +02:00
|
|
|
def create_users(name_list, realm):
|
|
|
|
for name, username in name_list:
|
2012-09-07 17:54:00 +02:00
|
|
|
if User.objects.filter(username=username):
|
|
|
|
# We're trying to create the same user twice!
|
|
|
|
raise
|
2012-09-11 19:20:20 +02:00
|
|
|
user = User.objects.create_user(username=username, password=username,
|
|
|
|
email=username+"@humbughq.com")
|
2012-09-07 17:54:00 +02:00
|
|
|
user.save()
|
2012-09-11 19:20:20 +02:00
|
|
|
create_user_profile(user, realm, name, username)
|
2012-09-07 17:54:00 +02:00
|
|
|
|
|
|
|
def create_classes(class_list, realm):
|
|
|
|
for name in class_list:
|
|
|
|
if ZephyrClass.objects.filter(name=name, realm=realm):
|
|
|
|
# We're trying to create the same zephyr class twice!
|
|
|
|
raise
|
2012-09-07 19:24:54 +02:00
|
|
|
create_zephyr_class(name, realm)
|
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 + (
|
|
|
|
make_option('-n', '--num-zephyrs',
|
|
|
|
dest='num_zephyrs',
|
|
|
|
type='int',
|
2012-09-05 18:38:35 +02:00
|
|
|
default=120,
|
2012-09-04 20:26:45 +02:00
|
|
|
help='The number of zephyrs 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.'),
|
|
|
|
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.'),
|
|
|
|
|
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
|
|
|
usernames = ["othello", "iago", "prospero", "cordelia", "hamlet"]
|
|
|
|
class_list = ["Verona", "Denmark", "Scotland", "Venice", "Rome"]
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
|
|
|
for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient,
|
|
|
|
Realm, Subscription, Huddle, UserMessage]:
|
|
|
|
klass.objects.all().delete()
|
2012-09-05 21:49:56 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Create a test realm
|
|
|
|
realm = Realm(domain="humbughq.com")
|
|
|
|
realm.save()
|
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-11 19:20:20 +02:00
|
|
|
names = [("Othello, the Moor of Venice", "othello"), ("Iago", "iago"),
|
|
|
|
("Prospero from The Tempest", "prospero"),
|
|
|
|
("Cordelia Lear", "cordelia"), ("King Hamlet", "hamlet")]
|
|
|
|
create_users(names, realm)
|
2012-09-10 20:38:29 +02:00
|
|
|
|
|
|
|
# Create public classes.
|
|
|
|
create_classes(class_list, realm)
|
|
|
|
recipient_classes = [klass.type_id for klass in
|
|
|
|
Recipient.objects.filter(type=Recipient.CLASS)]
|
|
|
|
|
|
|
|
# Create subscriptions to classes
|
|
|
|
profiles = UserProfile.objects.all()
|
|
|
|
for i, profile in enumerate(profiles):
|
|
|
|
# Subscribe to some classes.
|
|
|
|
for recipient in recipient_classes[:int(len(recipient_classes) *
|
|
|
|
float(i)/len(profiles)) + 1]:
|
|
|
|
r = Recipient.objects.get(type=Recipient.CLASS, type_id=recipient)
|
|
|
|
new_subscription = Subscription(userprofile=profile,
|
|
|
|
recipient=r)
|
|
|
|
new_subscription.save()
|
|
|
|
else:
|
|
|
|
realm = Realm.objects.get(domain="humbughq.com")
|
|
|
|
recipient_classes = [klass.type_id for klass in
|
|
|
|
Recipient.objects.filter(type=Recipient.CLASS)]
|
|
|
|
|
|
|
|
# Extract a list of all users
|
|
|
|
users = [user.id for user in User.objects.all()]
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-05 18:38:35 +02:00
|
|
|
# Create several initial huddles
|
|
|
|
for i in range(0, options["num_huddles"]):
|
2012-09-10 20:38:29 +02:00
|
|
|
get_huddle(random.sample(users, random.randint(3, 4)))
|
2012-09-05 18:38:35 +02:00
|
|
|
|
|
|
|
# Create several initial pairs for personals
|
|
|
|
personals_pairs = []
|
|
|
|
for i in range(0, options["num_personals"]):
|
|
|
|
personals_pairs.append(random.sample(users, 2))
|
|
|
|
|
2012-09-07 20:14:13 +02:00
|
|
|
recipient_classes = [klass.type_id for klass in
|
|
|
|
Recipient.objects.filter(type=Recipient.CLASS)]
|
|
|
|
recipient_huddles = [h.type_id for h in Recipient.objects.filter(type=Recipient.HUDDLE)]
|
2012-09-05 18:38:35 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
huddle_members = {}
|
|
|
|
for h in recipient_huddles:
|
|
|
|
huddle_members[h] = [s.userprofile.id for s in
|
|
|
|
Subscription.objects.filter(recipient=h)]
|
2012-09-05 18:38:35 +02:00
|
|
|
|
2012-08-28 18:45:10 +02:00
|
|
|
# Create some test zephyrs, including:
|
|
|
|
# - multiple classes
|
|
|
|
# - multiple instances per class
|
|
|
|
# - multiple zephyrs per instance
|
|
|
|
# - both single and multi-line content
|
2012-09-05 18:38:35 +02:00
|
|
|
|
2012-08-28 18:45:10 +02:00
|
|
|
texts = file("zephyr/management/commands/test_zephyrs.txt", "r").readlines()
|
|
|
|
offset = 0
|
2012-09-04 20:26:45 +02:00
|
|
|
num_zephyrs = 0
|
2012-09-05 18:38:35 +02:00
|
|
|
random_max = 1000000
|
2012-09-05 19:38:48 +02:00
|
|
|
recipients = {}
|
2012-09-04 20:26:45 +02:00
|
|
|
while num_zephyrs < options["num_zephyrs"]:
|
2012-09-05 19:38:48 +02:00
|
|
|
saved_data = ''
|
2012-08-28 18:45:10 +02:00
|
|
|
new_zephyr = Zephyr()
|
|
|
|
length = random.randint(1, 5)
|
|
|
|
new_zephyr.content = "".join(texts[offset: offset + length])
|
|
|
|
offset += length
|
2012-09-04 20:26:45 +02:00
|
|
|
offset = offset % len(texts)
|
2012-09-05 18:38:35 +02:00
|
|
|
|
|
|
|
randkey = random.randint(1, random_max)
|
2012-09-05 19:38:48 +02:00
|
|
|
if (num_zephyrs > 0 and
|
|
|
|
random.randint(1, random_max) * 100. / random_max < options["stickyness"]):
|
|
|
|
# Use an old recipient
|
|
|
|
zephyr_type, recipient, saved_data = recipients[num_zephyrs - 1]
|
2012-09-07 20:14:13 +02:00
|
|
|
if zephyr_type == Recipient.PERSONAL:
|
2012-09-05 19:38:48 +02:00
|
|
|
personals_pair = saved_data
|
|
|
|
random.shuffle(personals_pair)
|
2012-09-07 20:14:13 +02:00
|
|
|
elif zephyr_type == Recipient.CLASS:
|
2012-09-05 19:38:48 +02:00
|
|
|
new_zephyr.instance = saved_data
|
|
|
|
new_zephyr.recipient = recipient
|
2012-09-07 20:14:13 +02:00
|
|
|
elif zephyr_type == Recipient.HUDDLE:
|
2012-09-05 19:38:48 +02:00
|
|
|
new_zephyr.recipient = recipient
|
|
|
|
elif (randkey <= random_max * options["percent_huddles"] / 100.):
|
2012-09-07 20:14:13 +02:00
|
|
|
zephyr_type = Recipient.HUDDLE
|
|
|
|
new_zephyr.recipient = Recipient.objects.get(type=Recipient.HUDDLE,
|
|
|
|
type_id=random.choice(recipient_huddles))
|
2012-09-05 18:38:35 +02:00
|
|
|
elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.):
|
2012-09-07 20:14:13 +02:00
|
|
|
zephyr_type = Recipient.PERSONAL
|
2012-09-05 21:49:56 +02:00
|
|
|
personals_pair = random.choice(personals_pairs)
|
2012-09-05 19:38:48 +02:00
|
|
|
random.shuffle(personals_pair)
|
2012-09-05 18:38:35 +02:00
|
|
|
elif (randkey <= random_max * 1.0):
|
2012-09-07 20:14:13 +02:00
|
|
|
zephyr_type = Recipient.CLASS
|
|
|
|
new_zephyr.recipient = Recipient.objects.get(type=Recipient.CLASS,
|
|
|
|
type_id=random.choice(recipient_classes))
|
|
|
|
|
|
|
|
if zephyr_type == Recipient.HUDDLE:
|
|
|
|
sender_id = random.choice(huddle_members[new_zephyr.recipient.type_id])
|
|
|
|
new_zephyr.sender = UserProfile.objects.get(id=sender_id)
|
|
|
|
elif zephyr_type == Recipient.PERSONAL:
|
|
|
|
new_zephyr.recipient = Recipient.objects.get(type=Recipient.PERSONAL,
|
|
|
|
type_id=personals_pair[0])
|
2012-09-05 19:38:48 +02:00
|
|
|
new_zephyr.sender = UserProfile.objects.get(id=personals_pair[1])
|
|
|
|
saved_data = personals_pair
|
2012-09-07 20:14:13 +02:00
|
|
|
elif zephyr_type == Recipient.CLASS:
|
2012-09-10 19:43:11 +02:00
|
|
|
zephyr_class = ZephyrClass.objects.get(id=new_zephyr.recipient.type_id)
|
2012-09-05 19:38:48 +02:00
|
|
|
# Pick a random subscriber to the class
|
2012-09-07 20:14:13 +02:00
|
|
|
new_zephyr.sender = random.choice(Subscription.objects.filter(
|
|
|
|
recipient=new_zephyr.recipient)).userprofile
|
2012-09-05 18:38:35 +02:00
|
|
|
new_zephyr.instance = zephyr_class.name + str(random.randint(1, 3))
|
2012-09-05 19:38:48 +02:00
|
|
|
saved_data = new_zephyr.instance
|
|
|
|
|
2012-08-28 23:08:38 +02:00
|
|
|
new_zephyr.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc)
|
2012-08-28 18:45:10 +02:00
|
|
|
new_zephyr.save()
|
2012-09-05 19:38:48 +02:00
|
|
|
|
|
|
|
recipients[num_zephyrs] = [zephyr_type, new_zephyr.recipient, saved_data]
|
2012-09-04 20:26:45 +02:00
|
|
|
num_zephyrs += 1
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
|
|
|
# Create internal users
|
2012-09-11 19:20:20 +02:00
|
|
|
internal_users = []
|
|
|
|
create_users(internal_users, realm)
|
2012-09-07 17:54:00 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
create_classes(subs_list, realm)
|
2012-09-07 17:54:00 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Now subscribe everyone to these classes
|
|
|
|
profiles = UserProfile.objects.all()
|
|
|
|
for cls in subs_list:
|
|
|
|
zephyr_class = ZephyrClass.objects.get(name=cls, realm=realm)
|
|
|
|
recipient = Recipient.objects.get(type=Recipient.CLASS, type_id=zephyr_class.id)
|
|
|
|
for i, profile in enumerate(profiles):
|
|
|
|
# Subscribe to some classes.
|
|
|
|
new_subscription = Subscription(userprofile=profile, recipient=recipient)
|
|
|
|
new_subscription.save()
|
2012-09-07 17:54:00 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
self.stdout.write("Successfully populated test database.\n")
|