From b78d154370a993fead251f2d352d057ad735a0d1 Mon Sep 17 00:00:00 2001 From: Luke Faraone Date: Fri, 18 Jan 2013 12:25:36 -0500 Subject: [PATCH] Factor out subscription retrieval code into another function. (imported from commit 6a66a4feb03990e11c98cd8666d1a7bb97299987) --- zephyr/views.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/zephyr/views.py b/zephyr/views.py index b1f424f0fe..5a3f819442 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -1099,16 +1099,22 @@ def json_stream_exists(request, user_profile, stream=POST): active=True).exists() return json_success(result) -def set_stream_color(user_profile, stream_name, color): +def get_subscription_or_die(stream_name, user_profile): stream = get_stream(stream_name, user_profile.realm) if not stream: raise JsonableError("Invalid stream %s" % (stream.name,)) recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM) subscription = Subscription.objects.filter(user_profile=user_profile, recipient=recipient, active=True) + if not subscription.exists(): raise JsonableError("Not subscribed to stream %s" % (stream_name,)) + return subscription + +def set_stream_color(user_profile, stream_name, color): + subscription = get_subscription_or_die(stream_name, user_profile) + stream_color, _ = StreamColor.objects.get_or_create(subscription=subscription[0]) # TODO: sanitize color. stream_color.color = color