Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

zilencer: Extract validate_hostname_or_raise_error function.

Mateusz Mandera4196859cd65d
diff --git a/zilencer/views.py b/zilencer/views.py
index 13e99698f58..1f6578b38ec 100644
--- a/zilencer/views.py
+++ b/zilencer/views.py
@@ -121,6 +121,15 @@ def deactivate_remote_server(
return json_success(request)
+def validate_hostname_or_raise_error(hostname: str) -> None:
+ try:
+ # TODO: Ideally we'd not abuse the URL validator this way
+ url_validator = URLValidator()
+ url_validator("http://" + hostname)
+ except ValidationError:
+ raise JsonableError(_("{hostname} is not a valid hostname").format(hostname=hostname))
+
+
@csrf_exempt
@require_post
@typed_endpoint
@@ -147,12 +156,7 @@ def register_remote_server(
) -> HttpResponse:
# StringConstraints validated the field lengths, but we still need to
# validate the format of these fields.
- try:
- # TODO: Ideally we'd not abuse the URL validator this way
- url_validator = URLValidator()
- url_validator("http://" + hostname)
- except ValidationError:
- raise JsonableError(_("{hostname} is not a valid hostname").format(hostname=hostname))
+ validate_hostname_or_raise_error(hostname)
try:
validate_email(contact_email)