2016-07-26 23:16:20 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, Optional
|
2016-07-26 23:16:20 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-08-04 17:32:41 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2016-07-26 23:16:20 +02:00
|
|
|
from zerver.decorator import require_realm_admin, to_non_negative_int
|
|
|
|
from zerver.lib.actions import (
|
|
|
|
do_set_realm_message_editing,
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_authentication_methods,
|
|
|
|
do_set_realm_property,
|
2016-07-26 23:16:20 +02:00
|
|
|
)
|
2016-08-04 17:32:41 +02:00
|
|
|
from zerver.lib.i18n import get_available_language_codes
|
|
|
|
from zerver.lib.request import has_request_variables, REQ, JsonableError
|
2016-07-26 23:16:20 +02:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2016-11-02 21:51:56 +01:00
|
|
|
from zerver.lib.validator import check_string, check_dict, check_bool
|
2016-07-26 23:16:20 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2017-03-21 18:08:40 +01:00
|
|
|
|
2016-07-26 23:16:20 +02:00
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
|
|
|
def update_realm(request, user_profile, name=REQ(validator=check_string, default=None),
|
2017-03-18 20:19:44 +01:00
|
|
|
description=REQ(validator=check_string, default=None),
|
2016-07-26 23:16:20 +02:00
|
|
|
restricted_to_domain=REQ(validator=check_bool, default=None),
|
|
|
|
invite_required=REQ(validator=check_bool, default=None),
|
|
|
|
invite_by_admins_only=REQ(validator=check_bool, default=None),
|
2017-03-13 18:33:49 +01:00
|
|
|
name_changes_disabled=REQ(validator=check_bool, default=None),
|
2017-03-04 06:39:45 +01:00
|
|
|
email_changes_disabled=REQ(validator=check_bool, default=None),
|
2017-03-13 14:42:03 +01:00
|
|
|
inline_image_preview=REQ(validator=check_bool, default=None),
|
|
|
|
inline_url_embed_preview=REQ(validator=check_bool, default=None),
|
2016-07-26 23:16:20 +02:00
|
|
|
create_stream_by_admins_only=REQ(validator=check_bool, default=None),
|
2016-12-20 15:41:30 +01:00
|
|
|
add_emoji_by_admins_only=REQ(validator=check_bool, default=None),
|
2016-07-26 23:16:20 +02:00
|
|
|
allow_message_editing=REQ(validator=check_bool, default=None),
|
2016-08-04 17:32:41 +02:00
|
|
|
message_content_edit_limit_seconds=REQ(converter=to_non_negative_int, default=None),
|
2016-11-02 21:51:56 +01:00
|
|
|
default_language=REQ(validator=check_string, default=None),
|
2016-11-29 08:57:35 +01:00
|
|
|
waiting_period_threshold=REQ(converter=to_non_negative_int, default=None),
|
2016-11-02 21:51:56 +01:00
|
|
|
authentication_methods=REQ(validator=check_dict([]), default=None)):
|
2017-03-13 14:42:03 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Optional[str], Optional[str], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[int], Optional[str], Optional[int], Optional[dict]) -> HttpResponse
|
2016-08-04 17:32:41 +02:00
|
|
|
# Validation for default_language
|
|
|
|
if default_language is not None and default_language not in get_available_language_codes():
|
|
|
|
raise JsonableError(_("Invalid language '%s'" % (default_language,)))
|
2016-07-26 23:16:20 +02:00
|
|
|
realm = user_profile.realm
|
|
|
|
data = {} # type: Dict[str, Any]
|
|
|
|
if name is not None and realm.name != name:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'name', name)
|
2016-07-26 23:16:20 +02:00
|
|
|
data['name'] = 'updated'
|
2017-03-18 20:19:44 +01:00
|
|
|
if description is not None and realm.description != description:
|
|
|
|
if len(description) > 100:
|
|
|
|
return json_error(_("Realm description cannot exceed 100 characters."))
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'description', description)
|
2017-03-18 20:19:44 +01:00
|
|
|
data['description'] = 'updated'
|
2016-07-26 23:16:20 +02:00
|
|
|
if restricted_to_domain is not None and realm.restricted_to_domain != restricted_to_domain:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'restricted_to_domain', restricted_to_domain)
|
2016-07-26 23:16:20 +02:00
|
|
|
data['restricted_to_domain'] = restricted_to_domain
|
|
|
|
if invite_required is not None and realm.invite_required != invite_required:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'invite_required', invite_required)
|
2016-07-26 23:16:20 +02:00
|
|
|
data['invite_required'] = invite_required
|
|
|
|
if invite_by_admins_only is not None and realm.invite_by_admins_only != invite_by_admins_only:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'invite_by_admins_only', invite_by_admins_only)
|
2016-07-26 23:16:20 +02:00
|
|
|
data['invite_by_admins_only'] = invite_by_admins_only
|
2017-03-13 18:33:49 +01:00
|
|
|
if name_changes_disabled is not None and realm.name_changes_disabled != name_changes_disabled:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'name_changes_disabled', name_changes_disabled)
|
2017-03-13 18:33:49 +01:00
|
|
|
data['name_changes_disabled'] = name_changes_disabled
|
2017-03-04 06:39:45 +01:00
|
|
|
if email_changes_disabled is not None and realm.email_changes_disabled != email_changes_disabled:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'email_changes_disabled', email_changes_disabled)
|
2017-03-04 06:39:45 +01:00
|
|
|
data['email_changes_disabled'] = email_changes_disabled
|
2017-03-13 14:42:03 +01:00
|
|
|
if inline_image_preview is not None and realm.inline_image_preview != inline_image_preview:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'inline_image_preview', inline_image_preview)
|
2017-03-13 14:42:03 +01:00
|
|
|
data['inline_image_preview'] = inline_image_preview
|
|
|
|
if inline_url_embed_preview is not None and realm.inline_url_embed_preview != inline_url_embed_preview:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'inline_url_embed_preview', inline_url_embed_preview)
|
2017-03-13 14:42:03 +01:00
|
|
|
data['inline_url_embed_preview'] = inline_url_embed_preview
|
2017-01-23 02:11:10 +01:00
|
|
|
if authentication_methods is not None and realm.authentication_methods_dict() != authentication_methods:
|
2016-11-02 21:51:56 +01:00
|
|
|
if True not in list(authentication_methods.values()):
|
|
|
|
return json_error(_("At least one authentication method must be enabled."),
|
|
|
|
data={"reason": "no authentication"}, status=403)
|
|
|
|
else:
|
|
|
|
do_set_realm_authentication_methods(realm, authentication_methods)
|
|
|
|
data['authentication_methods'] = authentication_methods
|
2016-07-26 23:16:20 +02:00
|
|
|
if create_stream_by_admins_only is not None and realm.create_stream_by_admins_only != create_stream_by_admins_only:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'create_stream_by_admins_only', create_stream_by_admins_only)
|
2016-07-26 23:16:20 +02:00
|
|
|
data['create_stream_by_admins_only'] = create_stream_by_admins_only
|
2016-12-20 15:41:30 +01:00
|
|
|
if add_emoji_by_admins_only is not None and realm.add_emoji_by_admins_only != add_emoji_by_admins_only:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'add_emoji_by_admins_only', add_emoji_by_admins_only)
|
2016-12-20 15:41:30 +01:00
|
|
|
data['add_emoji_by_admins_only'] = add_emoji_by_admins_only
|
2016-07-26 23:16:20 +02:00
|
|
|
if (allow_message_editing is not None and realm.allow_message_editing != allow_message_editing) or \
|
|
|
|
(message_content_edit_limit_seconds is not None and
|
2016-12-03 18:19:09 +01:00
|
|
|
realm.message_content_edit_limit_seconds != message_content_edit_limit_seconds):
|
2016-07-26 23:16:20 +02:00
|
|
|
if allow_message_editing is None:
|
|
|
|
allow_message_editing = realm.allow_message_editing
|
|
|
|
if message_content_edit_limit_seconds is None:
|
|
|
|
message_content_edit_limit_seconds = realm.message_content_edit_limit_seconds
|
|
|
|
do_set_realm_message_editing(realm, allow_message_editing, message_content_edit_limit_seconds)
|
|
|
|
data['allow_message_editing'] = allow_message_editing
|
|
|
|
data['message_content_edit_limit_seconds'] = message_content_edit_limit_seconds
|
2016-08-04 17:32:41 +02:00
|
|
|
if default_language is not None and realm.default_language != default_language:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'default_language', default_language)
|
2016-08-04 17:32:41 +02:00
|
|
|
data['default_language'] = default_language
|
2016-11-29 08:57:35 +01:00
|
|
|
if waiting_period_threshold is not None and realm.waiting_period_threshold != waiting_period_threshold:
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'waiting_period_threshold', waiting_period_threshold)
|
2016-11-29 08:57:35 +01:00
|
|
|
data['waiting_period_threshold'] = waiting_period_threshold
|
2016-07-26 23:16:20 +02:00
|
|
|
return json_success(data)
|