2013-08-22 19:15:54 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
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
|
2017-01-04 05:30:48 +01:00
|
|
|
from zerver.models import Realm, get_realm
|
2016-02-12 21:08:56 +01:00
|
|
|
from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
|
2013-08-22 19:15:54 +02:00
|
|
|
import sys
|
2016-03-11 10:57:29 +01:00
|
|
|
import six
|
2013-08-22 19:15:54 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
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('-r', '--realm',
|
2016-11-16 21:16:02 +01:00
|
|
|
dest='string_id',
|
2015-08-21 02:10:41 +02:00
|
|
|
type=str,
|
|
|
|
required=True,
|
2016-11-16 21:16:02 +01:00
|
|
|
help='The subdomain or string_id of the realm.')
|
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")
|
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-01-04 05:30:48 +01:00
|
|
|
realm = get_realm(options["string_id"])
|
2013-08-22 19:15:54 +02:00
|
|
|
if options["op"] == "show":
|
2016-03-11 10:57:29 +01:00
|
|
|
for name, url in six.iteritems(realm.get_emoji()):
|
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)
|