diff --git a/zephyr/management/commands/clear_db.py b/zephyr/management/commands/clear_db.py index a9008fd218..995e201e50 100644 --- a/zephyr/management/commands/clear_db.py +++ b/zephyr/management/commands/clear_db.py @@ -1,14 +1,16 @@ from django.core.management.base import NoArgsCommand 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 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): - for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient]: + for klass in [Message, ZephyrClass, UserProfile, User, Recipient, + Realm, Subscription, Huddle, UserMessage]: klass.objects.all().delete() Session.objects.all().delete() diff --git a/zephyr/management/commands/populate_db.py b/zephyr/management/commands/populate_db.py index 617283f976..e813e69d0f 100644 --- a/zephyr/management/commands/populate_db.py +++ b/zephyr/management/commands/populate_db.py @@ -2,7 +2,7 @@ from django.core.management.base import BaseCommand from django.utils.timezone import utc 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, \ create_user, do_send_message, create_user_if_needed, create_class_if_needed from zephyr.lib.parallel import run_parallel @@ -97,7 +97,7 @@ class Command(BaseCommand): class_list = ["Verona", "Denmark", "Scotland", "Venice", "Rome"] if options["delete"]: - for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient, + for klass in [Message, ZephyrClass, UserProfile, User, Recipient, Realm, Subscription, Huddle, UserMessage]: klass.objects.all().delete() @@ -214,12 +214,12 @@ def get_recipient_by_id(rid): return Recipient.objects.get(id=rid) def restore_saved_zephyrs(): - old_zephyrs = file("all_zephyrs_log", "r").readlines() - for old_zephyr_json in old_zephyrs: - old_zephyr = simplejson.loads(old_zephyr_json.strip()) - new_zephyr = Zephyr() + old_messages = file("all_zephyrs_log", "r").readlines() + for old_message_json in old_messages: + old_message = simplejson.loads(old_message_json.strip()) + message = Message() - sender_email = old_zephyr["sender_email"] + sender_email = old_message["sender_email"] realm = None try: realm = Realm.objects.get(domain=sender_email.split('@')[1]) @@ -232,37 +232,37 @@ def restore_saved_zephyrs(): realm = Realm.objects.get(domain='mit.edu') create_user_if_needed(realm, sender_email, sender_email.split('@')[0], - old_zephyr["sender_full_name"], - old_zephyr["sender_short_name"]) - new_zephyr.sender = UserProfile.objects.get(user__email=old_zephyr["sender_email"]) + old_message["sender_full_name"], + old_message["sender_short_name"]) + message.sender = UserProfile.objects.get(user__email=old_message["sender_email"]) type_hash = {"class": Recipient.CLASS, "huddle": Recipient.HUDDLE, "personal": Recipient.PERSONAL} - new_zephyr.type = type_hash[old_zephyr["type"]] - new_zephyr.content = old_zephyr["content"] - new_zephyr.instance = old_zephyr["instance"] - new_zephyr.pub_date = datetime.datetime.utcfromtimestamp(float(old_zephyr["timestamp"])).replace(tzinfo=utc) + message.type = type_hash[old_message["type"]] + message.content = old_message["content"] + message.instance = old_message["instance"] + message.pub_date = datetime.datetime.utcfromtimestamp(float(old_message["timestamp"])).replace(tzinfo=utc) - if new_zephyr.type == Recipient.PERSONAL: - u = old_zephyr["recipient"][0] + if message.type == Recipient.PERSONAL: + u = old_message["recipient"][0] create_user_if_needed(realm, u["email"], u["email"].split('@')[0], u["full_name"], u["short_name"]) 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) - elif new_zephyr.type == Recipient.CLASS: - zephyr_class = create_class_if_needed(realm, old_zephyr["recipient"]) - new_zephyr.recipient = Recipient.objects.get(type=Recipient.CLASS, + elif message.type == Recipient.CLASS: + zephyr_class = create_class_if_needed(realm, old_message["recipient"]) + message.recipient = Recipient.objects.get(type=Recipient.CLASS, type_id=zephyr_class.id) - elif new_zephyr.type == Recipient.HUDDLE: - for u in old_zephyr["recipient"]: + elif message.type == Recipient.HUDDLE: + for u in old_message["recipient"]: create_user_if_needed(realm, u["email"], u["email"].split('@')[0], u["full_name"], u["short_name"]) target_huddle = get_huddle([UserProfile.objects.get(user__email=u["email"]).id - for u in old_zephyr["recipient"]]) - new_zephyr.recipient = Recipient.objects.get(type=Recipient.HUDDLE, + for u in old_message["recipient"]]) + message.recipient = Recipient.objects.get(type=Recipient.HUDDLE, type_id=target_huddle.id) else: 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: @@ -273,7 +273,7 @@ def restore_saved_zephyrs(): # - multiple zephyrs per instance # - both single and multi-line content def send_zephyrs(data): - (tot_zephyrs, personals_pairs, options, output) = data + (tot_messages, personals_pairs, options, output) = data from django.db import connection connection.close() 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 Subscription.objects.filter(recipient_id=h)] - num_zephyrs = 0 + num_messages = 0 random_max = 1000000 recipients = {} - while num_zephyrs < tot_zephyrs: + while num_messages < tot_messages: with transaction.commit_on_success(): saved_data = '' - new_zephyr = Zephyr() + message = Message() length = random.randint(1, 5) lines = (t.strip() for t in texts[offset: offset + length]) - new_zephyr.content = '\n'.join(lines) + message.content = '\n'.join(lines) offset += length offset = offset % len(texts) 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"]): # Use an old recipient - zephyr_type, recipient_id, saved_data = recipients[num_zephyrs - 1] - if zephyr_type == Recipient.PERSONAL: + message_type, recipient_id, saved_data = recipients[num_messages - 1] + if message_type == Recipient.PERSONAL: personals_pair = saved_data random.shuffle(personals_pair) - elif zephyr_type == Recipient.CLASS: - new_zephyr.instance = saved_data - new_zephyr.recipient = get_recipient_by_id(recipient_id) - elif zephyr_type == Recipient.HUDDLE: - new_zephyr.recipient = get_recipient_by_id(recipient_id) + elif message_type == Recipient.CLASS: + message.instance = saved_data + message.recipient = get_recipient_by_id(recipient_id) + elif message_type == Recipient.HUDDLE: + message.recipient = get_recipient_by_id(recipient_id) elif (randkey <= random_max * options["percent_huddles"] / 100.): - zephyr_type = Recipient.HUDDLE - new_zephyr.recipient = get_recipient_by_id(random.choice(recipient_huddles)) + message_type = Recipient.HUDDLE + message.recipient = get_recipient_by_id(random.choice(recipient_huddles)) 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) random.shuffle(personals_pair) elif (randkey <= random_max * 1.0): - zephyr_type = Recipient.CLASS - new_zephyr.recipient = get_recipient_by_id(random.choice(recipient_classes)) + message_type = Recipient.CLASS + message.recipient = get_recipient_by_id(random.choice(recipient_classes)) - if zephyr_type == Recipient.HUDDLE: - sender_id = random.choice(huddle_members[new_zephyr.recipient.id]) - new_zephyr.sender = get_user_profile_by_id(sender_id) - elif zephyr_type == Recipient.PERSONAL: - new_zephyr.recipient = Recipient.objects.get(type=Recipient.PERSONAL, + 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, 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 - elif zephyr_type == Recipient.CLASS: - zephyr_class = ZephyrClass.objects.get(id=new_zephyr.recipient.type_id) + elif message_type == Recipient.CLASS: + zephyr_class = ZephyrClass.objects.get(id=message.recipient.type_id) # Pick a random subscriber to the class - new_zephyr.sender = random.choice(Subscription.objects.filter( - recipient=new_zephyr.recipient)).userprofile - new_zephyr.instance = zephyr_class.name + str(random.randint(1, 3)) - saved_data = new_zephyr.instance + message.sender = random.choice(Subscription.objects.filter( + recipient=message.recipient)).userprofile + message.instance = zephyr_class.name + str(random.randint(1, 3)) + saved_data = message.instance - new_zephyr.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc) - do_send_message(new_zephyr) + message.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc) + do_send_message(message) - recipients[num_zephyrs] = [zephyr_type, new_zephyr.recipient.id, saved_data] - num_zephyrs += 1 - return tot_zephyrs + recipients[num_messages] = [message_type, message.recipient.id, saved_data] + num_messages += 1 + return tot_messages diff --git a/zephyr/models.py b/zephyr/models.py index ed6201e42e..2c1e9d7bb2 100644 --- a/zephyr/models.py +++ b/zephyr/models.py @@ -193,7 +193,7 @@ class Recipient(models.Model): display_recipient = get_display_recipient(self) return "" % (display_recipient, self.type_id, self.type) -class Zephyr(models.Model): +class Message(models.Model): sender = models.ForeignKey(UserProfile) recipient = models.ForeignKey(Recipient) instance = models.CharField(max_length=30) @@ -202,7 +202,7 @@ class Zephyr(models.Model): def __repr__(self): display_recipient = get_display_recipient(self.recipient) - return "" % (display_recipient, self.instance, self.sender) + return "" % (display_recipient, self.instance, self.sender) def __str__(self): return self.__repr__() @@ -238,7 +238,7 @@ class Zephyr(models.Model): class UserMessage(models.Model): 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 # since this table will be an unpleasant one to do schema changes # on later diff --git a/zephyr/tests.py b/zephyr/tests.py index 0cc29569a7..a495d75546 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -3,7 +3,7 @@ from django.test import TestCase from django.utils.timezone import utc 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 from zephyr.views import get_updates from zephyr.decorator import TornadoAsyncException @@ -56,7 +56,7 @@ class AuthedTestCase(TestCase): recipient = ZephyrClass.objects.get(name=recipient_name, realm=sender.realm) recipient = Recipient.objects.get(type_id=recipient.id, type=zephyr_type) 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) 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] 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): """ @@ -394,7 +394,7 @@ class GetUpdatesTest(AuthedTestCase): user = User.objects.get(email="hamlet@humbughq.com") 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: self.assertTrue(zephyr in correct_zephyrs) self.assertTrue(zephyr.id > 1) @@ -407,12 +407,12 @@ class GetUpdatesTest(AuthedTestCase): 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. """ self.login("hamlet@humbughq.com", "hamlet") 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 = [] def callback(data): @@ -433,7 +433,7 @@ class GetUpdatesTest(AuthedTestCase): user = User.objects.get(email="hamlet@humbughq.com") 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: self.assertTrue(zephyr in correct_zephyrs) self.assertTrue(zephyr.id > 1) diff --git a/zephyr/tests/generate-fixtures b/zephyr/tests/generate-fixtures index 83b7ba54c8..b852a0747f 100755 --- a/zephyr/tests/generate-fixtures +++ b/zephyr/tests/generate-fixtures @@ -3,4 +3,4 @@ mkdir -p zephyr/fixtures rm -f zephyr/fixtures/zephyrdb.test python manage.py syncdb --noinput --settings=humbug.test-settings 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 diff --git a/zephyr/views.py b/zephyr/views.py index 5774564414..1abd4376bf 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -10,7 +10,7 @@ from django.utils.timezone import utc from django.core.exceptions import ValidationError from django.contrib.auth.views import login as django_login_page 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, \ create_user, do_send_message, mit_sync_table, create_user_if_needed, \ create_class_if_needed, PreregistrationUser @@ -146,7 +146,7 @@ def home(request): return HttpResponseRedirect('accounts/home/') 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: 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) 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: # User has no messages yet @@ -299,10 +299,10 @@ def is_super_user_api(request): def already_sent_forged_message(request): email = strip_html(request.POST['sender']).lower() - if Zephyr.objects.filter(sender__user__email=email, - content=request.POST['content'], - 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)): + if Message.objects.filter(sender__user__email=email, + content=request.POST['content'], + 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)): return True return False @@ -390,21 +390,21 @@ def zephyr_backend(request, user_profile, sender): else: return json_error("Invalid message type") - new_zephyr = Zephyr() - new_zephyr.sender = UserProfile.objects.get(user=sender) - new_zephyr.content = strip_html(request.POST['content']) - new_zephyr.recipient = recipient + message = Message() + message.sender = UserProfile.objects.get(user=sender) + message.content = strip_html(request.POST['content']) + message.recipient = recipient 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: - # Forged zephyrs come with a timestamp - new_zephyr.pub_date = datetime.datetime.utcfromtimestamp(float(request.POST['time'])).replace(tzinfo=utc) + # Forged messages come with a timestamp + message.pub_date = datetime.datetime.utcfromtimestamp(float(request.POST['time'])).replace(tzinfo=utc) 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 # 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()