2016-02-13 19:17:15 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2022-04-14 23:32:56 +02:00
|
|
|
from zerver.actions.realm_linkifiers import (
|
|
|
|
do_add_linkifier,
|
|
|
|
do_remove_linkifier,
|
|
|
|
do_update_linkifier,
|
|
|
|
)
|
2017-10-28 00:07:31 +02:00
|
|
|
from zerver.decorator import require_realm_admin
|
2021-07-04 10:00:10 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError, ValidationFailureError
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-07-04 10:00:10 +02:00
|
|
|
from zerver.lib.response import json_success
|
2021-03-30 12:51:54 +02:00
|
|
|
from zerver.models import RealmFilter, UserProfile, linkifiers_for_realm
|
2016-02-13 19:17:15 +01:00
|
|
|
|
|
|
|
|
2021-03-30 12:15:39 +02:00
|
|
|
# Custom realm linkifiers
|
|
|
|
def list_linkifiers(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2021-03-30 12:51:54 +02:00
|
|
|
linkifiers = linkifiers_for_realm(user_profile.realm_id)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"linkifiers": linkifiers})
|
2016-02-13 19:17:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
2021-03-30 12:15:39 +02:00
|
|
|
def create_linkifier(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
pattern: str = REQ(),
|
|
|
|
url_format_string: str = REQ(),
|
|
|
|
) -> HttpResponse:
|
2016-02-13 19:17:15 +01:00
|
|
|
try:
|
2021-03-30 12:15:39 +02:00
|
|
|
linkifier_id = do_add_linkifier(
|
2016-02-13 19:17:15 +01:00
|
|
|
realm=user_profile.realm,
|
|
|
|
pattern=pattern,
|
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
|
|
|
url_format_string=url_format_string,
|
2022-03-14 11:50:24 +01:00
|
|
|
acting_user=user_profile,
|
2016-02-13 19:17:15 +01:00
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"id": linkifier_id})
|
2016-02-13 19:17:15 +01:00
|
|
|
except ValidationError as e:
|
2021-07-04 10:00:10 +02:00
|
|
|
raise ValidationFailureError(e)
|
2016-02-13 19:17:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
2021-03-30 12:15:39 +02:00
|
|
|
def delete_linkifier(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, filter_id: int
|
|
|
|
) -> HttpResponse:
|
2016-02-13 19:17:15 +01:00
|
|
|
try:
|
2022-03-14 14:50:55 +01:00
|
|
|
do_remove_linkifier(realm=user_profile.realm, id=filter_id, acting_user=None)
|
2016-02-13 19:17:15 +01:00
|
|
|
except RealmFilter.DoesNotExist:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Linkifier not found."))
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2021-04-15 19:51:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
|
|
|
def update_linkifier(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
filter_id: int,
|
|
|
|
pattern: str = REQ(),
|
|
|
|
url_format_string: str = REQ(),
|
|
|
|
) -> HttpResponse:
|
|
|
|
try:
|
|
|
|
do_update_linkifier(
|
|
|
|
realm=user_profile.realm,
|
|
|
|
id=filter_id,
|
|
|
|
pattern=pattern,
|
|
|
|
url_format_string=url_format_string,
|
2022-03-14 12:10:25 +01:00
|
|
|
acting_user=user_profile,
|
2021-04-15 19:51:36 +02:00
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2021-04-15 19:51:36 +02:00
|
|
|
except RealmFilter.DoesNotExist:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Linkifier not found."))
|
2021-04-15 19:51:36 +02:00
|
|
|
except ValidationError as e:
|
2021-07-04 10:00:10 +02:00
|
|
|
raise ValidationFailureError(e)
|