def render_all_stream_descriptions( apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: Stream = apps.get_model('zerver', 'Stream') all_streams = Stream.objects.exclude(description='') for stream in all_streams: stream.rendered_description = render_stream_description( stream.description) stream.save(update_fields=["rendered_description"])
def bulk_create_streams( realm: Realm, stream_dict: Dict[str, Dict[str, Any]]) -> None: # nocoverage existing_streams = frozenset([ name.lower() for name in Stream.objects.filter( realm=realm).values_list('name', flat=True) ]) streams_to_create = [] # type: List[Stream] for name, options in stream_dict.items(): if 'history_public_to_subscribers' not in options: options['history_public_to_subscribers'] = ( not options.get("invite_only", False) and not realm.is_zephyr_mirror_realm) if name.lower() not in existing_streams: from zerver.lib.actions import render_stream_description streams_to_create.append( Stream( realm=realm, name=name, description=options["description"], rendered_description=render_stream_description( options["description"]), invite_only=options.get("invite_only", False), stream_post_policy=options.get( "stream_post_policy", Stream.STREAM_POST_POLICY_EVERYONE), history_public_to_subscribers=options[ "history_public_to_subscribers"], is_web_public=options.get("is_web_public", False), is_in_zephyr_realm=realm.is_zephyr_mirror_realm, )) # Sort streams by name before creating them so that we can have a # reliable ordering of `stream_id` across different python versions. # This is required for test fixtures which contain `stream_id`. Prior # to python 3.3 hashes were not randomized but after a security fix # hash randomization was enabled in python 3.3 which made iteration # of dictionaries and sets completely unpredictable. Here the order # of elements while iterating `stream_dict` will be completely random # for python 3.3 and later versions. streams_to_create.sort(key=lambda x: x.name) Stream.objects.bulk_create(streams_to_create) recipients_to_create = [] # type: List[Recipient] for stream in Stream.objects.filter(realm=realm).values('id', 'name'): if stream['name'].lower() not in existing_streams: recipients_to_create.append( Recipient(type_id=stream['id'], type=Recipient.STREAM)) Recipient.objects.bulk_create(recipients_to_create) bulk_set_users_or_streams_recipient_fields(Stream, streams_to_create, recipients_to_create)
def render_all_stream_descriptions(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: Stream = apps.get_model('zerver', 'Stream') all_streams = Stream.objects.exclude(description='') for stream in all_streams: stream.rendered_description = render_stream_description(stream.description) stream.save(update_fields=["rendered_description"])