streams: Eliminate get_subscription_or_die helper function.

This commit is contained in:
Tim Abbott 2017-01-29 16:45:00 -08:00
parent 0af34ee710
commit 2cf223470d
2 changed files with 5 additions and 16 deletions

View File

@ -930,7 +930,7 @@ class SubscriptionPropertiesTest(ZulipTestCase):
"stream": stream_name, "stream": stream_name,
"value": False}])}) "value": False}])})
self.assert_json_error(result, "Invalid stream %s" % (stream_name,)) self.assert_json_error(result, "Invalid stream name '%s'" % (stream_name,))
def test_set_invalid_property(self): def test_set_invalid_property(self):
# type: () -> None # type: () -> None

View File

@ -18,6 +18,7 @@ from zerver.lib.actions import bulk_remove_subscriptions, \
do_change_stream_description, do_get_streams, do_make_stream_private, \ do_change_stream_description, do_get_streams, do_make_stream_private, \
do_remove_default_stream, get_topic_history_for_stream do_remove_default_stream, get_topic_history_for_stream
from zerver.lib.response import json_success, json_error, json_response from zerver.lib.response import json_success, json_error, json_response
from zerver.lib.streams import access_stream_by_id, access_stream_by_name
from zerver.lib.validator import check_string, check_list, check_dict, \ from zerver.lib.validator import check_string, check_list, check_dict, \
check_bool, check_variable_type check_bool, check_variable_type
from zerver.models import UserProfile, Stream, Realm, Subscription, \ from zerver.models import UserProfile, Stream, Realm, Subscription, \
@ -507,20 +508,6 @@ def json_get_stream_id(request, user_profile, stream=REQ()):
return json_error(_("No such stream name")) return json_error(_("No such stream name"))
return json_success({'stream_id': stream_id}) return json_success({'stream_id': stream_id})
def get_subscription_or_die(stream_name, user_profile):
# type: (Text, UserProfile) -> Subscription
stream = get_stream(stream_name, user_profile.realm)
if not stream:
raise JsonableError(_("Invalid stream %s") % (stream_name,))
recipient = get_recipient(Recipient.STREAM, stream.id)
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
@authenticated_json_view @authenticated_json_view
@has_request_variables @has_request_variables
def json_subscription_property(request, user_profile, subscription_data=REQ( def json_subscription_property(request, user_profile, subscription_data=REQ(
@ -557,7 +544,9 @@ def json_subscription_property(request, user_profile, subscription_data=REQ(
if property not in property_converters: if property not in property_converters:
return json_error(_("Unknown subscription property: %s") % (property,)) return json_error(_("Unknown subscription property: %s") % (property,))
sub = get_subscription_or_die(stream_name, user_profile)[0] (stream, recipient, sub) = access_stream_by_name(user_profile, stream_name)
if sub is None:
return json_error(_("Not subscribed to stream %s") % (stream_name,))
property_conversion = property_converters[property](property, value) property_conversion = property_converters[property](property, value)
if property_conversion: if property_conversion: