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
|
2013-08-22 19:15:54 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2015-10-13 22:54:35 +02: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-08-19 20:59:38 +02:00
|
|
|
Example: python manage.py realm_emoji --realm=zulip.com --op=add robotheart \\
|
|
|
|
https://humbug-user-avatars.s3.amazonaws.com/95ffa70fe0e7aea3c052ba91b38a28d8779f5705
|
2016-04-07 15:27:25 +02:00
|
|
|
Example: python manage.py realm_emoji --realm=zulip.com --op=remove robotheart
|
|
|
|
Example: python 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):
|
|
|
|
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',
|
|
|
|
dest='domain',
|
|
|
|
type=str,
|
|
|
|
required=True,
|
|
|
|
help='The name of the realm.')
|
|
|
|
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
|
2015-10-13 22:54:35 +02:00
|
|
|
realm = get_realm(options["domain"])
|
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-04-07 15:27:25 +02:00
|
|
|
self.print_help("python 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-04-07 15:27:25 +02:00
|
|
|
self.print_help("python 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-04-07 15:27:25 +02:00
|
|
|
self.print_help("python manage.py", "realm_emoji")
|
2013-08-22 19:15:54 +02:00
|
|
|
sys.exit(1)
|