Add Stream.num_subscribers

(imported from commit 9b79ab84516c3634ef6795ef2d90db37553cc0d1)
This commit is contained in:
Steve Howell 2013-09-19 16:55:08 -04:00
parent 5ae5e3f881
commit 4b75dd0d85
2 changed files with 14 additions and 1 deletions

View File

@ -295,6 +295,14 @@ class Stream(models.Model):
type=Recipient.STREAM) type=Recipient.STREAM)
return (stream, recipient) return (stream, recipient)
def num_subscribers(self):
return Subscription.objects.filter(
recipient__type=Recipient.STREAM,
recipient__type_id=self.id,
user_profile__is_active=True,
active=True
).count()
def valid_stream_name(name): def valid_stream_name(name):
return name != "" return name != ""

View File

@ -1126,6 +1126,7 @@ class SubscriptionAPITest(AuthedTestCase):
def test_multi_user_subscription(self): def test_multi_user_subscription(self):
email1 = 'cordelia@zulip.com' email1 = 'cordelia@zulip.com'
email2 = 'iago@zulip.com' email2 = 'iago@zulip.com'
realm = Realm.objects.get(domain="zulip.com")
streams_to_sub = ['multi_user_stream'] streams_to_sub = ['multi_user_stream']
events = [] events = []
with tornado_redirected_to_list(events): with tornado_redirected_to_list(events):
@ -1143,6 +1144,9 @@ class SubscriptionAPITest(AuthedTestCase):
set([email1, email2]) set([email1, email2])
) )
stream = get_stream('multi_user_stream', realm)
self.assertEqual(stream.num_subscribers(), 2)
# Now add ourselves # Now add ourselves
events = [] events = []
with tornado_redirected_to_list(events): with tornado_redirected_to_list(events):
@ -1168,12 +1172,13 @@ class SubscriptionAPITest(AuthedTestCase):
self.assertEqual(add_peer_event['event']['op'], 'peer_add') self.assertEqual(add_peer_event['event']['op'], 'peer_add')
self.assertEqual(add_peer_event['event']['user_email'], self.test_email) self.assertEqual(add_peer_event['event']['user_email'], self.test_email)
stream = get_stream('multi_user_stream', realm)
self.assertEqual(stream.num_subscribers(), 3)
# Finally, add othello, exercising the do_add_subscription() code path. # Finally, add othello, exercising the do_add_subscription() code path.
events = [] events = []
email3 = 'othello@zulip.com' email3 = 'othello@zulip.com'
user_profile = get_user_profile_by_email(email3) user_profile = get_user_profile_by_email(email3)
realm = Realm.objects.get(domain="zulip.com")
stream = get_stream('multi_user_stream', realm) stream = get_stream('multi_user_stream', realm)
with tornado_redirected_to_list(events): with tornado_redirected_to_list(events):
do_add_subscription(user_profile, stream) do_add_subscription(user_profile, stream)