2020-06-24 19:59:36 +02:00
|
|
|
from typing import Any, Dict, Optional, Union
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
2016-07-26 23:16:20 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2018-11-12 14:15:49 +01:00
|
|
|
from django.shortcuts import render
|
2016-08-04 17:32:41 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2019-08-12 05:44:35 +02:00
|
|
|
from django.views.decorators.http import require_safe
|
2018-01-25 19:08:40 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from confirmation.models import Confirmation, ConfirmationKeyException, get_object_from_key
|
2020-06-11 00:26:49 +02:00
|
|
|
from zerver.decorator import require_realm_admin, require_realm_owner
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.forms import check_subdomain_available as check_subdomain
|
2016-07-26 23:16:20 +02:00
|
|
|
from zerver.lib.actions import (
|
2020-06-11 00:54:34 +02:00
|
|
|
do_deactivate_realm,
|
|
|
|
do_reactivate_realm,
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_authentication_methods,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_set_realm_message_deleting,
|
|
|
|
do_set_realm_message_editing,
|
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,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_set_realm_signup_notifications_stream,
|
2016-07-26 23:16:20 +02:00
|
|
|
)
|
2020-06-15 06:32:10 +02:00
|
|
|
from zerver.lib.exceptions import OrganizationOwnerRequired
|
2016-08-04 17:32:41 +02:00
|
|
|
from zerver.lib.i18n import get_available_language_codes
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.request import REQ, JsonableError, has_request_variables
|
|
|
|
from zerver.lib.response import json_error, json_success
|
2020-06-21 11:14:35 +02:00
|
|
|
from zerver.lib.retention import parse_message_retention_days
|
2017-06-09 20:50:38 +02:00
|
|
|
from zerver.lib.streams import access_stream_by_id
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.validator import (
|
|
|
|
check_bool,
|
|
|
|
check_dict,
|
|
|
|
check_int,
|
|
|
|
check_int_in,
|
|
|
|
check_string,
|
2020-06-21 11:14:35 +02:00
|
|
|
check_string_or_int,
|
2020-06-11 00:54:34 +02:00
|
|
|
to_non_negative_int,
|
|
|
|
)
|
2017-08-23 07:03:19 +02:00
|
|
|
from zerver.models import Realm, UserProfile
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-03-21 18:08:40 +01:00
|
|
|
|
2016-07-26 23:16:20 +02:00
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
2017-12-06 12:25:18 +01:00
|
|
|
def update_realm(
|
|
|
|
request: HttpRequest, user_profile: UserProfile,
|
|
|
|
name: Optional[str]=REQ(validator=check_string, default=None),
|
|
|
|
description: Optional[str]=REQ(validator=check_string, default=None),
|
2018-07-27 23:26:29 +02:00
|
|
|
emails_restricted_to_domains: Optional[bool]=REQ(validator=check_bool, default=None),
|
2018-03-05 20:19:07 +01:00
|
|
|
disallow_disposable_email_addresses: Optional[bool]=REQ(validator=check_bool, default=None),
|
2017-12-06 12:25:18 +01:00
|
|
|
invite_required: Optional[bool]=REQ(validator=check_bool, default=None),
|
|
|
|
invite_by_admins_only: Optional[bool]=REQ(validator=check_bool, default=None),
|
|
|
|
name_changes_disabled: Optional[bool]=REQ(validator=check_bool, default=None),
|
|
|
|
email_changes_disabled: Optional[bool]=REQ(validator=check_bool, default=None),
|
2019-04-23 04:51:04 +02:00
|
|
|
avatar_changes_disabled: Optional[bool]=REQ(validator=check_bool, default=None),
|
2017-12-06 12:25:18 +01:00
|
|
|
inline_image_preview: Optional[bool]=REQ(validator=check_bool, default=None),
|
|
|
|
inline_url_embed_preview: Optional[bool]=REQ(validator=check_bool, default=None),
|
|
|
|
add_emoji_by_admins_only: Optional[bool]=REQ(validator=check_bool, default=None),
|
|
|
|
allow_message_deleting: Optional[bool]=REQ(validator=check_bool, default=None),
|
2017-11-26 09:12:10 +01:00
|
|
|
message_content_delete_limit_seconds: Optional[int]=REQ(converter=to_non_negative_int, default=None),
|
2017-12-06 12:25:18 +01:00
|
|
|
allow_message_editing: Optional[bool]=REQ(validator=check_bool, default=None),
|
2017-12-03 00:50:48 +01:00
|
|
|
allow_community_topic_editing: Optional[bool]=REQ(validator=check_bool, default=None),
|
2017-12-06 12:25:18 +01:00
|
|
|
mandatory_topics: Optional[bool]=REQ(validator=check_bool, default=None),
|
|
|
|
message_content_edit_limit_seconds: Optional[int]=REQ(converter=to_non_negative_int, default=None),
|
|
|
|
allow_edit_history: Optional[bool]=REQ(validator=check_bool, default=None),
|
|
|
|
default_language: Optional[str]=REQ(validator=check_string, default=None),
|
|
|
|
waiting_period_threshold: Optional[int]=REQ(converter=to_non_negative_int, default=None),
|
2020-06-20 12:25:36 +02:00
|
|
|
authentication_methods: Optional[Dict[str, Any]]=REQ(validator=check_dict([]), default=None),
|
2017-12-06 12:25:18 +01:00
|
|
|
notifications_stream_id: Optional[int]=REQ(validator=check_int, default=None),
|
|
|
|
signup_notifications_stream_id: Optional[int]=REQ(validator=check_int, default=None),
|
2020-06-24 19:59:36 +02:00
|
|
|
message_retention_days_raw: Optional[Union[int, str]] = REQ(
|
|
|
|
"message_retention_days", validator=check_string_or_int, default=None),
|
2018-01-29 16:10:54 +01:00
|
|
|
send_welcome_emails: Optional[bool]=REQ(validator=check_bool, default=None),
|
2019-04-06 06:34:49 +02:00
|
|
|
digest_emails_enabled: Optional[bool]=REQ(validator=check_bool, default=None),
|
2019-11-16 17:43:35 +01:00
|
|
|
message_content_allowed_in_email_notifications: Optional[bool]=REQ(
|
|
|
|
validator=check_bool, default=None),
|
|
|
|
bot_creation_policy: Optional[int]=REQ(validator=check_int_in(
|
|
|
|
Realm.BOT_CREATION_POLICY_TYPES), default=None),
|
|
|
|
create_stream_policy: Optional[int]=REQ(validator=check_int_in(
|
2020-04-02 21:53:20 +02:00
|
|
|
Realm.COMMON_POLICY_TYPES), default=None),
|
2019-11-16 17:43:35 +01:00
|
|
|
invite_to_stream_policy: Optional[int]=REQ(validator=check_int_in(
|
2020-04-02 21:53:20 +02:00
|
|
|
Realm.COMMON_POLICY_TYPES), default=None),
|
2019-11-16 17:43:35 +01:00
|
|
|
user_group_edit_policy: Optional[int]=REQ(validator=check_int_in(
|
|
|
|
Realm.USER_GROUP_EDIT_POLICY_TYPES), default=None),
|
2020-01-08 01:49:44 +01:00
|
|
|
private_message_policy: Optional[int]=REQ(validator=check_int_in(
|
|
|
|
Realm.PRIVATE_MESSAGE_POLICY_TYPES), default=None),
|
2019-11-16 17:43:35 +01:00
|
|
|
email_address_visibility: Optional[int]=REQ(validator=check_int_in(
|
|
|
|
Realm.EMAIL_ADDRESS_VISIBILITY_TYPES), default=None),
|
2018-03-30 22:38:16 +02:00
|
|
|
default_twenty_four_hour_time: Optional[bool]=REQ(validator=check_bool, default=None),
|
2019-05-10 10:32:47 +02:00
|
|
|
video_chat_provider: Optional[int]=REQ(validator=check_int, default=None),
|
2020-03-31 15:21:27 +02:00
|
|
|
default_code_block_language: Optional[str]=REQ(validator=check_string, default=None),
|
2019-11-16 19:16:34 +01:00
|
|
|
digest_weekday: Optional[int]=REQ(validator=check_int_in(Realm.DIGEST_WEEKDAY_VALUES), default=None),
|
2017-12-06 12:25:18 +01:00
|
|
|
) -> 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():
|
2020-06-15 23:22:24 +02:00
|
|
|
raise JsonableError(_("Invalid language '{}'").format(default_language))
|
2017-05-11 22:52:14 +02:00
|
|
|
if description is not None and len(description) > 1000:
|
2018-03-17 00:47:55 +01:00
|
|
|
return json_error(_("Organization description is too long."))
|
2017-08-23 07:03:19 +02:00
|
|
|
if name is not None and len(name) > Realm.MAX_REALM_NAME_LENGTH:
|
2018-03-17 00:47:55 +01:00
|
|
|
return json_error(_("Organization name is too long."))
|
2020-06-11 16:24:15 +02:00
|
|
|
if authentication_methods is not None:
|
|
|
|
if not user_profile.is_realm_owner:
|
2020-06-15 06:32:10 +02:00
|
|
|
raise OrganizationOwnerRequired()
|
2020-06-11 16:24:15 +02:00
|
|
|
if True not in list(authentication_methods.values()):
|
|
|
|
return json_error(_("At least one authentication method must be enabled."))
|
2019-05-10 10:32:47 +02:00
|
|
|
if (video_chat_provider is not None and
|
2020-04-09 21:51:58 +02:00
|
|
|
video_chat_provider not in {p['id'] for p in Realm.VIDEO_CHAT_PROVIDERS.values()}):
|
2019-11-16 19:16:34 +01:00
|
|
|
return json_error(_("Invalid video_chat_provider {}").format(video_chat_provider))
|
2017-04-16 17:30:49 +02:00
|
|
|
|
2020-06-24 19:59:36 +02:00
|
|
|
message_retention_days: Optional[int] = None
|
|
|
|
if message_retention_days_raw is not None:
|
2020-06-11 21:16:53 +02:00
|
|
|
if not user_profile.is_realm_owner:
|
2020-06-15 06:32:10 +02:00
|
|
|
raise OrganizationOwnerRequired()
|
2020-05-19 14:38:43 +02:00
|
|
|
realm.ensure_not_on_limited_plan()
|
2020-06-21 11:14:35 +02:00
|
|
|
message_retention_days = parse_message_retention_days(
|
2020-06-24 19:59:36 +02:00
|
|
|
message_retention_days_raw, Realm.MESSAGE_RETENTION_SPECIAL_VALUES_MAP)
|
2020-05-09 19:46:56 +02:00
|
|
|
|
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}
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
data: 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)
|
2018-04-24 03:47:28 +02:00
|
|
|
if isinstance(v, str):
|
2017-04-16 17:30:49 +02:00
|
|
|
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-11-05 03:14:28 +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.
|
2017-12-03 00:50:48 +01: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
|
|
|
|
realm.message_content_edit_limit_seconds != message_content_edit_limit_seconds) or
|
|
|
|
(allow_community_topic_editing is not None and
|
|
|
|
realm.allow_community_topic_editing != allow_community_topic_editing)):
|
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
|
2017-12-03 00:50:48 +01:00
|
|
|
if allow_community_topic_editing is None:
|
|
|
|
allow_community_topic_editing = realm.allow_community_topic_editing
|
2017-11-05 03:14:28 +01:00
|
|
|
do_set_realm_message_editing(realm, allow_message_editing,
|
2017-12-03 00:50:48 +01:00
|
|
|
message_content_edit_limit_seconds,
|
|
|
|
allow_community_topic_editing)
|
2016-07-26 23:16:20 +02:00
|
|
|
data['allow_message_editing'] = allow_message_editing
|
|
|
|
data['message_content_edit_limit_seconds'] = message_content_edit_limit_seconds
|
2017-12-03 00:50:48 +01:00
|
|
|
data['allow_community_topic_editing'] = allow_community_topic_editing
|
2017-11-26 09:12:10 +01:00
|
|
|
|
|
|
|
if (message_content_delete_limit_seconds is not None and
|
|
|
|
realm.message_content_delete_limit_seconds != message_content_delete_limit_seconds):
|
|
|
|
do_set_realm_message_deleting(realm, message_content_delete_limit_seconds)
|
|
|
|
data['message_content_delete_limit_seconds'] = message_content_delete_limit_seconds
|
2017-10-20 16:55:04 +02:00
|
|
|
# Realm.notifications_stream and Realm.signup_notifications_stream are not boolean,
|
2018-04-24 03:47:28 +02:00
|
|
|
# str or integer field, and thus doesn't fit into the do_set_realm_property framework.
|
2017-06-09 20:50:38 +02:00
|
|
|
if notifications_stream_id is not None:
|
2017-11-05 03:14:28 +01:00
|
|
|
if realm.notifications_stream is None or (realm.notifications_stream.id !=
|
|
|
|
notifications_stream_id):
|
2017-06-09 20:50:38 +02:00
|
|
|
new_notifications_stream = None
|
|
|
|
if notifications_stream_id >= 0:
|
2017-11-05 03:14:28 +01:00
|
|
|
(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)
|
2017-06-09 20:50:38 +02:00
|
|
|
data['notifications_stream_id'] = notifications_stream_id
|
|
|
|
|
2017-10-20 16:55:04 +02:00
|
|
|
if signup_notifications_stream_id is not None:
|
|
|
|
if realm.signup_notifications_stream is None or (realm.signup_notifications_stream.id !=
|
|
|
|
signup_notifications_stream_id):
|
|
|
|
new_signup_notifications_stream = None
|
|
|
|
if signup_notifications_stream_id >= 0:
|
|
|
|
(new_signup_notifications_stream, recipient, sub) = access_stream_by_id(
|
|
|
|
user_profile, signup_notifications_stream_id)
|
|
|
|
do_set_realm_signup_notifications_stream(realm, new_signup_notifications_stream,
|
|
|
|
signup_notifications_stream_id)
|
|
|
|
data['signup_notifications_stream_id'] = signup_notifications_stream_id
|
|
|
|
|
2020-03-31 15:21:27 +02:00
|
|
|
if default_code_block_language is not None:
|
|
|
|
# Migrate '', used in the API to encode the default/None behavior of this feature.
|
|
|
|
if default_code_block_language == '':
|
|
|
|
data['default_code_block_language'] = None
|
|
|
|
else:
|
|
|
|
data['default_code_block_language'] = default_code_block_language
|
|
|
|
|
2016-07-26 23:16:20 +02:00
|
|
|
return json_success(data)
|
2018-01-30 14:58:50 +01:00
|
|
|
|
2020-06-11 00:26:49 +02:00
|
|
|
@require_realm_owner
|
2018-01-30 14:58:50 +01:00
|
|
|
@has_request_variables
|
2019-05-08 05:59:04 +02:00
|
|
|
def deactivate_realm(request: HttpRequest, user: UserProfile) -> HttpResponse:
|
|
|
|
realm = user.realm
|
|
|
|
do_deactivate_realm(realm, user)
|
2018-01-30 14:58:50 +01:00
|
|
|
return json_success()
|
2018-01-25 19:08:40 +01:00
|
|
|
|
2019-08-12 05:44:35 +02:00
|
|
|
@require_safe
|
2018-04-24 03:47:28 +02:00
|
|
|
def check_subdomain_available(request: HttpRequest, subdomain: str) -> HttpResponse:
|
2018-01-25 19:08:40 +01:00
|
|
|
try:
|
|
|
|
check_subdomain(subdomain)
|
|
|
|
return json_success({"msg": "available"})
|
|
|
|
except ValidationError as e:
|
|
|
|
return json_success({"msg": e.message})
|
2018-11-12 14:15:49 +01:00
|
|
|
|
|
|
|
def realm_reactivation(request: HttpRequest, confirmation_key: str) -> HttpResponse:
|
|
|
|
try:
|
|
|
|
realm = get_object_from_key(confirmation_key, Confirmation.REALM_REACTIVATION)
|
|
|
|
except ConfirmationKeyException:
|
|
|
|
return render(request, 'zerver/realm_reactivation_link_error.html')
|
|
|
|
do_reactivate_realm(realm)
|
|
|
|
context = {"realm": realm}
|
|
|
|
return render(request, 'zerver/realm_reactivation.html', context)
|