Factor out subscription retrieval code into another function.

(imported from commit 6a66a4feb03990e11c98cd8666d1a7bb97299987)
This commit is contained in:
Luke Faraone
2013-01-18 12:25:36 -05:00
parent 3d25fbce49
commit b78d154370

View File

@@ -1099,16 +1099,22 @@ def json_stream_exists(request, user_profile, stream=POST):
active=True).exists() active=True).exists()
return json_success(result) 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) stream = get_stream(stream_name, user_profile.realm)
if not stream: if not stream:
raise JsonableError("Invalid stream %s" % (stream.name,)) raise JsonableError("Invalid stream %s" % (stream.name,))
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM) recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
subscription = Subscription.objects.filter(user_profile=user_profile, subscription = Subscription.objects.filter(user_profile=user_profile,
recipient=recipient, active=True) recipient=recipient, active=True)
if not subscription.exists(): if not subscription.exists():
raise JsonableError("Not subscribed to stream %s" % (stream_name,)) 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]) stream_color, _ = StreamColor.objects.get_or_create(subscription=subscription[0])
# TODO: sanitize color. # TODO: sanitize color.
stream_color.color = color stream_color.color = color