diff --git a/zephyr/management/commands/populate_db.py b/zephyr/management/commands/populate_db.py index 1c44d157b5..bbbfc6a5bf 100644 --- a/zephyr/management/commands/populate_db.py +++ b/zephyr/management/commands/populate_db.py @@ -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") diff --git a/zephyr/models.py b/zephyr/models.py index d7c3107c4c..4c81a38e1f 100644 --- a/zephyr/models.py +++ b/zephyr/models.py @@ -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 "" % (self.name,) diff --git a/zephyr/tests.py b/zephyr/tests.py index a2bc624a6f..440b225b3b 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -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))) diff --git a/zephyr/views.py b/zephyr/views.py index d1442bba5d..0594a30f52 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -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")