2013-08-22 19:15:54 +02:00
|
|
|
|
2016-08-19 20:59:38 +02:00
|
|
|
from argparse import RawTextHelpFormatter
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2016-09-12 08:08:36 +02:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2016-02-12 21:08:56 +01:00
|
|
|
from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
|
2017-08-07 22:07:43 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2013-08-22 19:15:54 +02:00
|
|
|
import sys
|
|
|
|
|
2017-08-07 22:07:43 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2013-08-22 19:15:54 +02:00
|
|
|
help = """Manage emoji for the specified realm
|
|
|
|
|
2016-11-22 01:44:16 +01:00
|
|
|
Example: ./manage.py realm_emoji --realm=zulip.com --op=add robotheart \\
|
2016-08-19 20:59:38 +02:00
|
|
|
https://humbug-user-avatars.s3.amazonaws.com/95ffa70fe0e7aea3c052ba91b38a28d8779f5705
|
2016-11-22 01:44:16 +01:00
|
|
|
Example: ./manage.py realm_emoji --realm=zulip.com --op=remove robotheart
|
|
|
|
Example: ./manage.py realm_emoji --realm=zulip.com --op=show
|
2013-08-22 19:15:54 +02:00
|
|
|
"""
|
|
|
|
|
2016-08-19 20:59:38 +02:00
|
|
|
# Fix support for multi-line usage
|
|
|
|
def create_parser(self, *args, **kwargs):
|
2016-09-12 08:08:36 +02:00
|
|
|
# type: (*Any, **Any) -> CommandParser
|
2016-08-19 20:59:38 +02:00
|
|
|
parser = super(Command, self).create_parser(*args, **kwargs)
|
|
|
|
parser.formatter_class = RawTextHelpFormatter
|
|
|
|
return parser
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('--op',
|
|
|
|
dest='op',
|
|
|
|
type=str,
|
|
|
|
default="show",
|
|
|
|
help='What operation to do (add, show, remove).')
|
|
|
|
parser.add_argument('name', metavar='<name>', type=str, nargs='?', default=None,
|
|
|
|
help="name of the emoji")
|
|
|
|
parser.add_argument('img_url', metavar='<image url>', type=str, nargs='?',
|
|
|
|
help="URL of image to display for the emoji")
|
2017-08-07 22:07:43 +02:00
|
|
|
self.add_realm_args(parser, True)
|
2013-08-22 19:15:54 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **str) -> None
|
2017-08-07 22:07:43 +02:00
|
|
|
realm = self.get_realm(options)
|
2017-09-26 01:25:39 +02:00
|
|
|
assert realm is not None # Should be ensured by parser
|
2013-08-22 19:15:54 +02:00
|
|
|
if options["op"] == "show":
|
2017-09-27 10:14:26 +02:00
|
|
|
for name, url in realm.get_emoji().items():
|
2015-11-01 17:11:06 +01:00
|
|
|
print(name, url)
|
2013-08-22 19:15:54 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
name = options['name']
|
|
|
|
if name is None:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "realm_emoji")
|
2013-08-22 19:15:54 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if options["op"] == "add":
|
2015-08-21 02:10:41 +02:00
|
|
|
img_url = options['img_url']
|
|
|
|
if img_url is None:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "realm_emoji")
|
2015-08-21 02:10:41 +02:00
|
|
|
sys.exit(1)
|
2016-02-12 21:08:56 +01:00
|
|
|
check_add_realm_emoji(realm, name, img_url)
|
2013-08-22 19:15:54 +02:00
|
|
|
sys.exit(0)
|
|
|
|
elif options["op"] == "remove":
|
|
|
|
do_remove_realm_emoji(realm, name)
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "realm_emoji")
|
2013-08-22 19:15:54 +02:00
|
|
|
sys.exit(1)
|