Add create_zephyr_class helper.

(imported from commit 993fbb799b706e402ae212330e4abbe28bf84ee9)
This commit is contained in:
Tim Abbott 2012-09-07 13:24:54 -04:00
parent 4848ae3c48
commit d8ade6de5b
4 changed files with 16 additions and 13 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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")

View File

@ -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)