def update_stream_backend( request: HttpRequest, user_profile: UserProfile, stream_id: int, description: Optional[str]=REQ(validator=check_capped_string( Stream.MAX_DESCRIPTION_LENGTH), default=None), is_private: Optional[bool]=REQ(validator=check_bool, default=None), is_announcement_only: Optional[bool]=REQ(validator=check_bool, default=None), history_public_to_subscribers: Optional[bool]=REQ(validator=check_bool, default=None), new_name: Optional[str]=REQ(validator=check_string, default=None), ) -> HttpResponse: # We allow realm administrators to to update the stream name and # description even for private streams. stream = access_stream_for_delete_or_update(user_profile, stream_id) if description is not None: do_change_stream_description(stream, description) if new_name is not None: new_name = new_name.strip() if stream.name == new_name: return json_error(_("Stream already has that name!")) if stream.name.lower() != new_name.lower(): # Check that the stream name is available (unless we are # are only changing the casing of the stream name). check_stream_name_available(user_profile.realm, new_name) do_rename_stream(stream, new_name) if is_announcement_only is not None: do_change_stream_announcement_only(stream, is_announcement_only) # But we require even realm administrators to be actually # subscribed to make a private stream public. if is_private is not None: (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) do_change_stream_invite_only(stream, is_private, history_public_to_subscribers) return json_success()
def update_stream_backend( request: HttpRequest, user_profile: UserProfile, stream_id: int, description: Optional[Text]=REQ(validator=check_string, default=None), is_private: Optional[bool]=REQ(validator=check_bool, default=None), new_name: Optional[Text]=REQ(validator=check_string, default=None), ) -> HttpResponse: # We allow realm administrators to to update the stream name and # description even for private streams. stream = access_stream_for_delete_or_update(user_profile, stream_id) if description is not None: do_change_stream_description(stream, description) if new_name is not None: new_name = new_name.strip() if stream.name == new_name: return json_error(_("Stream already has that name!")) if stream.name.lower() != new_name.lower(): # Check that the stream name is available (unless we are # are only changing the casing of the stream name). check_stream_name_available(user_profile.realm, new_name) do_rename_stream(stream, new_name) # But we require even realm administrators to be actually # subscribed to make a private stream public. if is_private is not None: (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) do_change_stream_invite_only(stream, is_private) return json_success()
def handle(self, *args: Any, **options: str) -> None: realm = self.get_realm(options) assert realm is not None # Should be ensured by parser old_name = options['old_name'] new_name = options['new_name'] stream = get_stream(old_name, realm) do_rename_stream(stream, new_name, self.user_profile)
def handle(self, *args: Any, **options: str) -> None: realm = self.get_realm(options) assert realm is not None # Should be ensured by parser old_name = options['old_name'] new_name = options['new_name'] stream = get_stream(old_name, realm) do_rename_stream(stream, new_name)
def update_stream_backend(request, user_profile, stream_name, description=REQ(validator=check_string, default=None), new_name=REQ(validator=check_string, default=None)): # type: (HttpRequest, UserProfile, text_type, Optional[text_type], Optional[text_type]) -> HttpResponse if description is not None: do_change_stream_description(user_profile.realm, stream_name, description) if stream_name is not None and new_name is not None: do_rename_stream(user_profile.realm, stream_name, new_name) return json_success()
def handle(self, *args: Any, **options: str) -> None: realm = self.get_realm(options) assert realm is not None # Should be ensured by parser old_name = options['old_name'] new_name = options['new_name'] encoding = sys.getfilesystemencoding() stream = get_stream(force_text(old_name, encoding), realm) do_rename_stream(stream, force_text(new_name, encoding))
def update_stream_backend(request, user_profile, stream_name, description=REQ(validator=check_string, default=None), new_name=REQ(validator=check_string, default=None)): # type: (HttpRequest, UserProfile, text_type, Optional[text_type], Optional[text_type]) -> HttpResponse if description is not None: do_change_stream_description(user_profile.realm, stream_name, description) if stream_name is not None and new_name is not None: do_rename_stream(user_profile.realm, stream_name, new_name) return json_success({})
def handle(self, *args, **options): # type: (*Any, **str) -> None realm = self.get_realm(options) old_name = options['old_name'] new_name = options['new_name'] encoding = sys.getfilesystemencoding() stream = get_stream(force_text(old_name, encoding), realm) do_rename_stream(stream, force_text(new_name, encoding))
def update_stream_backend( request: HttpRequest, user_profile: UserProfile, stream_id: int, description: Optional[str]=REQ(validator=check_capped_string( Stream.MAX_DESCRIPTION_LENGTH), default=None), is_private: Optional[bool]=REQ(validator=check_bool, default=None), is_announcement_only: Optional[bool]=REQ(validator=check_bool, default=None), stream_post_policy: Optional[int]=REQ(validator=check_int_in( Stream.STREAM_POST_POLICY_TYPES), default=None), history_public_to_subscribers: Optional[bool]=REQ(validator=check_bool, default=None), new_name: Optional[str]=REQ(validator=check_string, default=None), message_retention_days: Optional[Union[int, str]]=REQ(validator=check_string_or_int, default=None), ) -> HttpResponse: # We allow realm administrators to to update the stream name and # description even for private streams. stream = access_stream_for_delete_or_update(user_profile, stream_id) if message_retention_days is not None: if not user_profile.is_realm_owner: raise OrganizationOwnerRequired() user_profile.realm.ensure_not_on_limited_plan() message_retention_days_value = parse_message_retention_days( message_retention_days, Stream.MESSAGE_RETENTION_SPECIAL_VALUES_MAP) do_change_stream_message_retention_days(stream, message_retention_days_value) if description is not None: if '\n' in description: # We don't allow newline characters in stream descriptions. description = description.replace("\n", " ") do_change_stream_description(stream, description) if new_name is not None: new_name = new_name.strip() if stream.name == new_name: return json_error(_("Stream already has that name!")) if stream.name.lower() != new_name.lower(): # Check that the stream name is available (unless we are # are only changing the casing of the stream name). check_stream_name_available(user_profile.realm, new_name) do_rename_stream(stream, new_name, user_profile) if is_announcement_only is not None: # is_announcement_only is a legacy way to specify # stream_post_policy. We can probably just delete this code, # since we're not aware of clients that used it, but we're # keeping it for backwards-compatibility for now. stream_post_policy = Stream.STREAM_POST_POLICY_EVERYONE if is_announcement_only: stream_post_policy = Stream.STREAM_POST_POLICY_ADMINS if stream_post_policy is not None: do_change_stream_post_policy(stream, stream_post_policy) # But we require even realm administrators to be actually # subscribed to make a private stream public. if is_private is not None: (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) do_change_stream_invite_only(stream, is_private, history_public_to_subscribers) return json_success()
def setUp(self) -> None: super().setUp() stream = self.subscribe(self.test_user, self.STREAM_NAME) # In actual webhook tests, we will not need to use stream id. # We assign stream id to STREAM_NAME for testing URL configuration only. self.STREAM_NAME = str(stream.id) do_rename_stream(stream, "helloworld_renamed", self.test_user) self.url = self.build_webhook_url()
def handle(self, *args, **options): # type: (*Any, **str) -> None domain = options["domain"] old_name = options["old_name"] new_name = options["new_name"] encoding = sys.getfilesystemencoding() try: realm = get_realm(domain) except Realm.DoesNotExist: print("Unknown domain %s" % (domain,)) exit(1) do_rename_stream(realm, old_name.decode(encoding), new_name.decode(encoding))
def test_rename_stream(self) -> None: now = timezone_now() user = self.example_user('hamlet') stream = self.make_stream('test', user.realm) old_name = stream.name do_rename_stream(stream, 'updated name', user) self.assertEqual(RealmAuditLog.objects.filter( realm=user.realm, event_type=RealmAuditLog.STREAM_NAME_CHANGED, event_time__gte=now, acting_user=user, modified_stream=stream, extra_data=ujson.dumps({ RealmAuditLog.OLD_VALUE: old_name, RealmAuditLog.NEW_VALUE: 'updated name' })).count(), 1) self.assertEqual(stream.name, 'updated name')
def handle(self, *args, **options): domain = options['domain'] old_name = options['old_name'] new_name = options['new_name'] encoding = sys.getfilesystemencoding() try: realm = get_realm(domain) except Realm.DoesNotExist: print("Unknown domain %s" % (domain, )) exit(1) do_rename_stream(realm, old_name.decode(encoding), new_name.decode(encoding))
def handle(self, *args, **options): domain = options['domain'] old_name = options['old_name'] new_name = options['new_name'] encoding = sys.getfilesystemencoding() try: realm = get_realm(domain) except Realm.DoesNotExist: print("Unknown domain %s" % (domain,)) exit(1) do_rename_stream(realm, old_name.decode(encoding), new_name.decode(encoding))
def handle(self, *args, **options): # type: (*Any, **str) -> None string_id = options['string_id'] old_name = options['old_name'] new_name = options['new_name'] encoding = sys.getfilesystemencoding() realm = get_realm(force_text(string_id, encoding)) if realm is None: print("Unknown subdomain or string_id %s" % (string_id,)) exit(1) do_rename_stream(realm, force_text(old_name, encoding), force_text(new_name, encoding))
def test_rename_stream(self): realm = get_realm('zulip.com') stream, _ = create_stream_if_needed(realm, 'old_name') new_name = u'stream with a brand new name' self.subscribe_to_stream(self.user_profile.email, stream.name) action = lambda: do_rename_stream(realm, stream.name, new_name) events = self.do_test(action) schema_checker = check_dict([ ('type', equals('stream')), ('op', equals('update')), ('property', equals('email_address')), ('value', check_string), ('name', equals('old_name')), ]) error = schema_checker('events[0]', events[0]) self.assert_on_error(error) schema_checker = check_dict([ ('type', equals('stream')), ('op', equals('update')), ('property', equals('name')), ('value', equals(new_name)), ('name', equals('old_name')), ]) error = schema_checker('events[1]', events[1]) self.assert_on_error(error)
def update_stream_backend(request, user_profile, stream_id, description=REQ(validator=check_string, default=None), is_private=REQ(validator=check_bool, default=None), new_name=REQ(validator=check_string, default=None)): # type: (HttpRequest, UserProfile, int, Optional[Text], Optional[bool], Optional[Text]) -> HttpResponse stream = get_and_validate_stream_by_id(stream_id, user_profile.realm) stream_name = stream.name if description is not None: do_change_stream_description(user_profile.realm, stream_name, description) if stream_name is not None and new_name is not None: do_rename_stream(user_profile.realm, stream_name, new_name) if is_private is not None: if is_private: do_make_stream_private(user_profile.realm, stream_name) else: do_make_stream_public(user_profile, user_profile.realm, stream_name) return json_success()
def update_stream_backend(request, user_profile, stream_id, description=REQ(validator=check_string, default=None), is_private=REQ(validator=check_bool, default=None), new_name=REQ(validator=check_string, default=None)): # type: (HttpRequest, UserProfile, int, Optional[Text], Optional[bool], Optional[Text]) -> HttpResponse (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) if description is not None: do_change_stream_description(stream, description) if new_name is not None: new_name = new_name.strip() # Will raise if the new name has invalid characters. if stream.name.lower() == new_name.lower(): return json_error(_("Stream already has that name!")) check_stream_name_available(user_profile.realm, new_name) do_rename_stream(stream, new_name) if is_private is not None: do_change_stream_invite_only(stream, is_private) return json_success()
def update_stream_backend(request, user_profile, stream_name, description=REQ(validator=check_string, default=None), is_private=REQ(validator=check_bool, default=None), new_name=REQ(validator=check_string, default=None)): # type: (HttpRequest, UserProfile, Text, Optional[Text], Optional[bool], Optional[Text]) -> HttpResponse if description is not None: do_change_stream_description(user_profile.realm, stream_name, description) if stream_name is not None and new_name is not None: do_rename_stream(user_profile.realm, stream_name, new_name) if is_private is not None: if is_private: do_make_stream_private(user_profile.realm, stream_name) else: do_make_stream_public(user_profile, user_profile.realm, stream_name) return json_success()
def test_rename_stream(self) -> None: now = timezone_now() user = self.example_user("hamlet") stream = self.make_stream("test", user.realm) old_name = stream.name do_rename_stream(stream, "updated name", user) self.assertEqual( RealmAuditLog.objects.filter( realm=user.realm, event_type=RealmAuditLog.STREAM_NAME_CHANGED, event_time__gte=now, acting_user=user, modified_stream=stream, extra_data=orjson.dumps( {RealmAuditLog.OLD_VALUE: old_name, RealmAuditLog.NEW_VALUE: "updated name"} ).decode(), ).count(), 1, ) self.assertEqual(stream.name, "updated name")
def update_stream_backend(request, user_profile, stream_id, description=REQ(validator=check_string, default=None), is_private=REQ(validator=check_bool, default=None), new_name=REQ(validator=check_string, default=None)): # type: (HttpRequest, UserProfile, int, Optional[Text], Optional[bool], Optional[Text]) -> HttpResponse (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) if description is not None: do_change_stream_description(stream, description) if new_name is not None: new_name = new_name.strip() if stream.name == new_name: return json_error(_("Stream already has that name!")) if stream.name.lower() != new_name.lower(): # Check that the stream name is available (unless we are # are only changing the casing of the stream name). check_stream_name_available(user_profile.realm, new_name) do_rename_stream(stream, new_name) if is_private is not None: do_change_stream_invite_only(stream, is_private) return json_success()
def update_stream_backend( request: HttpRequest, user_profile: UserProfile, stream_id: int, description: Optional[str]=REQ(validator=check_capped_string( Stream.MAX_DESCRIPTION_LENGTH), default=None), is_private: Optional[bool]=REQ(validator=check_bool, default=None), is_announcement_only: Optional[bool]=REQ(validator=check_bool, default=None), history_public_to_subscribers: Optional[bool]=REQ(validator=check_bool, default=None), new_name: Optional[str]=REQ(validator=check_string, default=None), ) -> HttpResponse: # We allow realm administrators to to update the stream name and # description even for private streams. stream = access_stream_for_delete_or_update(user_profile, stream_id) if description is not None: if '\n' in description: # We don't allow newline characters in stream descriptions. description = description.replace("\n", " ") do_change_stream_description(stream, description) if new_name is not None: new_name = new_name.strip() if stream.name == new_name: return json_error(_("Stream already has that name!")) if stream.name.lower() != new_name.lower(): # Check that the stream name is available (unless we are # are only changing the casing of the stream name). check_stream_name_available(user_profile.realm, new_name) do_rename_stream(stream, new_name, user_profile) if is_announcement_only is not None: do_change_stream_announcement_only(stream, is_announcement_only) # But we require even realm administrators to be actually # subscribed to make a private stream public. if is_private is not None: (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) do_change_stream_invite_only(stream, is_private, history_public_to_subscribers) return json_success()
def update_stream_backend( request: HttpRequest, user_profile: UserProfile, stream_id: int, description: Optional[Text] = REQ(validator=check_string, default=None), is_private: Optional[bool] = REQ(validator=check_bool, default=None), new_name: Optional[Text] = REQ(validator=check_string, default=None), ) -> HttpResponse: (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) if description is not None: do_change_stream_description(stream, description) if new_name is not None: new_name = new_name.strip() if stream.name == new_name: return json_error(_("Stream already has that name!")) if stream.name.lower() != new_name.lower(): # Check that the stream name is available (unless we are # are only changing the casing of the stream name). check_stream_name_available(user_profile.realm, new_name) do_rename_stream(stream, new_name) if is_private is not None: do_change_stream_invite_only(stream, is_private) return json_success()
def json_rename_stream(request, user_profile, old_name=REQ(), new_name=REQ()): # type: (HttpRequest, UserProfile, str, str) -> HttpResponse do_rename_stream(user_profile.realm, old_name, new_name) return json_success()
def update_stream_backend( request: HttpRequest, user_profile: UserProfile, stream_id: int, description: Optional[str] = REQ(str_validator=check_capped_string( Stream.MAX_DESCRIPTION_LENGTH), default=None), is_private: Optional[bool] = REQ(json_validator=check_bool, default=None), is_announcement_only: Optional[bool] = REQ(json_validator=check_bool, default=None), stream_post_policy: Optional[int] = REQ(json_validator=check_int_in( Stream.STREAM_POST_POLICY_TYPES), default=None), history_public_to_subscribers: Optional[bool] = REQ( json_validator=check_bool, default=None), is_web_public: Optional[bool] = REQ(json_validator=check_bool, default=None), new_name: Optional[str] = REQ(default=None), message_retention_days: Optional[Union[int, str]] = REQ( json_validator=check_string_or_int, default=None), ) -> HttpResponse: # We allow realm administrators to to update the stream name and # description even for private streams. (stream, sub) = access_stream_for_delete_or_update(user_profile, stream_id) if message_retention_days is not None: if not user_profile.is_realm_owner: raise OrganizationOwnerRequired() user_profile.realm.ensure_not_on_limited_plan() new_message_retention_days_value = parse_message_retention_days( message_retention_days, Stream.MESSAGE_RETENTION_SPECIAL_VALUES_MAP) do_change_stream_message_retention_days( stream, user_profile, new_message_retention_days_value) if description is not None: if "\n" in description: # We don't allow newline characters in stream descriptions. description = description.replace("\n", " ") do_change_stream_description(stream, description, acting_user=user_profile) if new_name is not None: new_name = new_name.strip() if stream.name == new_name: raise JsonableError(_("Stream already has that name!")) if stream.name.lower() != new_name.lower(): # Check that the stream name is available (unless we are # are only changing the casing of the stream name). check_stream_name_available(user_profile.realm, new_name) do_rename_stream(stream, new_name, user_profile) if is_announcement_only is not None: # is_announcement_only is a legacy way to specify # stream_post_policy. We can probably just delete this code, # since we're not aware of clients that used it, but we're # keeping it for backwards-compatibility for now. stream_post_policy = Stream.STREAM_POST_POLICY_EVERYONE if is_announcement_only: stream_post_policy = Stream.STREAM_POST_POLICY_ADMINS if stream_post_policy is not None: do_change_stream_post_policy(stream, stream_post_policy, acting_user=user_profile) # But we require even realm administrators to be actually # subscribed to make a private stream public. if is_private is not None: default_stream_ids = { s.id for s in get_default_streams_for_realm(stream.realm_id) } (stream, sub) = access_stream_by_id(user_profile, stream_id) if is_private and stream.id in default_stream_ids: raise JsonableError(_("Default streams cannot be made private.")) if is_web_public: # Enforce restrictions on creating web-public streams. if not user_profile.realm.web_public_streams_enabled(): raise JsonableError(_("Web-public streams are not enabled.")) if not user_profile.can_create_web_public_streams(): raise JsonableError(_("Insufficient permission")) # Forbid parameter combinations that are inconsistent if is_private or history_public_to_subscribers is False: raise JsonableError(_("Invalid parameters")) if is_private is not None or is_web_public is not None: do_change_stream_permission( stream, invite_only=is_private, history_public_to_subscribers=history_public_to_subscribers, is_web_public=is_web_public, acting_user=user_profile, ) return json_success(request)
def json_rename_stream(request, user_profile, old_name=REQ, new_name=REQ): return json_success( do_rename_stream(user_profile.realm, old_name, new_name))
def json_rename_stream(request, user_profile, old_name=REQ, new_name=REQ): return json_success(do_rename_stream(user_profile.realm, old_name, new_name))
def json_rename_stream(request, user_profile, old_name=REQ(), new_name=REQ()): # type: (HttpRequest, UserProfile, text_type, text_type) -> HttpResponse do_rename_stream(user_profile.realm, old_name, new_name) return json_success()