[schema] Rename Zephyr => Message in the schema.

(imported from commit 4f402f150c45d2097d6b16943935e6e370b4f8d8)
This commit is contained in:
Tim Abbott
2012-10-03 15:05:48 -04:00
parent 6bce5f95b0
commit 1ab81cff92
6 changed files with 92 additions and 90 deletions

View File

@@ -1,14 +1,16 @@
from django.core.management.base import NoArgsCommand from django.core.management.base import NoArgsCommand
from django.contrib.auth.models import User from django.contrib.auth.models import User
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient from zephyr.models import Message, UserProfile, ZephyrClass, Recipient, \
Subscription, Huddle, Realm, UserMessage
from django.contrib.sessions.models import Session from django.contrib.sessions.models import Session
class Command(NoArgsCommand): class Command(NoArgsCommand):
help = "Clear only tables we change: zephyr + sessions" help = "Clear only tables we change: messages, accounts + sessions"
def handle_noargs(self, **options): def handle_noargs(self, **options):
for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient]: for klass in [Message, ZephyrClass, UserProfile, User, Recipient,
Realm, Subscription, Huddle, UserMessage]:
klass.objects.all().delete() klass.objects.all().delete()
Session.objects.all().delete() Session.objects.all().delete()

View File

@@ -2,7 +2,7 @@ from django.core.management.base import BaseCommand
from django.utils.timezone import utc from django.utils.timezone import utc
from django.contrib.auth.models import User from django.contrib.auth.models import User
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, \ from zephyr.models import Message, UserProfile, ZephyrClass, Recipient, \
Subscription, Huddle, get_huddle, Realm, UserMessage, get_user_profile_by_id, \ Subscription, Huddle, get_huddle, Realm, UserMessage, get_user_profile_by_id, \
create_user, do_send_message, create_user_if_needed, create_class_if_needed create_user, do_send_message, create_user_if_needed, create_class_if_needed
from zephyr.lib.parallel import run_parallel from zephyr.lib.parallel import run_parallel
@@ -97,7 +97,7 @@ class Command(BaseCommand):
class_list = ["Verona", "Denmark", "Scotland", "Venice", "Rome"] class_list = ["Verona", "Denmark", "Scotland", "Venice", "Rome"]
if options["delete"]: if options["delete"]:
for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient, for klass in [Message, ZephyrClass, UserProfile, User, Recipient,
Realm, Subscription, Huddle, UserMessage]: Realm, Subscription, Huddle, UserMessage]:
klass.objects.all().delete() klass.objects.all().delete()
@@ -214,12 +214,12 @@ def get_recipient_by_id(rid):
return Recipient.objects.get(id=rid) return Recipient.objects.get(id=rid)
def restore_saved_zephyrs(): def restore_saved_zephyrs():
old_zephyrs = file("all_zephyrs_log", "r").readlines() old_messages = file("all_zephyrs_log", "r").readlines()
for old_zephyr_json in old_zephyrs: for old_message_json in old_messages:
old_zephyr = simplejson.loads(old_zephyr_json.strip()) old_message = simplejson.loads(old_message_json.strip())
new_zephyr = Zephyr() message = Message()
sender_email = old_zephyr["sender_email"] sender_email = old_message["sender_email"]
realm = None realm = None
try: try:
realm = Realm.objects.get(domain=sender_email.split('@')[1]) realm = Realm.objects.get(domain=sender_email.split('@')[1])
@@ -232,37 +232,37 @@ def restore_saved_zephyrs():
realm = Realm.objects.get(domain='mit.edu') realm = Realm.objects.get(domain='mit.edu')
create_user_if_needed(realm, sender_email, sender_email.split('@')[0], create_user_if_needed(realm, sender_email, sender_email.split('@')[0],
old_zephyr["sender_full_name"], old_message["sender_full_name"],
old_zephyr["sender_short_name"]) old_message["sender_short_name"])
new_zephyr.sender = UserProfile.objects.get(user__email=old_zephyr["sender_email"]) message.sender = UserProfile.objects.get(user__email=old_message["sender_email"])
type_hash = {"class": Recipient.CLASS, "huddle": Recipient.HUDDLE, "personal": Recipient.PERSONAL} type_hash = {"class": Recipient.CLASS, "huddle": Recipient.HUDDLE, "personal": Recipient.PERSONAL}
new_zephyr.type = type_hash[old_zephyr["type"]] message.type = type_hash[old_message["type"]]
new_zephyr.content = old_zephyr["content"] message.content = old_message["content"]
new_zephyr.instance = old_zephyr["instance"] message.instance = old_message["instance"]
new_zephyr.pub_date = datetime.datetime.utcfromtimestamp(float(old_zephyr["timestamp"])).replace(tzinfo=utc) message.pub_date = datetime.datetime.utcfromtimestamp(float(old_message["timestamp"])).replace(tzinfo=utc)
if new_zephyr.type == Recipient.PERSONAL: if message.type == Recipient.PERSONAL:
u = old_zephyr["recipient"][0] u = old_message["recipient"][0]
create_user_if_needed(realm, u["email"], u["email"].split('@')[0], create_user_if_needed(realm, u["email"], u["email"].split('@')[0],
u["full_name"], u["short_name"]) u["full_name"], u["short_name"])
user_profile = UserProfile.objects.get(user__email=u["email"]) user_profile = UserProfile.objects.get(user__email=u["email"])
new_zephyr.recipient = Recipient.objects.get(type=Recipient.PERSONAL, message.recipient = Recipient.objects.get(type=Recipient.PERSONAL,
type_id=user_profile.id) type_id=user_profile.id)
elif new_zephyr.type == Recipient.CLASS: elif message.type == Recipient.CLASS:
zephyr_class = create_class_if_needed(realm, old_zephyr["recipient"]) zephyr_class = create_class_if_needed(realm, old_message["recipient"])
new_zephyr.recipient = Recipient.objects.get(type=Recipient.CLASS, message.recipient = Recipient.objects.get(type=Recipient.CLASS,
type_id=zephyr_class.id) type_id=zephyr_class.id)
elif new_zephyr.type == Recipient.HUDDLE: elif message.type == Recipient.HUDDLE:
for u in old_zephyr["recipient"]: for u in old_message["recipient"]:
create_user_if_needed(realm, u["email"], u["email"].split('@')[0], create_user_if_needed(realm, u["email"], u["email"].split('@')[0],
u["full_name"], u["short_name"]) u["full_name"], u["short_name"])
target_huddle = get_huddle([UserProfile.objects.get(user__email=u["email"]).id target_huddle = get_huddle([UserProfile.objects.get(user__email=u["email"]).id
for u in old_zephyr["recipient"]]) for u in old_message["recipient"]])
new_zephyr.recipient = Recipient.objects.get(type=Recipient.HUDDLE, message.recipient = Recipient.objects.get(type=Recipient.HUDDLE,
type_id=target_huddle.id) type_id=target_huddle.id)
else: else:
raise raise
do_send_message(new_zephyr, synced_from_mit=True, no_log=True) do_send_message(message, synced_from_mit=True, no_log=True)
# Create some test zephyrs, including: # Create some test zephyrs, including:
@@ -273,7 +273,7 @@ def restore_saved_zephyrs():
# - multiple zephyrs per instance # - multiple zephyrs per instance
# - both single and multi-line content # - both single and multi-line content
def send_zephyrs(data): def send_zephyrs(data):
(tot_zephyrs, personals_pairs, options, output) = data (tot_messages, personals_pairs, options, output) = data
from django.db import connection from django.db import connection
connection.close() connection.close()
texts = file("zephyr/management/commands/test_zephyrs.txt", "r").readlines() texts = file("zephyr/management/commands/test_zephyrs.txt", "r").readlines()
@@ -288,62 +288,62 @@ def send_zephyrs(data):
huddle_members[h] = [s.userprofile.id for s in huddle_members[h] = [s.userprofile.id for s in
Subscription.objects.filter(recipient_id=h)] Subscription.objects.filter(recipient_id=h)]
num_zephyrs = 0 num_messages = 0
random_max = 1000000 random_max = 1000000
recipients = {} recipients = {}
while num_zephyrs < tot_zephyrs: while num_messages < tot_messages:
with transaction.commit_on_success(): with transaction.commit_on_success():
saved_data = '' saved_data = ''
new_zephyr = Zephyr() message = Message()
length = random.randint(1, 5) length = random.randint(1, 5)
lines = (t.strip() for t in texts[offset: offset + length]) lines = (t.strip() for t in texts[offset: offset + length])
new_zephyr.content = '\n'.join(lines) message.content = '\n'.join(lines)
offset += length offset += length
offset = offset % len(texts) offset = offset % len(texts)
randkey = random.randint(1, random_max) randkey = random.randint(1, random_max)
if (num_zephyrs > 0 and if (num_messages > 0 and
random.randint(1, random_max) * 100. / random_max < options["stickyness"]): random.randint(1, random_max) * 100. / random_max < options["stickyness"]):
# Use an old recipient # Use an old recipient
zephyr_type, recipient_id, saved_data = recipients[num_zephyrs - 1] message_type, recipient_id, saved_data = recipients[num_messages - 1]
if zephyr_type == Recipient.PERSONAL: if message_type == Recipient.PERSONAL:
personals_pair = saved_data personals_pair = saved_data
random.shuffle(personals_pair) random.shuffle(personals_pair)
elif zephyr_type == Recipient.CLASS: elif message_type == Recipient.CLASS:
new_zephyr.instance = saved_data message.instance = saved_data
new_zephyr.recipient = get_recipient_by_id(recipient_id) message.recipient = get_recipient_by_id(recipient_id)
elif zephyr_type == Recipient.HUDDLE: elif message_type == Recipient.HUDDLE:
new_zephyr.recipient = get_recipient_by_id(recipient_id) message.recipient = get_recipient_by_id(recipient_id)
elif (randkey <= random_max * options["percent_huddles"] / 100.): elif (randkey <= random_max * options["percent_huddles"] / 100.):
zephyr_type = Recipient.HUDDLE message_type = Recipient.HUDDLE
new_zephyr.recipient = get_recipient_by_id(random.choice(recipient_huddles)) message.recipient = get_recipient_by_id(random.choice(recipient_huddles))
elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.): elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.):
zephyr_type = Recipient.PERSONAL message_type = Recipient.PERSONAL
personals_pair = random.choice(personals_pairs) personals_pair = random.choice(personals_pairs)
random.shuffle(personals_pair) random.shuffle(personals_pair)
elif (randkey <= random_max * 1.0): elif (randkey <= random_max * 1.0):
zephyr_type = Recipient.CLASS message_type = Recipient.CLASS
new_zephyr.recipient = get_recipient_by_id(random.choice(recipient_classes)) message.recipient = get_recipient_by_id(random.choice(recipient_classes))
if zephyr_type == Recipient.HUDDLE: if message_type == Recipient.HUDDLE:
sender_id = random.choice(huddle_members[new_zephyr.recipient.id]) sender_id = random.choice(huddle_members[message.recipient.id])
new_zephyr.sender = get_user_profile_by_id(sender_id) message.sender = get_user_profile_by_id(sender_id)
elif zephyr_type == Recipient.PERSONAL: elif message_type == Recipient.PERSONAL:
new_zephyr.recipient = Recipient.objects.get(type=Recipient.PERSONAL, message.recipient = Recipient.objects.get(type=Recipient.PERSONAL,
type_id=personals_pair[0]) type_id=personals_pair[0])
new_zephyr.sender = get_user_profile_by_id(personals_pair[1]) message.sender = get_user_profile_by_id(personals_pair[1])
saved_data = personals_pair saved_data = personals_pair
elif zephyr_type == Recipient.CLASS: elif message_type == Recipient.CLASS:
zephyr_class = ZephyrClass.objects.get(id=new_zephyr.recipient.type_id) zephyr_class = ZephyrClass.objects.get(id=message.recipient.type_id)
# Pick a random subscriber to the class # Pick a random subscriber to the class
new_zephyr.sender = random.choice(Subscription.objects.filter( message.sender = random.choice(Subscription.objects.filter(
recipient=new_zephyr.recipient)).userprofile recipient=message.recipient)).userprofile
new_zephyr.instance = zephyr_class.name + str(random.randint(1, 3)) message.instance = zephyr_class.name + str(random.randint(1, 3))
saved_data = new_zephyr.instance saved_data = message.instance
new_zephyr.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc) message.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc)
do_send_message(new_zephyr) do_send_message(message)
recipients[num_zephyrs] = [zephyr_type, new_zephyr.recipient.id, saved_data] recipients[num_messages] = [message_type, message.recipient.id, saved_data]
num_zephyrs += 1 num_messages += 1
return tot_zephyrs return tot_messages

View File

@@ -193,7 +193,7 @@ class Recipient(models.Model):
display_recipient = get_display_recipient(self) display_recipient = get_display_recipient(self)
return "<Recipient: %s (%d, %s)>" % (display_recipient, self.type_id, self.type) return "<Recipient: %s (%d, %s)>" % (display_recipient, self.type_id, self.type)
class Zephyr(models.Model): class Message(models.Model):
sender = models.ForeignKey(UserProfile) sender = models.ForeignKey(UserProfile)
recipient = models.ForeignKey(Recipient) recipient = models.ForeignKey(Recipient)
instance = models.CharField(max_length=30) instance = models.CharField(max_length=30)
@@ -202,7 +202,7 @@ class Zephyr(models.Model):
def __repr__(self): def __repr__(self):
display_recipient = get_display_recipient(self.recipient) display_recipient = get_display_recipient(self.recipient)
return "<Zephyr: %s / %s / %r>" % (display_recipient, self.instance, self.sender) return "<Message: %s / %s / %r>" % (display_recipient, self.instance, self.sender)
def __str__(self): def __str__(self):
return self.__repr__() return self.__repr__()
@@ -238,7 +238,7 @@ class Zephyr(models.Model):
class UserMessage(models.Model): class UserMessage(models.Model):
user_profile = models.ForeignKey(UserProfile) user_profile = models.ForeignKey(UserProfile)
message = models.ForeignKey(Zephyr) message = models.ForeignKey(Message)
# We're not using the archived field for now, but create it anyway # We're not using the archived field for now, but create it anyway
# since this table will be an unpleasant one to do schema changes # since this table will be an unpleasant one to do schema changes
# on later # on later

