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-08-29 17:50:44 +02:00
|
|
|
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, Subscription
|
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-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',
|
|
|
|
default=100,
|
|
|
|
help='The number of zephyrs to create.'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, **options):
|
2012-08-29 17:50:44 +02:00
|
|
|
for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient, Subscription]:
|
2012-08-28 18:45:10 +02:00
|
|
|
klass.objects.all().delete()
|
|
|
|
|
2012-08-31 16:58:09 +02:00
|
|
|
# Create test Users (UserProfiles are automatically created,
|
|
|
|
# as are subscriptions to the ability to receive personals).
|
2012-08-29 17:50:44 +02:00
|
|
|
usernames = ["othello", "iago", "prospero", "cordelia", "hamlet"]
|
|
|
|
for username in usernames:
|
2012-08-28 18:45:10 +02:00
|
|
|
u = User.objects.create_user(username=username, password=username)
|
|
|
|
u.save()
|
|
|
|
|
2012-08-29 17:50:44 +02:00
|
|
|
# Create public classes.
|
2012-08-28 18:45:10 +02:00
|
|
|
for name in ["Verona", "Denmark", "Scotland", "Venice", "Rome"]:
|
2012-08-30 20:18:59 +02:00
|
|
|
new_class = ZephyrClass(name=name)
|
2012-08-28 18:45:10 +02:00
|
|
|
new_class.save()
|
2012-08-28 21:27:42 +02:00
|
|
|
|
2012-08-30 20:18:59 +02:00
|
|
|
recipient = Recipient(user_or_class=new_class.pk, type="class")
|
2012-08-28 21:27:42 +02:00
|
|
|
recipient.save()
|
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
|
|
|
|
users = [user.id for user in User.objects.all()]
|
2012-08-29 17:50:44 +02:00
|
|
|
recipient_classes = [klass.id for klass in Recipient.objects.filter(type="class")]
|
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
|
|
|
|
while num_zephyrs < options["num_zephyrs"]:
|
2012-08-28 18:45:10 +02:00
|
|
|
new_zephyr = Zephyr()
|
|
|
|
new_zephyr.sender = UserProfile.objects.get(id=random.choice(users))
|
|
|
|
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-08-29 17:50:44 +02:00
|
|
|
new_zephyr.recipient = Recipient.objects.get(id=random.choice(recipient_classes))
|
2012-08-31 16:58:09 +02:00
|
|
|
zephyr_class = ZephyrClass.objects.get(pk=new_zephyr.recipient.user_or_class)
|
2012-08-28 21:27:42 +02:00
|
|
|
new_zephyr.instance = zephyr_class.name + str(random.randint(1, 3))
|
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-04 20:26:45 +02:00
|
|
|
num_zephyrs += 1
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-08-31 16:58:09 +02:00
|
|
|
# Create subscriptions
|
|
|
|
profiles = UserProfile.objects.all()
|
2012-08-29 17:50:44 +02:00
|
|
|
for i, profile in enumerate(profiles):
|
|
|
|
# Subscribe to some classes.
|
|
|
|
for recipient in recipient_classes[:int(len(recipient_classes) * float(i)/len(profiles)) + 1]:
|
2012-08-30 20:18:59 +02:00
|
|
|
new_subscription = Subscription(userprofile_id=profile,
|
|
|
|
recipient_id=Recipient.objects.get(id=recipient))
|
2012-08-29 17:50:44 +02:00
|
|
|
new_subscription.save()
|
|
|
|
|
2012-08-28 22:57:45 +02:00
|
|
|
self.stdout.write("Successfully populated test database.\n")
|