Files
zulip/zerver/management/commands/create_default_stream_groups.py
Anders Kaseorg 69730a78cc python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:

import re
import sys

last_filename = None
last_row = None
lines = []

for msg in sys.stdin:
    m = re.match(
        r"\x1b\[35mflake8    \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
    )
    if m:
        filename, row_str, col_str, err = m.groups()
        row, col = int(row_str), int(col_str)

        if filename == last_filename:
            assert last_row != row
        else:
            if last_filename is not None:
                with open(last_filename, "w") as f:
                    f.writelines(lines)

            with open(filename) as f:
                lines = f.readlines()
            last_filename = filename
        last_row = row

        line = lines[row - 1]
        if err in ["C812", "C815"]:
            lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
        elif err in ["C819"]:
            assert line[col - 2] == ","
            lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")

if last_filename is not None:
    with open(last_filename, "w") as f:
        f.writelines(lines)

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-06-11 16:04:12 -07:00

67 lines
2.2 KiB
Python

from argparse import ArgumentParser
from typing import Any
from zerver.lib.actions import ensure_stream
from zerver.lib.management import ZulipBaseCommand
from zerver.models import DefaultStreamGroup
class Command(ZulipBaseCommand):
help = """
Create default stream groups which the users can choose during sign up.
./manage.py create_default_stream_groups -s gsoc-1,gsoc-2,gsoc-3 -d "Google summer of code" -r zulip
"""
def add_arguments(self, parser: ArgumentParser) -> None:
self.add_realm_args(parser, True)
parser.add_argument(
'-n', '--name',
dest='name',
type=str,
required=True,
help='Name of the group you want to create.',
)
parser.add_argument(
'-d', '--description',
dest='description',
type=str,
required=True,
help='Description of the group.',
)
parser.add_argument(
'-s', '--streams',
dest='streams',
type=str,
required=True,
help='A comma-separated list of stream names.')
def handle(self, *args: Any, **options: Any) -> None:
realm = self.get_realm(options)
assert realm is not None # Should be ensured by parser
streams = []
stream_names = {stream.strip() for stream in options["streams"].split(",")}
for stream_name in set(stream_names):
stream = ensure_stream(realm, stream_name)
streams.append(stream)
try:
default_stream_group = DefaultStreamGroup.objects.get(
name=options["name"], realm=realm, description=options["description"])
except DefaultStreamGroup.DoesNotExist:
default_stream_group = DefaultStreamGroup.objects.create(
name=options["name"], realm=realm, description=options["description"])
default_stream_group.streams.set(streams)
default_stream_groups = DefaultStreamGroup.objects.all()
for default_stream_group in default_stream_groups:
print(default_stream_group.name)
print(default_stream_group.description)
for stream in default_stream_group.streams.all():
print(stream.name)
print("")