gather_subscriptions: Use .values() for fetching all the user's subs.

(imported from commit 1f27841527bb5f49fab347071e40d289151fa2c5)
This commit is contained in:
Tim Abbott
2013-10-02 12:45:10 -04:00
parent 1eeb808a79
commit 356d33cfa0

View File

@@ -1450,13 +1450,17 @@ def decode_email_address(email):
# transformed. # transformed.
return re.sub("%\d{4}", lambda x: unichr(int(x.group(0)[1:])), email) return re.sub("%\d{4}", lambda x: unichr(int(x.group(0)[1:])), email)
# In general, it's better to avoid using .values() because it makes
# the code pretty ugly, but in this case, it has significant
# performance impact for loading / for users with large numbers of
# subscriptions, so it's worth optimizing.
def gather_subscriptions(user_profile): def gather_subscriptions(user_profile):
# For now, don't display subscriptions for private messages. sub_dicts = Subscription.objects.select_related("recipient").filter(
subs = Subscription.objects.select_related("recipient").filter(
user_profile = user_profile, user_profile = user_profile,
recipient__type = Recipient.STREAM) recipient__type = Recipient.STREAM).values(
"recipient__type_id", "in_home_view", "color", "notifications", "active")
stream_ids = [sub.recipient.type_id for sub in subs] stream_ids = [sub["recipient__type_id"] for sub in sub_dicts]
stream_hash = {} stream_hash = {}
for stream in Stream.objects.select_related("realm").filter(id__in=stream_ids): for stream in Stream.objects.select_related("realm").filter(id__in=stream_ids):
@@ -1465,27 +1469,27 @@ def gather_subscriptions(user_profile):
subscribed = [] subscribed = []
unsubscribed = [] unsubscribed = []
streams = [stream_hash[sub.recipient.type_id] for sub in subs] streams = [stream_hash[sub["recipient__type_id"]] for sub in sub_dicts]
subscriber_map = bulk_get_subscriber_emails(streams, user_profile) subscriber_map = bulk_get_subscriber_emails(streams, user_profile)
for sub in subs: for sub in sub_dicts:
stream = stream_hash[sub.recipient.type_id] stream = stream_hash[sub["recipient__type_id"]]
subscribers = subscriber_map[stream.id] subscribers = subscriber_map[stream.id]
# Important: don't show the subscribers if the stream is invite only # Important: don't show the subscribers if the stream is invite only
# and this user isn't on it anymore. # and this user isn't on it anymore.
if stream.invite_only and not sub.active: if stream.invite_only and not sub["active"]:
subscribers = None subscribers = None
stream_dict = {'name': stream.name, stream_dict = {'name': stream.name,
'in_home_view': sub.in_home_view, 'in_home_view': sub["in_home_view"],
'invite_only': stream.invite_only, 'invite_only': stream.invite_only,
'color': sub.color, 'color': sub["color"],
'notifications': sub.notifications, 'notifications': sub["notifications"],
'email_address': encode_email_address(stream)} 'email_address': encode_email_address(stream)}
if subscribers is not None: if subscribers is not None:
stream_dict['subscribers'] = subscribers stream_dict['subscribers'] = subscribers
if sub.active: if sub["active"]:
subscribed.append(stream_dict) subscribed.append(stream_dict)
else: else:
unsubscribed.append(stream_dict) unsubscribed.append(stream_dict)