mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
JsonableError has two major benefits over json_error: * It can be raised from anywhere in the codebase, rather than being a return value, which is much more convenient for refactoring, as one doesn't potentially need to change error handling style when extracting a bit of view code to a function. * It is guaranteed to contain the `code` property, which is helpful for API consistency. Various stragglers are not updated because JsonableError requires subclassing in order to specify custom data or HTTP status codes.
68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
from django.core.exceptions import ValidationError
|
|
from django.http import HttpRequest, HttpResponse
|
|
from django.utils.translation import gettext as _
|
|
|
|
from zerver.decorator import require_realm_admin
|
|
from zerver.lib.actions import do_add_realm_domain, do_change_realm_domain, do_remove_realm_domain
|
|
from zerver.lib.domains import validate_domain
|
|
from zerver.lib.exceptions import JsonableError
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
from zerver.lib.response import json_success
|
|
from zerver.lib.validator import check_bool
|
|
from zerver.models import RealmDomain, UserProfile, get_realm_domains
|
|
|
|
|
|
def list_realm_domains(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
|
domains = get_realm_domains(user_profile.realm)
|
|
return json_success({"domains": domains})
|
|
|
|
|
|
@require_realm_admin
|
|
@has_request_variables
|
|
def create_realm_domain(
|
|
request: HttpRequest,
|
|
user_profile: UserProfile,
|
|
domain: str = REQ(),
|
|
allow_subdomains: bool = REQ(json_validator=check_bool),
|
|
) -> HttpResponse:
|
|
domain = domain.strip().lower()
|
|
try:
|
|
validate_domain(domain)
|
|
except ValidationError as e:
|
|
raise JsonableError(_("Invalid domain: {}").format(e.messages[0]))
|
|
if RealmDomain.objects.filter(realm=user_profile.realm, domain=domain).exists():
|
|
raise JsonableError(
|
|
_("The domain {domain} is already a part of your organization.").format(domain=domain)
|
|
)
|
|
realm_domain = do_add_realm_domain(user_profile.realm, domain, allow_subdomains)
|
|
return json_success({"new_domain": [realm_domain.id, realm_domain.domain]})
|
|
|
|
|
|
@require_realm_admin
|
|
@has_request_variables
|
|
def patch_realm_domain(
|
|
request: HttpRequest,
|
|
user_profile: UserProfile,
|
|
domain: str,
|
|
allow_subdomains: bool = REQ(json_validator=check_bool),
|
|
) -> HttpResponse:
|
|
try:
|
|
realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain)
|
|
do_change_realm_domain(realm_domain, allow_subdomains)
|
|
except RealmDomain.DoesNotExist:
|
|
raise JsonableError(_("No entry found for domain {domain}.").format(domain=domain))
|
|
return json_success()
|
|
|
|
|
|
@require_realm_admin
|
|
@has_request_variables
|
|
def delete_realm_domain(
|
|
request: HttpRequest, user_profile: UserProfile, domain: str
|
|
) -> HttpResponse:
|
|
try:
|
|
realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain)
|
|
do_remove_realm_domain(realm_domain, acting_user=user_profile)
|
|
except RealmDomain.DoesNotExist:
|
|
raise JsonableError(_("No entry found for domain {domain}.").format(domain=domain))
|
|
return json_success()
|