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 (
|
2023-08-10 04:09:25 +02:00
|
|
|
check_reorder_linkifiers,
|
2022-04-14 23:32:56 +02:00
|
|
|
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
|
2023-08-10 04:09:25 +02:00
|
|
|
from zerver.lib.validator import check_int, check_list
|
2023-12-15 02:51:31 +01:00
|
|
|
from zerver.models import RealmFilter, UserProfile
|
|
|
|
from zerver.models.linkifiers import 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(),
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
url_template: str = REQ(),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> 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,
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
url_template=url_template,
|
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(),
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
url_template: str = REQ(),
|
2021-04-15 19:51:36 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
try:
|
|
|
|
do_update_linkifier(
|
|
|
|
realm=user_profile.realm,
|
|
|
|
id=filter_id,
|
|
|
|
pattern=pattern,
|
linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-10-05 20:55:31 +02:00
|
|
|
url_template=url_template,
|
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)
|
2023-08-10 04:09:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
|
|
|
def reorder_linkifiers(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-07-12 02:30:17 +02:00
|
|
|
ordered_linkifier_ids: list[int] = REQ(json_validator=check_list(check_int)),
|
2023-08-10 04:09:25 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
check_reorder_linkifiers(user_profile.realm, ordered_linkifier_ids, acting_user=user_profile)
|
|
|
|
return json_success(request)
|