2017-11-16 00:43:27 +01:00
|
|
|
import sys
|
2016-06-04 16:52:18 +02:00
|
|
|
from argparse import ArgumentParser
|
2017-11-16 00:43:27 +01:00
|
|
|
from typing import Any
|
2013-06-05 22:33:17 +02:00
|
|
|
|
2021-03-30 12:15:39 +02:00
|
|
|
from zerver.lib.actions import do_add_linkifier, do_remove_linkifier
|
2020-01-14 21:59:46 +01:00
|
|
|
from zerver.lib.management import CommandError, ZulipBaseCommand
|
2021-04-13 17:58:44 +02:00
|
|
|
from zerver.models import linkifiers_for_realm
|
2013-06-05 22:33:17 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-08-07 22:10:28 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2017-03-14 23:43:47 +01:00
|
|
|
help = """Create a link filter rule for the specified realm.
|
2013-06-05 22:33:17 +02:00
|
|
|
|
2014-01-27 19:43:55 +01:00
|
|
|
NOTE: Regexes must be simple enough that they can be easily translated to JavaScript
|
|
|
|
RegExp syntax. In addition to JS-compatible syntax, the following features are available:
|
|
|
|
|
|
|
|
* Named groups will be converted to numbered groups automatically
|
|
|
|
* Inline-regex flags will be stripped, and where possible translated to RegExp-wide flags
|
|
|
|
|
2017-10-27 02:47:30 +02:00
|
|
|
Example: ./manage.py realm_filters --realm=zulip --op=add '#(?P<id>[0-9]{2,8})' \
|
|
|
|
'https://support.example.com/ticket/%(id)s'
|
2016-11-16 21:16:02 +01:00
|
|
|
Example: ./manage.py realm_filters --realm=zulip --op=remove '#(?P<id>[0-9]{2,8})'
|
|
|
|
Example: ./manage.py realm_filters --realm=zulip --op=show
|
2013-06-05 22:33:17 +02:00
|
|
|
"""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--op", default="show", help="What operation to do (add, show, remove)."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"pattern", metavar="<pattern>", nargs="?", help="regular expression to match"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"url_format_string",
|
|
|
|
metavar="<URL pattern>",
|
|
|
|
nargs="?",
|
2021-02-12 08:19:30 +01:00
|
|
|
help="format string to substitute",
|
|
|
|
)
|
2017-08-07 22:10:28 +02:00
|
|
|
self.add_realm_args(parser, True)
|
2013-06-05 22:33:17 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2017-08-07 22:10:28 +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-06-05 22:33:17 +02:00
|
|
|
if options["op"] == "show":
|
2021-04-13 17:58:44 +02:00
|
|
|
print(f"{realm.string_id}: {linkifiers_for_realm(realm.id)}")
|
2013-06-05 22:33:17 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
pattern = options["pattern"]
|
2015-08-21 02:10:41 +02:00
|
|
|
if not pattern:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "realm_filters")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|
2013-06-05 22:33:17 +02:00
|
|
|
|
|
|
|
if options["op"] == "add":
|
2021-02-12 08:20:45 +01:00
|
|
|
url_format_string = options["url_format_string"]
|
2015-08-21 02:10:41 +02:00
|
|
|
if not url_format_string:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "realm_filters")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|
2021-03-30 12:15:39 +02:00
|
|
|
do_add_linkifier(realm, pattern, url_format_string)
|
2013-06-05 22:33:17 +02:00
|
|
|
sys.exit(0)
|
|
|
|
elif options["op"] == "remove":
|
2021-03-30 12:15:39 +02:00
|
|
|
do_remove_linkifier(realm, pattern=pattern)
|
2013-06-05 22:33:17 +02:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "realm_filters")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|