2016-07-26 23:16:20 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-04-16 17:30:49 +02:00
|
|
|
from typing import Any, Dict, Optional, List, Text
|
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-11-30 10:42:58 +01:00
|
|
|
from zerver.decorator import require_realm_admin, to_non_negative_int, to_not_negative_int_or_none
|
2016-07-26 23:16:20 +02:00
|
|
|
from zerver.lib.actions import (
|
|
|
|
do_set_realm_message_editing,
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_authentication_methods,
|
2017-06-09 20:50:38 +02:00
|
|
|
do_set_realm_notifications_stream,
|
2017-03-21 18:08:40 +01:00
|
|
|
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
|
2017-06-09 20:50:38 +02:00
|
|
|
from zerver.lib.validator import check_string, check_dict, check_bool, check_int
|
|
|
|
from zerver.lib.streams import access_stream_by_id
|
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),
|
2017-07-04 20:04:27 +02:00
|
|
|
mandatory_topics=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),
|
2017-07-16 11:00:44 +02:00
|
|
|
allow_edit_history=REQ(validator=check_bool, 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-30 10:42:58 +01:00
|
|
|
authentication_methods=REQ(validator=check_dict([]), default=None),
|
2017-06-09 20:50:38 +02:00
|
|
|
notifications_stream_id=REQ(validator=check_int, default=None),
|
2016-11-30 10:42:58 +01:00
|
|
|
message_retention_days=REQ(converter=to_not_negative_int_or_none, default=None)):
|
2017-07-16 11:00:44 +02: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[bool], Optional[int], Optional[bool], Optional[str], Optional[int], Optional[dict], Optional[int], Optional[int]) -> HttpResponse
|
2017-04-16 17:30:49 +02:00
|
|
|
realm = user_profile.realm
|
|
|
|
|
|
|
|
# Additional validation/error checking beyond types go here, so
|
|
|
|
# the entire request can succeed or fail atomically.
|
2016-08-04 17:32:41 +02:00
|
|
|
if default_language is not None and default_language not in get_available_language_codes():
|
|
|
|
raise JsonableError(_("Invalid language '%s'" % (default_language,)))
|
2017-05-11 22:52:14 +02:00
|
|
|
if description is not None and len(description) > 1000:
|
|
|
|
return json_error(_("Realm description is too long."))
|
2017-04-16 17:30:49 +02:00
|
|
|
if authentication_methods is not None and True not in list(authentication_methods.values()):
|
2017-07-25 02:28:33 +02:00
|
|
|
return json_error(_("At least one authentication method must be enabled."))
|
2017-04-16 17:30:49 +02:00
|
|
|
|
|
|
|
# The user of `locals()` here is a bit of a code smell, but it's
|
|
|
|
# restricted to the elements present in realm.property_types.
|
|
|
|
#
|
|
|
|
# TODO: It should be possible to deduplicate this function up
|
|
|
|
# further by some more advanced usage of the
|
|
|
|
# `REQ/has_request_variables` extraction.
|
|
|
|
req_vars = {k: v for k, v in list(locals().items()) if k in realm.property_types}
|
2017-05-17 22:13:14 +02:00
|
|
|
data = {} # type: Dict[str, Any]
|
2017-04-16 17:30:49 +02:00
|
|
|
|
|
|
|
for k, v in list(req_vars.items()):
|
2017-04-19 05:49:53 +02:00
|
|
|
if v is not None and getattr(realm, k) != v:
|
2017-04-16 17:30:49 +02:00
|
|
|
do_set_realm_property(realm, k, v)
|
|
|
|
if isinstance(v, Text):
|
|
|
|
data[k] = 'updated'
|
|
|
|
else:
|
|
|
|
data[k] = v
|
|
|
|
|
|
|
|
# The following realm properties do not fit the pattern above
|
2017-04-19 05:49:53 +02:00
|
|
|
# authentication_methods is not supported by the do_set_realm_property
|
|
|
|
# framework because of its bitfield.
|
2017-01-23 02:11:10 +01:00
|
|
|
if authentication_methods is not None and realm.authentication_methods_dict() != authentication_methods:
|
2017-04-16 17:30:49 +02:00
|
|
|
do_set_realm_authentication_methods(realm, authentication_methods)
|
2016-11-02 21:51:56 +01:00
|
|
|
data['authentication_methods'] = authentication_methods
|
2017-04-19 05:49:53 +02:00
|
|
|
# The message_editing settings are coupled to each other, and thus don't fit
|
|
|
|
# into the do_set_realm_property framework.
|
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
|
2017-06-09 20:50:38 +02:00
|
|
|
# Realm.notifications_stream is not a boolean, Text or integer field, and thus doesn't fit
|
|
|
|
# into the do_set_realm_property framework.
|
|
|
|
if notifications_stream_id is not None:
|
|
|
|
if realm.notifications_stream is None or realm.notifications_stream.id != notifications_stream_id:
|
|
|
|
new_notifications_stream = None
|
|
|
|
if notifications_stream_id >= 0:
|
|
|
|
(new_notifications_stream, recipient, sub) = access_stream_by_id(user_profile, notifications_stream_id)
|
|
|
|
do_set_realm_notifications_stream(realm, new_notifications_stream, notifications_stream_id)
|
|
|
|
data['notifications_stream_id'] = notifications_stream_id
|
|
|
|
|
2016-07-26 23:16:20 +02:00
|
|
|
return json_success(data)
|