[schema] Add the realm to the ZepyhrClass objects.

(imported from commit 8189ef27f2feef0c92f9c06a9243c085e36a66f5)
This commit is contained in:
Tim Abbott 2012-09-05 16:30:50 -04:00
parent b761bf030b
commit ef25dffb92
4 changed files with 16 additions and 11 deletions

View File

@ -69,7 +69,7 @@ class Command(BaseCommand):
# Create public classes.
for name in ["Verona", "Denmark", "Scotland", "Venice", "Rome"]:
new_class = ZephyrClass(name=name)
new_class = ZephyrClass(name=name, realm=realm)
new_class.save()
recipient = Recipient(type_id=new_class.pk, type="class")

View File

@ -74,6 +74,7 @@ def create_user_profile(user, realm):
class ZephyrClass(models.Model):
name = models.CharField(max_length=30)
realm = models.ForeignKey(Realm)
def __repr__(self):
return "<ZephyrClass: %s>" % (self.name,)

View File

@ -3,7 +3,8 @@ 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, filter_by_subscriptions
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, Subscription, \
filter_by_subscriptions, Realm
import datetime
import os
@ -24,13 +25,14 @@ class AuthedTestCase(TestCase):
if zephyr_type == "personal":
recipient = UserProfile.objects.get(user=User.objects.get(username=recipient_name))
else:
recipient = ZephyrClass.objects.get(name=recipient_name)
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)
Zephyr(sender=sender, recipient=recipient, instance="test", pub_date=pub_date).save()
def users_subscribed_to_class(self, class_name):
zephyr_class = ZephyrClass.objects.get(name=class_name)
def users_subscribed_to_class(self, class_name, realm_domain):
realm = Realm.objects.get(domain=realm_domain)
zephyr_class = ZephyrClass.objects.get(name=class_name, realm=realm)
recipient = Recipient.objects.get(type_id=zephyr_class.id, type="class")
subscriptions = Subscription.objects.filter(recipient=recipient)
@ -182,7 +184,7 @@ class ClassZephyrsTest(AuthedTestCase):
If you send a zephyr to a class, everyone subscribed to the class
receives the zephyr.
"""
subscribers = self.users_subscribed_to_class("Scotland")
subscribers = self.users_subscribed_to_class("Scotland", "humbughq.com")
old_subscriber_zephyrs = []
for subscriber in subscribers:
old_subscriber_zephyrs.append(len(self.zephyr_stream(subscriber)))

View File

@ -137,14 +137,16 @@ def zephyr(request):
# TODO: Do something
pass
user_profile = UserProfile.objects.get(user=request.user)
zephyr_type = request.POST["type"]
if zephyr_type == 'class':
class_name = request.POST['class']
if ZephyrClass.objects.filter(name=class_name):
my_class = ZephyrClass.objects.get(name=class_name)
if ZephyrClass.objects.filter(name=class_name, realm=user_profile.realm):
my_class = ZephyrClass.objects.get(name=class_name, realm=user_profile.realm)
else:
my_class = ZephyrClass()
my_class.name = class_name
my.realm = user_profile.realm
my_class.save()
try:
recipient = Recipient.objects.get(type_id=my_class.id, type="class")
@ -213,7 +215,7 @@ def manage_subscriptions(request):
unsubs = request.POST.getlist('subscription')
for sub_name in unsubs:
zephyr_class = ZephyrClass.objects.get(name=sub_name)
zephyr_class = ZephyrClass.objects.get(name=sub_name, realm=user_profile.realm)
recipient = Recipient.objects.get(type_id=zephyr_class.id, type="class")
subscription = Subscription.objects.get(
userprofile=user_profile, recipient=recipient)
@ -234,11 +236,11 @@ def add_subscriptions(request):
return HttpResponseRedirect(reverse('zephyr.views.subscriptions'))
for sub_name in new_subs.split(","):
zephyr_class = ZephyrClass.objects.filter(name=sub_name)
zephyr_class = ZephyrClass.objects.filter(name=sub_name, realm=user_profile.realm)
if zephyr_class:
zephyr_class = zephyr_class[0]
else:
zephyr_class = ZephyrClass(name=sub_name)
zephyr_class = ZephyrClass(name=sub_name, realm=user_profile.realm)
zephyr_class.save()
recipient = Recipient.objects.filter(type_id=zephyr_class.pk, type="class")