View File

@@ -3,7 +3,7 @@ from django.test import TestCase
from django.utils.timezone import utc from django.utils.timezone import utc
from django.db.models import Q from django.db.models import Q
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, Subscription, \ from zephyr.models import Message, UserProfile, ZephyrClass, Recipient, Subscription, \
filter_by_subscriptions, Realm, do_send_message filter_by_subscriptions, Realm, do_send_message
from zephyr.views import get_updates from zephyr.views import get_updates
from zephyr.decorator import TornadoAsyncException from zephyr.decorator import TornadoAsyncException
@@ -56,7 +56,7 @@ class AuthedTestCase(TestCase):
recipient = ZephyrClass.objects.get(name=recipient_name, realm=sender.realm) recipient = ZephyrClass.objects.get(name=recipient_name, realm=sender.realm)
recipient = Recipient.objects.get(type_id=recipient.id, type=zephyr_type) recipient = Recipient.objects.get(type_id=recipient.id, type=zephyr_type)
pub_date = datetime.datetime.utcnow().replace(tzinfo=utc) pub_date = datetime.datetime.utcnow().replace(tzinfo=utc)
do_send_message(Zephyr(sender=sender, recipient=recipient, instance="test", pub_date=pub_date), do_send_message(Message(sender=sender, recipient=recipient, instance="test", pub_date=pub_date),
synced_from_mit=True) synced_from_mit=True)
def users_subscribed_to_class(self, class_name, realm_domain): def users_subscribed_to_class(self, class_name, realm_domain):
@@ -68,7 +68,7 @@ class AuthedTestCase(TestCase):
return [subscription.userprofile.user for subscription in subscriptions] return [subscription.userprofile.user for subscription in subscriptions]
def zephyr_stream(self, user): def zephyr_stream(self, user):
return filter_by_subscriptions(Zephyr.objects.all(), user) return filter_by_subscriptions(Message.objects.all(), user)
def assert_json_success(self, result): def assert_json_success(self, result):
""" """
@@ -394,7 +394,7 @@ class GetUpdatesTest(AuthedTestCase):
user = User.objects.get(email="hamlet@humbughq.com") user = User.objects.get(email="hamlet@humbughq.com")
def callback(zephyrs): def callback(zephyrs):
correct_zephyrs = filter_by_subscriptions(Zephyr.objects.all(), user) correct_zephyrs = filter_by_subscriptions(Message.objects.all(), user)
for zephyr in zephyrs: for zephyr in zephyrs:
self.assertTrue(zephyr in correct_zephyrs) self.assertTrue(zephyr in correct_zephyrs)
self.assertTrue(zephyr.id > 1) self.assertTrue(zephyr.id > 1)
@@ -407,12 +407,12 @@ class GetUpdatesTest(AuthedTestCase):
def test_beyond_last_zephyr(self): def test_beyond_last_zephyr(self):
""" """
If your last_received zephyr is greater than the greatest Zephyr ID, you If your last_received zephyr is greater than the greatest Message ID, you
don't get any new zephyrs. don't get any new zephyrs.
""" """
self.login("hamlet@humbughq.com", "hamlet") self.login("hamlet@humbughq.com", "hamlet")
user = User.objects.get(email="hamlet@humbughq.com") user = User.objects.get(email="hamlet@humbughq.com")
last_received = max(zephyr.id for zephyr in Zephyr.objects.all()) + 100 last_received = max(zephyr.id for zephyr in Message.objects.all()) + 100
zephyrs = [] zephyrs = []
def callback(data): def callback(data):
@@ -433,7 +433,7 @@ class GetUpdatesTest(AuthedTestCase):
user = User.objects.get(email="hamlet@humbughq.com") user = User.objects.get(email="hamlet@humbughq.com")
def callback(zephyrs): def callback(zephyrs):
correct_zephyrs = filter_by_subscriptions(Zephyr.objects.all(), user) correct_zephyrs = filter_by_subscriptions(Message.objects.all(), user)
for zephyr in zephyrs: for zephyr in zephyrs:
self.assertTrue(zephyr in correct_zephyrs) self.assertTrue(zephyr in correct_zephyrs)
self.assertTrue(zephyr.id > 1) self.assertTrue(zephyr.id > 1)

