mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 13:03:29 +00:00
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>
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
from django.core.exceptions import ValidationError
|
|
from django.http import HttpRequest, HttpResponse
|
|
from django.utils.translation import gettext as _
|
|
|
|
from zerver.actions.realm_linkifiers import (
|
|
do_add_linkifier,
|
|
do_remove_linkifier,
|
|
do_update_linkifier,
|
|
)
|
|
from zerver.decorator import require_realm_admin
|
|
from zerver.lib.exceptions import JsonableError, ValidationFailureError
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
from zerver.lib.response import json_success
|
|
from zerver.models import RealmFilter, UserProfile, linkifiers_for_realm
|
|
|
|
|
|
# Custom realm linkifiers
|
|
def list_linkifiers(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
|
linkifiers = linkifiers_for_realm(user_profile.realm_id)
|
|
return json_success(request, data={"linkifiers": linkifiers})
|
|
|
|
|
|
@require_realm_admin
|
|
@has_request_variables
|
|
def create_linkifier(
|
|
request: HttpRequest,
|
|
user_profile: UserProfile,
|
|
pattern: str = REQ(),
|
|
url_template: str = REQ(),
|
|
) -> HttpResponse:
|
|
try:
|
|
linkifier_id = do_add_linkifier(
|
|
realm=user_profile.realm,
|
|
pattern=pattern,
|
|
url_template=url_template,
|
|
acting_user=user_profile,
|
|
)
|
|
return json_success(request, data={"id": linkifier_id})
|
|
except ValidationError as e:
|
|
raise ValidationFailureError(e)
|
|
|
|
|
|
@require_realm_admin
|
|
def delete_linkifier(
|
|
request: HttpRequest, user_profile: UserProfile, filter_id: int
|
|
) -> HttpResponse:
|
|
try:
|
|
do_remove_linkifier(realm=user_profile.realm, id=filter_id, acting_user=None)
|
|
except RealmFilter.DoesNotExist:
|
|
raise JsonableError(_("Linkifier not found."))
|
|
return json_success(request)
|
|
|
|
|
|
@require_realm_admin
|
|
@has_request_variables
|
|
def update_linkifier(
|
|
request: HttpRequest,
|
|
user_profile: UserProfile,
|
|
filter_id: int,
|
|
pattern: str = REQ(),
|
|
url_template: str = REQ(),
|
|
) -> HttpResponse:
|
|
try:
|
|
do_update_linkifier(
|
|
realm=user_profile.realm,
|
|
id=filter_id,
|
|
pattern=pattern,
|
|
url_template=url_template,
|
|
acting_user=user_profile,
|
|
)
|
|
return json_success(request)
|
|
except RealmFilter.DoesNotExist:
|
|
raise JsonableError(_("Linkifier not found."))
|
|
except ValidationError as e:
|
|
raise ValidationFailureError(e)
|