2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2017-10-14 16:58:31 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2018-03-21 22:05:21 +01:00
|
|
|
from zerver.lib.actions import ensure_stream
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-11-02 10:19:24 +01:00
|
|
|
from zerver.models import DefaultStreamGroup
|
2017-10-14 16:58:31 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-10-14 16:58:31 +02:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2018-03-12 02:47:49 +01:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2017-10-14 16:58:31 +02:00
|
|
|
self.add_realm_args(parser, True)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2017-11-16 00:25:38 +01:00
|
|
|
'-n', '--name',
|
2017-10-14 16:58:31 +02:00
|
|
|
type=str,
|
|
|
|
required=True,
|
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-04-10 05:23:40 +02:00
|
|
|
help='Name of the group you want to create.',
|
2017-11-16 00:25:38 +01:00
|
|
|
)
|
2017-10-14 16:58:31 +02:00
|
|
|
|
|
|
|
parser.add_argument(
|
2017-11-16 00:25:38 +01:00
|
|
|
'-d', '--description',
|
2017-10-14 16:58:31 +02:00
|
|
|
type=str,
|
|
|
|
required=True,
|
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-04-10 05:23:40 +02:00
|
|
|
help='Description of the group.',
|
2017-10-14 16:58:31 +02:00
|
|
|
)
|
|
|
|
|
2017-11-16 00:25:38 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-s', '--streams',
|
|
|
|
type=str,
|
|
|
|
required=True,
|
|
|
|
help='A comma-separated list of stream names.')
|
|
|
|
|
2018-03-12 02:47:49 +01:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-10-14 16:58:31 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
assert realm is not None # Should be ensured by parser
|
|
|
|
|
|
|
|
streams = []
|
2020-04-09 21:51:58 +02:00
|
|
|
stream_names = {stream.strip() for stream in options["streams"].split(",")}
|
2017-10-14 16:58:31 +02:00
|
|
|
for stream_name in set(stream_names):
|
2020-06-29 23:31:25 +02:00
|
|
|
stream = ensure_stream(realm, stream_name, acting_user=None)
|
2017-10-14 16:58:31 +02:00
|
|
|
streams.append(stream)
|
|
|
|
|
|
|
|
try:
|
2017-11-09 11:45:56 +01:00
|
|
|
default_stream_group = DefaultStreamGroup.objects.get(
|
2017-11-16 00:25:38 +01:00
|
|
|
name=options["name"], realm=realm, description=options["description"])
|
2017-10-14 16:58:31 +02:00
|
|
|
except DefaultStreamGroup.DoesNotExist:
|
2017-11-09 11:45:56 +01:00
|
|
|
default_stream_group = DefaultStreamGroup.objects.create(
|
2017-11-16 00:25:38 +01:00
|
|
|
name=options["name"], realm=realm, description=options["description"])
|
2018-01-31 08:22:07 +01:00
|
|
|
default_stream_group.streams.set(streams)
|
2017-10-14 16:58:31 +02:00
|
|
|
|
|
|
|
default_stream_groups = DefaultStreamGroup.objects.all()
|
|
|
|
for default_stream_group in default_stream_groups:
|
|
|
|
print(default_stream_group.name)
|
2017-11-16 00:25:38 +01:00
|
|
|
print(default_stream_group.description)
|
2017-10-14 16:58:31 +02:00
|
|
|
for stream in default_stream_group.streams.all():
|
|
|
|
print(stream.name)
|
2017-11-16 00:25:38 +01:00
|
|
|
print("")
|