View File

@@ -3,4 +3,4 @@ mkdir -p zephyr/fixtures
rm -f zephyr/fixtures/zephyrdb.test rm -f zephyr/fixtures/zephyrdb.test
python manage.py syncdb --noinput --settings=humbug.test-settings python manage.py syncdb --noinput --settings=humbug.test-settings
python manage.py populate_db --settings=humbug.test-settings -n20 python manage.py populate_db --settings=humbug.test-settings -n20
python manage.py dumpdata --settings=humbug.test-settings auth.User zephyr.UserProfile zephyr.ZephyrClass zephyr.Recipient zephyr.Subscription zephyr.Zephyr zephyr.Huddle zephyr.Realm zephyr.UserMessage > zephyr/fixtures/zephyrs.json python manage.py dumpdata --settings=humbug.test-settings auth.User zephyr.UserProfile zephyr.ZephyrClass zephyr.Recipient zephyr.Subscription zephyr.Message zephyr.Huddle zephyr.Realm zephyr.UserMessage > zephyr/fixtures/zephyrs.json

View File

@@ -10,7 +10,7 @@ from django.utils.timezone import utc
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.contrib.auth.views import login as django_login_page from django.contrib.auth.views import login as django_login_page
from django.contrib.auth.models import User from django.contrib.auth.models import User
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Subscription, \ from zephyr.models import Message, UserProfile, ZephyrClass, Subscription, \
Recipient, get_display_recipient, get_huddle, Realm, \ Recipient, get_display_recipient, get_huddle, Realm, \
create_user, do_send_message, mit_sync_table, create_user_if_needed, \ create_user, do_send_message, mit_sync_table, create_user_if_needed, \
create_class_if_needed, PreregistrationUser create_class_if_needed, PreregistrationUser
@@ -146,7 +146,7 @@ def home(request):
return HttpResponseRedirect('accounts/home/') return HttpResponseRedirect('accounts/home/')
user_profile = UserProfile.objects.get(user=request.user) user_profile = UserProfile.objects.get(user=request.user)
zephyrs = Zephyr.objects.filter(usermessage__user_profile=user_profile) zephyrs = Message.objects.filter(usermessage__user_profile=user_profile)
if user_profile.pointer == -1 and zephyrs: if user_profile.pointer == -1 and zephyrs:
user_profile.pointer = min([zephyr.id for zephyr in zephyrs]) user_profile.pointer = min([zephyr.id for zephyr in zephyrs])
@@ -216,7 +216,7 @@ def return_messages_immediately(request, handler, user_profile, **kwargs):
failures = int(failures) failures = int(failures)
where = 'bottom' where = 'bottom'
query = Zephyr.objects.filter(usermessage__user_profile = user_profile).order_by('id') query = Message.objects.filter(usermessage__user_profile = user_profile).order_by('id')
if last == -1: if last == -1:
# User has no messages yet # User has no messages yet
@@ -299,10 +299,10 @@ def is_super_user_api(request):
def already_sent_forged_message(request): def already_sent_forged_message(request):
email = strip_html(request.POST['sender']).lower() email = strip_html(request.POST['sender']).lower()
if Zephyr.objects.filter(sender__user__email=email, if Message.objects.filter(sender__user__email=email,
content=request.POST['content'], content=request.POST['content'],
pub_date__gt=datetime.datetime.utcfromtimestamp(float(request.POST['time']) - 10).replace(tzinfo=utc), pub_date__gt=datetime.datetime.utcfromtimestamp(float(request.POST['time']) - 10).replace(tzinfo=utc),
pub_date__lt=datetime.datetime.utcfromtimestamp(float(request.POST['time']) + 10).replace(tzinfo=utc)): pub_date__lt=datetime.datetime.utcfromtimestamp(float(request.POST['time']) + 10).replace(tzinfo=utc)):
return True return True
return False return False
@@ -390,21 +390,21 @@ def zephyr_backend(request, user_profile, sender):
else: else:
return json_error("Invalid message type") return json_error("Invalid message type")
new_zephyr = Zephyr() message = Message()
new_zephyr.sender = UserProfile.objects.get(user=sender) message.sender = UserProfile.objects.get(user=sender)
new_zephyr.content = strip_html(request.POST['content']) message.content = strip_html(request.POST['content'])
new_zephyr.recipient = recipient message.recipient = recipient
if zephyr_type_name == 'class': if zephyr_type_name == 'class':
new_zephyr.instance = strip_html(request.POST['instance']) message.instance = strip_html(request.POST['instance'])
if 'time' in request.POST: if 'time' in request.POST:
# Forged zephyrs come with a timestamp # Forged messages come with a timestamp
new_zephyr.pub_date = datetime.datetime.utcfromtimestamp(float(request.POST['time'])).replace(tzinfo=utc) message.pub_date = datetime.datetime.utcfromtimestamp(float(request.POST['time'])).replace(tzinfo=utc)
else: else:
new_zephyr.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc) message.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc)
# To avoid message loops, we must pass whether the message was # To avoid message loops, we must pass whether the message was
# synced from MIT zephyr here. # synced from MIT zephyr here.
do_send_message(new_zephyr, synced_from_mit = 'time' in request.POST) do_send_message(message, synced_from_mit = 'time' in request.POST)
return json_success() return json_success()