From d8ade6de5b55a77ddaa0ae0b47fae8c066965cf8 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Fri, 7 Sep 2012 13:24:54 -0400 Subject: [PATCH] Add create_zephyr_class helper. (imported from commit 993fbb799b706e402ae212330e4abbe28bf84ee9) --- zephyr/management/commands/populate_db.py | 9 +++------ zephyr/models.py | 8 ++++++++ zephyr/tests.py | 4 +++- zephyr/views.py | 8 ++------ 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/zephyr/management/commands/populate_db.py b/zephyr/management/commands/populate_db.py index d45e6d2b2c..5d1e3aec7a 100644 --- a/zephyr/management/commands/populate_db.py +++ b/zephyr/management/commands/populate_db.py @@ -3,7 +3,8 @@ from django.utils.timezone import utc from django.contrib.auth.models import User from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, \ - Subscription, Huddle, get_huddle, Realm, create_user_profile, UserMessage + Subscription, Huddle, get_huddle, Realm, create_user_profile, UserMessage, \ + create_zephyr_class from zephyr.zephyr_mirror import subs_list import datetime @@ -24,11 +25,7 @@ def create_classes(class_list, realm): if ZephyrClass.objects.filter(name=name, realm=realm): # We're trying to create the same zephyr class twice! raise - new_class = ZephyrClass(name=name, realm=realm) - new_class.save() - - recipient = Recipient(type_id=new_class.pk, type="class") - recipient.save() + create_zephyr_class(name, realm) class Command(BaseCommand): help = "Populate a test database" diff --git a/zephyr/models.py b/zephyr/models.py index 06881c4852..64fb2b6fe7 100644 --- a/zephyr/models.py +++ b/zephyr/models.py @@ -84,6 +84,14 @@ class ZephyrClass(models.Model): def __str__(self): return self.__repr__() +def create_zephyr_class(name, realm): + zephyr_class = ZephyrClass(name=name, realm=realm) + zephyr_class.save() + + recipient = Recipient(type_id=zephyr_class.id, type="class") + recipient.save() + return (zephyr_class, recipient) + class Recipient(models.Model): type_id = models.IntegerField() type = models.CharField(max_length=30) diff --git a/zephyr/tests.py b/zephyr/tests.py index d1e4105898..8bd7c42261 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -338,5 +338,7 @@ class ZephyrPOSTTest(AuthedTestCase): Sending a zephyr of unknown type returns error JSON. """ self.login("hamlet", "hamlet") - result = self.client.post("/zephyr/", {"type": "invalid type"}) + result = self.client.post("/zephyr/", {"type": "invalid type", + "new_zephyr": "Test message", + "recipient": "othello"}) self.assert_json_error(result, "Invalid zephyr type") diff --git a/zephyr/views.py b/zephyr/views.py index d8bcbc8713..865344f8a8 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -10,7 +10,7 @@ from django.utils.timezone import utc from django.contrib.auth.models import User from zephyr.models import Zephyr, UserProfile, ZephyrClass, Subscription, \ Recipient, filter_by_subscriptions, get_display_recipient, get_huddle, \ - create_user_profile, Realm, UserMessage + create_user_profile, Realm, UserMessage, create_zephyr_class from zephyr.forms import RegistrationForm import tornado.web @@ -287,11 +287,7 @@ def add_subscriptions(request): zephyr_class = zephyr_class[0] recipient = Recipient.objects.get(type_id=zephyr_class.pk, type="class") else: - zephyr_class = ZephyrClass(name=sub_name, realm=user_profile.realm) - zephyr_class.save() - - recipient = Recipient(type_id=zephyr_class.pk, type="class") - recipient.save() + (_, recipient) = create_zephyr_class(sub_name, user_profile.realm) subscription = Subscription.objects.filter(userprofile=user_profile, recipient=recipient)