2023-01-18 05:25:49 +01:00
|
|
|
from contextlib import suppress
|
2021-06-17 23:37:52 +02:00
|
|
|
from datetime import timedelta
|
|
|
|
from decimal import Decimal
|
2023-10-18 13:18:12 +02:00
|
|
|
from typing import Any, Dict, Iterable, List, Optional, Union
|
2023-12-05 21:25:00 +01:00
|
|
|
from urllib.parse import urlencode, urlsplit
|
2021-06-17 23:37:52 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core.validators import URLValidator
|
2023-05-16 13:47:56 +02:00
|
|
|
from django.db.models import Q
|
2021-06-17 23:37:52 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
|
|
|
|
from django.shortcuts import render
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.timesince import timesince
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
2021-04-05 18:42:45 +02:00
|
|
|
from confirmation.models import Confirmation, confirmation_url
|
2022-07-16 20:09:13 +02:00
|
|
|
from confirmation.settings import STATUS_USED
|
2022-04-14 23:58:15 +02:00
|
|
|
from zerver.actions.create_realm import do_change_realm_subdomain
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.actions.realm_settings import (
|
2021-10-07 21:30:54 +02:00
|
|
|
do_change_realm_org_type,
|
2021-12-01 02:10:40 +01:00
|
|
|
do_change_realm_plan_type,
|
2021-06-17 23:37:52 +02:00
|
|
|
do_deactivate_realm,
|
|
|
|
do_scrub_realm,
|
|
|
|
do_send_realm_reactivation_email,
|
|
|
|
)
|
2023-05-15 23:40:35 +02:00
|
|
|
from zerver.actions.users import do_delete_user_preserving_messages
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.decorator import require_server_admin
|
|
|
|
from zerver.forms import check_subdomain_available
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2021-06-17 23:37:52 +02:00
|
|
|
from zerver.lib.realm_icon import realm_icon_url
|
2021-07-29 17:32:18 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-06-17 23:37:52 +02:00
|
|
|
from zerver.lib.subdomains import get_subdomain_from_hostname
|
2021-07-29 17:32:18 +02:00
|
|
|
from zerver.lib.validator import check_bool, check_string_in, to_decimal, to_non_negative_int
|
2021-07-02 20:34:00 +02:00
|
|
|
from zerver.models import (
|
|
|
|
MultiuseInvite,
|
2023-03-03 11:58:00 +01:00
|
|
|
PreregistrationRealm,
|
2021-07-02 20:34:00 +02:00
|
|
|
PreregistrationUser,
|
|
|
|
Realm,
|
2022-07-26 15:48:26 +02:00
|
|
|
RealmReactivationStatus,
|
2021-07-02 20:34:00 +02:00
|
|
|
UserProfile,
|
|
|
|
)
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models.realms import get_org_type_display_name, get_realm
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models.users import get_user_profile_by_id
|
2021-06-17 23:37:52 +02:00
|
|
|
from zerver.views.invite import get_invitee_emails_set
|
|
|
|
|
2023-10-04 13:38:12 +02:00
|
|
|
if settings.ZILENCER_ENABLED:
|
2023-11-08 19:00:13 +01:00
|
|
|
from zilencer.lib.remote_counts import MissingDataError, compute_max_monthly_messages
|
2023-12-05 16:10:15 +01:00
|
|
|
from zilencer.models import RemoteRealm, RemoteZulipServer
|
2023-10-04 13:38:12 +02:00
|
|
|
|
2021-06-17 23:37:52 +02:00
|
|
|
if settings.BILLING_ENABLED:
|
2023-12-02 18:08:49 +01:00
|
|
|
from corporate.lib.stripe import (
|
|
|
|
RealmBillingSession,
|
2023-12-11 17:29:32 +01:00
|
|
|
RemoteRealmBillingSession,
|
2023-12-02 18:08:49 +01:00
|
|
|
RemoteServerBillingSession,
|
|
|
|
SupportType,
|
|
|
|
SupportViewRequest,
|
|
|
|
)
|
2023-12-02 17:55:39 +01:00
|
|
|
from corporate.lib.support import (
|
|
|
|
PlanData,
|
2023-12-13 18:23:05 +01:00
|
|
|
SupportData,
|
2023-12-02 17:55:39 +01:00
|
|
|
get_current_plan_data_for_support_view,
|
|
|
|
get_customer_discount_for_support_view,
|
2023-12-13 18:23:05 +01:00
|
|
|
get_data_for_support_view,
|
2022-07-14 00:20:15 +02:00
|
|
|
)
|
2023-12-02 17:55:39 +01:00
|
|
|
from corporate.models import CustomerPlan
|
2021-06-17 23:37:52 +02:00
|
|
|
|
|
|
|
|
2023-12-05 13:09:33 +01:00
|
|
|
def get_plan_type_string(plan_type: int) -> str:
|
2021-09-16 16:05:26 +02:00
|
|
|
return {
|
2023-12-05 13:09:33 +01:00
|
|
|
Realm.PLAN_TYPE_SELF_HOSTED: "Self-hosted",
|
|
|
|
Realm.PLAN_TYPE_LIMITED: "Limited",
|
|
|
|
Realm.PLAN_TYPE_STANDARD: "Standard",
|
|
|
|
Realm.PLAN_TYPE_STANDARD_FREE: "Standard free",
|
|
|
|
Realm.PLAN_TYPE_PLUS: "Plus",
|
2023-12-14 00:17:55 +01:00
|
|
|
RemoteZulipServer.PLAN_TYPE_SELF_MANAGED: "Self-managed",
|
2023-12-14 08:38:54 +01:00
|
|
|
RemoteZulipServer.PLAN_TYPE_SELF_MANAGED_LEGACY: CustomerPlan.name_from_tier(
|
|
|
|
CustomerPlan.TIER_SELF_HOSTED_LEGACY
|
|
|
|
),
|
2023-12-05 13:09:33 +01:00
|
|
|
RemoteZulipServer.PLAN_TYPE_COMMUNITY: "Community",
|
|
|
|
RemoteZulipServer.PLAN_TYPE_BUSINESS: "Business",
|
|
|
|
RemoteZulipServer.PLAN_TYPE_ENTERPRISE: "Enterprise",
|
2021-09-16 16:05:26 +02:00
|
|
|
}[plan_type]
|
2021-06-17 23:37:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_confirmations(
|
2022-07-01 19:35:35 +02:00
|
|
|
types: List[int], object_ids: Iterable[int], hostname: Optional[str] = None
|
2021-06-17 23:37:52 +02:00
|
|
|
) -> List[Dict[str, Any]]:
|
|
|
|
lowest_datetime = timezone_now() - timedelta(days=30)
|
|
|
|
confirmations = Confirmation.objects.filter(
|
|
|
|
type__in=types, object_id__in=object_ids, date_sent__gte=lowest_datetime
|
|
|
|
)
|
|
|
|
confirmation_dicts = []
|
|
|
|
for confirmation in confirmations:
|
|
|
|
realm = confirmation.realm
|
|
|
|
content_object = confirmation.content_object
|
|
|
|
|
|
|
|
type = confirmation.type
|
2021-04-05 18:42:45 +02:00
|
|
|
expiry_date = confirmation.expiry_date
|
2021-06-17 23:37:52 +02:00
|
|
|
|
2021-07-24 18:16:48 +02:00
|
|
|
assert content_object is not None
|
2021-06-17 23:37:52 +02:00
|
|
|
if hasattr(content_object, "status"):
|
2022-07-16 20:09:13 +02:00
|
|
|
if content_object.status == STATUS_USED:
|
2022-05-06 21:09:00 +02:00
|
|
|
link_status = "Link has been used"
|
2021-06-17 23:37:52 +02:00
|
|
|
else:
|
2022-05-06 21:09:00 +02:00
|
|
|
link_status = "Link has not been used"
|
2021-06-17 23:37:52 +02:00
|
|
|
else:
|
|
|
|
link_status = ""
|
|
|
|
|
|
|
|
now = timezone_now()
|
2021-11-30 13:34:37 +01:00
|
|
|
if expiry_date is None:
|
|
|
|
expires_in = "Never"
|
|
|
|
elif now < expiry_date:
|
2021-06-17 23:37:52 +02:00
|
|
|
expires_in = timesince(now, expiry_date)
|
|
|
|
else:
|
|
|
|
expires_in = "Expired"
|
|
|
|
|
|
|
|
url = confirmation_url(confirmation.confirmation_key, realm, type)
|
|
|
|
confirmation_dicts.append(
|
|
|
|
{
|
|
|
|
"object": confirmation.content_object,
|
|
|
|
"url": url,
|
|
|
|
"type": type,
|
|
|
|
"link_status": link_status,
|
|
|
|
"expires_in": expires_in,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return confirmation_dicts
|
|
|
|
|
|
|
|
|
2023-04-11 00:14:41 +02:00
|
|
|
VALID_MODIFY_PLAN_METHODS = [
|
2021-07-29 17:32:18 +02:00
|
|
|
"downgrade_at_billing_cycle_end",
|
|
|
|
"downgrade_now_without_additional_licenses",
|
|
|
|
"downgrade_now_void_open_invoices",
|
2023-12-01 19:45:11 +01:00
|
|
|
"upgrade_plan_tier",
|
2021-07-29 17:32:18 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
VALID_STATUS_VALUES = [
|
|
|
|
"active",
|
|
|
|
"deactivated",
|
|
|
|
]
|
|
|
|
|
2023-12-01 13:19:04 +01:00
|
|
|
VALID_BILLING_MODALITY_VALUES = [
|
2021-07-29 17:32:18 +02:00
|
|
|
"send_invoice",
|
|
|
|
"charge_automatically",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-06-17 23:37:52 +02:00
|
|
|
@require_server_admin
|
2021-07-29 17:32:18 +02:00
|
|
|
@has_request_variables
|
|
|
|
def support(
|
|
|
|
request: HttpRequest,
|
|
|
|
realm_id: Optional[int] = REQ(default=None, converter=to_non_negative_int),
|
|
|
|
plan_type: Optional[int] = REQ(default=None, converter=to_non_negative_int),
|
|
|
|
discount: Optional[Decimal] = REQ(default=None, converter=to_decimal),
|
|
|
|
new_subdomain: Optional[str] = REQ(default=None),
|
|
|
|
status: Optional[str] = REQ(default=None, str_validator=check_string_in(VALID_STATUS_VALUES)),
|
2023-12-01 13:19:04 +01:00
|
|
|
billing_modality: Optional[str] = REQ(
|
|
|
|
default=None, str_validator=check_string_in(VALID_BILLING_MODALITY_VALUES)
|
2021-07-29 17:32:18 +02:00
|
|
|
),
|
|
|
|
sponsorship_pending: Optional[bool] = REQ(default=None, json_validator=check_bool),
|
2022-04-27 04:23:43 +02:00
|
|
|
approve_sponsorship: bool = REQ(default=False, json_validator=check_bool),
|
2023-04-11 00:14:41 +02:00
|
|
|
modify_plan: Optional[str] = REQ(
|
|
|
|
default=None, str_validator=check_string_in(VALID_MODIFY_PLAN_METHODS)
|
2021-07-29 17:32:18 +02:00
|
|
|
),
|
2022-04-27 04:23:43 +02:00
|
|
|
scrub_realm: bool = REQ(default=False, json_validator=check_bool),
|
2023-05-15 23:40:35 +02:00
|
|
|
delete_user_by_id: Optional[int] = REQ(default=None, converter=to_non_negative_int),
|
2021-07-29 17:32:18 +02:00
|
|
|
query: Optional[str] = REQ("q", default=None),
|
2021-10-07 21:30:54 +02:00
|
|
|
org_type: Optional[int] = REQ(default=None, converter=to_non_negative_int),
|
2021-07-29 17:32:18 +02:00
|
|
|
) -> HttpResponse:
|
2021-06-17 23:37:52 +02:00
|
|
|
context: Dict[str, Any] = {}
|
|
|
|
|
|
|
|
if "success_message" in request.session:
|
|
|
|
context["success_message"] = request.session["success_message"]
|
|
|
|
del request.session["success_message"]
|
|
|
|
|
2023-11-13 15:05:56 +01:00
|
|
|
acting_user = request.user
|
|
|
|
assert isinstance(acting_user, UserProfile)
|
2021-06-17 23:37:52 +02:00
|
|
|
if settings.BILLING_ENABLED and request.method == "POST":
|
|
|
|
# We check that request.POST only has two keys in it: The
|
|
|
|
# realm_id and a field to change.
|
|
|
|
keys = set(request.POST.keys())
|
|
|
|
if "csrfmiddlewaretoken" in keys:
|
|
|
|
keys.remove("csrfmiddlewaretoken")
|
|
|
|
if len(keys) != 2:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Invalid parameters"))
|
2021-06-17 23:37:52 +02:00
|
|
|
|
2022-06-15 05:17:23 +02:00
|
|
|
assert realm_id is not None
|
2021-06-17 23:37:52 +02:00
|
|
|
realm = Realm.objects.get(id=realm_id)
|
|
|
|
|
2023-11-30 21:11:54 +01:00
|
|
|
support_view_request = None
|
|
|
|
|
|
|
|
if approve_sponsorship:
|
|
|
|
support_view_request = SupportViewRequest(support_type=SupportType.approve_sponsorship)
|
|
|
|
elif sponsorship_pending is not None:
|
|
|
|
support_view_request = SupportViewRequest(
|
|
|
|
support_type=SupportType.update_sponsorship_status,
|
|
|
|
sponsorship_status=sponsorship_pending,
|
|
|
|
)
|
|
|
|
elif discount is not None:
|
|
|
|
support_view_request = SupportViewRequest(
|
|
|
|
support_type=SupportType.attach_discount,
|
|
|
|
discount=discount,
|
|
|
|
)
|
2023-12-01 12:23:31 +01:00
|
|
|
elif billing_modality is not None:
|
|
|
|
support_view_request = SupportViewRequest(
|
|
|
|
support_type=SupportType.update_billing_modality,
|
|
|
|
billing_modality=billing_modality,
|
|
|
|
)
|
2023-12-01 19:45:11 +01:00
|
|
|
elif modify_plan is not None:
|
|
|
|
support_view_request = SupportViewRequest(
|
|
|
|
support_type=SupportType.modify_plan,
|
|
|
|
plan_modification=modify_plan,
|
|
|
|
)
|
|
|
|
if modify_plan == "upgrade_plan_tier":
|
|
|
|
support_view_request["new_plan_tier"] = CustomerPlan.TIER_CLOUD_PLUS
|
2023-11-30 21:11:54 +01:00
|
|
|
elif plan_type is not None:
|
2021-06-17 23:37:52 +02:00
|
|
|
current_plan_type = realm.plan_type
|
2021-12-01 02:10:40 +01:00
|
|
|
do_change_realm_plan_type(realm, plan_type, acting_user=acting_user)
|
2023-12-05 13:09:33 +01:00
|
|
|
msg = f"Plan type of {realm.string_id} changed from {get_plan_type_string(current_plan_type)} to {get_plan_type_string(plan_type)} "
|
2021-06-17 23:37:52 +02:00
|
|
|
context["success_message"] = msg
|
2021-10-07 21:30:54 +02:00
|
|
|
elif org_type is not None:
|
|
|
|
current_realm_type = realm.org_type
|
|
|
|
do_change_realm_org_type(realm, org_type, acting_user=acting_user)
|
|
|
|
msg = f"Org type of {realm.string_id} changed from {get_org_type_display_name(current_realm_type)} to {get_org_type_display_name(org_type)} "
|
|
|
|
context["success_message"] = msg
|
2021-07-29 17:32:18 +02:00
|
|
|
elif new_subdomain is not None:
|
2021-06-17 23:37:52 +02:00
|
|
|
old_subdomain = realm.string_id
|
|
|
|
try:
|
|
|
|
check_subdomain_available(new_subdomain)
|
|
|
|
except ValidationError as error:
|
|
|
|
context["error_message"] = error.message
|
|
|
|
else:
|
2021-07-26 14:23:13 +02:00
|
|
|
do_change_realm_subdomain(realm, new_subdomain, acting_user=acting_user)
|
2021-06-17 23:37:52 +02:00
|
|
|
request.session[
|
|
|
|
"success_message"
|
|
|
|
] = f"Subdomain changed from {old_subdomain} to {new_subdomain}"
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse("support") + "?" + urlencode({"q": new_subdomain})
|
|
|
|
)
|
2021-07-29 17:32:18 +02:00
|
|
|
elif status is not None:
|
2021-06-17 23:37:52 +02:00
|
|
|
if status == "active":
|
2021-07-26 14:23:13 +02:00
|
|
|
do_send_realm_reactivation_email(realm, acting_user=acting_user)
|
2021-06-17 23:37:52 +02:00
|
|
|
context[
|
|
|
|
"success_message"
|
|
|
|
] = f"Realm reactivation email sent to admins of {realm.string_id}."
|
|
|
|
elif status == "deactivated":
|
2021-07-26 14:23:13 +02:00
|
|
|
do_deactivate_realm(realm, acting_user=acting_user)
|
2021-06-17 23:37:52 +02:00
|
|
|
context["success_message"] = f"{realm.string_id} deactivated."
|
2021-08-09 17:25:31 +02:00
|
|
|
elif scrub_realm:
|
2021-07-29 17:32:18 +02:00
|
|
|
do_scrub_realm(realm, acting_user=acting_user)
|
|
|
|
context["success_message"] = f"{realm.string_id} scrubbed."
|
2023-05-15 23:40:35 +02:00
|
|
|
elif delete_user_by_id:
|
|
|
|
user_profile_for_deletion = get_user_profile_by_id(delete_user_by_id)
|
|
|
|
user_email = user_profile_for_deletion.delivery_email
|
|
|
|
assert user_profile_for_deletion.realm == realm
|
|
|
|
do_delete_user_preserving_messages(user_profile_for_deletion)
|
|
|
|
context["success_message"] = f"{user_email} in {realm.subdomain} deleted."
|
2021-06-17 23:37:52 +02:00
|
|
|
|
2023-11-30 21:11:54 +01:00
|
|
|
if support_view_request is not None:
|
|
|
|
billing_session = RealmBillingSession(
|
|
|
|
user=acting_user, realm=realm, support_session=True
|
|
|
|
)
|
|
|
|
success_message = billing_session.process_support_view_request(support_view_request)
|
|
|
|
context["success_message"] = success_message
|
|
|
|
|
2021-06-17 23:37:52 +02:00
|
|
|
if query:
|
|
|
|
key_words = get_invitee_emails_set(query)
|
|
|
|
|
2023-05-16 13:47:56 +02:00
|
|
|
case_insensitive_users_q = Q()
|
|
|
|
for key_word in key_words:
|
|
|
|
case_insensitive_users_q |= Q(delivery_email__iexact=key_word)
|
|
|
|
users = set(UserProfile.objects.filter(case_insensitive_users_q))
|
2021-06-17 23:37:52 +02:00
|
|
|
realms = set(Realm.objects.filter(string_id__in=key_words))
|
|
|
|
|
|
|
|
for key_word in key_words:
|
|
|
|
try:
|
|
|
|
URLValidator()(key_word)
|
2023-12-05 21:25:00 +01:00
|
|
|
parse_result = urlsplit(key_word)
|
2021-06-17 23:37:52 +02:00
|
|
|
hostname = parse_result.hostname
|
|
|
|
assert hostname is not None
|
|
|
|
if parse_result.port:
|
|
|
|
hostname = f"{hostname}:{parse_result.port}"
|
|
|
|
subdomain = get_subdomain_from_hostname(hostname)
|
2023-01-18 05:25:49 +01:00
|
|
|
with suppress(Realm.DoesNotExist):
|
2021-06-17 23:37:52 +02:00
|
|
|
realms.add(get_realm(subdomain))
|
|
|
|
except ValidationError:
|
|
|
|
users.update(UserProfile.objects.filter(full_name__iexact=key_word))
|
|
|
|
|
|
|
|
# full_names can have , in them
|
|
|
|
users.update(UserProfile.objects.filter(full_name__iexact=query))
|
|
|
|
|
|
|
|
context["users"] = users
|
|
|
|
context["realms"] = realms
|
|
|
|
|
|
|
|
confirmations: List[Dict[str, Any]] = []
|
|
|
|
|
2022-07-01 19:35:35 +02:00
|
|
|
preregistration_user_ids = [
|
|
|
|
user.id for user in PreregistrationUser.objects.filter(email__in=key_words)
|
|
|
|
]
|
2021-06-17 23:37:52 +02:00
|
|
|
confirmations += get_confirmations(
|
2023-03-03 11:58:00 +01:00
|
|
|
[Confirmation.USER_REGISTRATION, Confirmation.INVITATION],
|
2022-07-01 19:35:35 +02:00
|
|
|
preregistration_user_ids,
|
2021-06-17 23:37:52 +02:00
|
|
|
hostname=request.get_host(),
|
|
|
|
)
|
|
|
|
|
2023-03-03 11:58:00 +01:00
|
|
|
preregistration_realm_ids = [
|
|
|
|
user.id for user in PreregistrationRealm.objects.filter(email__in=key_words)
|
|
|
|
]
|
|
|
|
confirmations += get_confirmations(
|
|
|
|
[Confirmation.REALM_CREATION],
|
|
|
|
preregistration_realm_ids,
|
|
|
|
hostname=request.get_host(),
|
|
|
|
)
|
|
|
|
|
2022-07-01 19:35:35 +02:00
|
|
|
multiuse_invite_ids = [
|
|
|
|
invite.id for invite in MultiuseInvite.objects.filter(realm__in=realms)
|
|
|
|
]
|
|
|
|
confirmations += get_confirmations([Confirmation.MULTIUSE_INVITE], multiuse_invite_ids)
|
2021-06-17 23:37:52 +02:00
|
|
|
|
2022-07-26 15:48:26 +02:00
|
|
|
realm_reactivation_status_objects = RealmReactivationStatus.objects.filter(realm__in=realms)
|
2021-06-17 23:37:52 +02:00
|
|
|
confirmations += get_confirmations(
|
2022-07-26 15:48:26 +02:00
|
|
|
[Confirmation.REALM_REACTIVATION], [obj.id for obj in realm_reactivation_status_objects]
|
2021-06-17 23:37:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
context["confirmations"] = confirmations
|
|
|
|
|
2022-07-07 21:37:29 +02:00
|
|
|
# We want a union of all realms that might appear in the search result,
|
|
|
|
# but not necessary as a separate result item.
|
|
|
|
# Therefore, we do not modify the realms object in the context.
|
|
|
|
all_realms = realms.union(
|
|
|
|
[
|
|
|
|
confirmation["object"].realm
|
|
|
|
for confirmation in confirmations
|
|
|
|
# For confirmations, we only display realm details when the type is USER_REGISTRATION
|
|
|
|
# or INVITATION.
|
|
|
|
if confirmation["type"] in (Confirmation.USER_REGISTRATION, Confirmation.INVITATION)
|
|
|
|
]
|
|
|
|
+ [user.realm for user in users]
|
|
|
|
)
|
|
|
|
plan_data: Dict[int, PlanData] = {}
|
|
|
|
for realm in all_realms:
|
2023-12-02 17:55:39 +01:00
|
|
|
billing_session = RealmBillingSession(user=None, realm=realm)
|
|
|
|
realm_plan_data = get_current_plan_data_for_support_view(billing_session)
|
|
|
|
plan_data[realm.id] = realm_plan_data
|
2022-07-07 21:37:29 +02:00
|
|
|
context["plan_data"] = plan_data
|
|
|
|
|
2021-06-17 23:37:52 +02:00
|
|
|
def get_realm_owner_emails_as_string(realm: Realm) -> str:
|
|
|
|
return ", ".join(
|
|
|
|
realm.get_human_owner_users()
|
|
|
|
.order_by("delivery_email")
|
|
|
|
.values_list("delivery_email", flat=True)
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_realm_admin_emails_as_string(realm: Realm) -> str:
|
|
|
|
return ", ".join(
|
|
|
|
realm.get_human_admin_users(include_realm_owners=False)
|
|
|
|
.order_by("delivery_email")
|
|
|
|
.values_list("delivery_email", flat=True)
|
|
|
|
)
|
|
|
|
|
|
|
|
context["get_realm_owner_emails_as_string"] = get_realm_owner_emails_as_string
|
|
|
|
context["get_realm_admin_emails_as_string"] = get_realm_admin_emails_as_string
|
2023-12-02 17:45:25 +01:00
|
|
|
context["get_discount"] = get_customer_discount_for_support_view
|
2021-07-02 20:57:37 +02:00
|
|
|
context["get_org_type_display_name"] = get_org_type_display_name
|
2021-06-17 23:37:52 +02:00
|
|
|
context["realm_icon_url"] = realm_icon_url
|
|
|
|
context["Confirmation"] = Confirmation
|
2021-10-07 21:30:54 +02:00
|
|
|
context["sorted_realm_types"] = sorted(
|
|
|
|
Realm.ORG_TYPES.values(), key=lambda d: d["display_order"]
|
|
|
|
)
|
|
|
|
|
2021-06-17 23:37:52 +02:00
|
|
|
return render(request, "analytics/support.html", context=context)
|
2023-10-04 13:38:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_remote_servers_for_support(
|
|
|
|
email_to_search: Optional[str], hostname_to_search: Optional[str]
|
|
|
|
) -> List["RemoteZulipServer"]:
|
|
|
|
if not email_to_search and not hostname_to_search:
|
|
|
|
return []
|
|
|
|
|
2023-12-05 16:10:15 +01:00
|
|
|
remote_servers_query = RemoteZulipServer.objects.order_by("id").prefetch_related(
|
|
|
|
"remoterealm_set"
|
|
|
|
)
|
2023-10-04 13:38:12 +02:00
|
|
|
if email_to_search:
|
|
|
|
remote_servers_query = remote_servers_query.filter(contact_email__iexact=email_to_search)
|
|
|
|
elif hostname_to_search:
|
|
|
|
remote_servers_query = remote_servers_query.filter(hostname__icontains=hostname_to_search)
|
|
|
|
|
|
|
|
return list(remote_servers_query)
|
|
|
|
|
|
|
|
|
|
|
|
@require_server_admin
|
|
|
|
@has_request_variables
|
|
|
|
def remote_servers_support(
|
2023-12-02 18:13:39 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
query: Optional[str] = REQ("q", default=None),
|
|
|
|
remote_server_id: Optional[int] = REQ(default=None, converter=to_non_negative_int),
|
2023-12-11 17:29:32 +01:00
|
|
|
remote_realm_id: Optional[int] = REQ(default=None, converter=to_non_negative_int),
|
2023-12-02 18:13:39 +01:00
|
|
|
discount: Optional[Decimal] = REQ(default=None, converter=to_decimal),
|
2023-12-02 18:18:32 +01:00
|
|
|
sponsorship_pending: Optional[bool] = REQ(default=None, json_validator=check_bool),
|
2023-12-02 18:21:04 +01:00
|
|
|
approve_sponsorship: bool = REQ(default=False, json_validator=check_bool),
|
2023-12-04 22:44:04 +01:00
|
|
|
billing_modality: Optional[str] = REQ(
|
|
|
|
default=None, str_validator=check_string_in(VALID_BILLING_MODALITY_VALUES)
|
|
|
|
),
|
2023-12-04 23:20:49 +01:00
|
|
|
modify_plan: Optional[str] = REQ(
|
|
|
|
default=None, str_validator=check_string_in(VALID_MODIFY_PLAN_METHODS)
|
|
|
|
),
|
2023-10-04 13:38:12 +02:00
|
|
|
) -> HttpResponse:
|
2023-12-02 18:05:40 +01:00
|
|
|
context: Dict[str, Any] = {}
|
|
|
|
|
|
|
|
if "success_message" in request.session:
|
|
|
|
context["success_message"] = request.session["success_message"]
|
|
|
|
del request.session["success_message"]
|
|
|
|
|
2023-12-02 18:13:39 +01:00
|
|
|
acting_user = request.user
|
|
|
|
assert isinstance(acting_user, UserProfile)
|
|
|
|
if settings.BILLING_ENABLED and request.method == "POST":
|
2023-12-11 17:29:32 +01:00
|
|
|
# We check that request.POST only has two keys in it:
|
|
|
|
# either the remote_server_id or a remote_realm_id,
|
|
|
|
# and a field to change.
|
2023-12-02 18:13:39 +01:00
|
|
|
keys = set(request.POST.keys())
|
|
|
|
if "csrfmiddlewaretoken" in keys:
|
|
|
|
keys.remove("csrfmiddlewaretoken")
|
|
|
|
if len(keys) != 2:
|
|
|
|
raise JsonableError(_("Invalid parameters"))
|
|
|
|
|
2023-12-11 17:29:32 +01:00
|
|
|
if remote_realm_id is not None:
|
|
|
|
remote_realm_support_request = True
|
|
|
|
remote_realm = RemoteRealm.objects.get(id=remote_realm_id)
|
|
|
|
else:
|
|
|
|
assert remote_server_id is not None
|
|
|
|
remote_realm_support_request = False
|
|
|
|
remote_server = RemoteZulipServer.objects.get(id=remote_server_id)
|
2023-12-02 18:13:39 +01:00
|
|
|
|
|
|
|
support_view_request = None
|
|
|
|
|
2023-12-02 18:21:04 +01:00
|
|
|
if approve_sponsorship:
|
|
|
|
support_view_request = SupportViewRequest(support_type=SupportType.approve_sponsorship)
|
|
|
|
elif sponsorship_pending is not None:
|
2023-12-02 18:18:32 +01:00
|
|
|
support_view_request = SupportViewRequest(
|
|
|
|
support_type=SupportType.update_sponsorship_status,
|
|
|
|
sponsorship_status=sponsorship_pending,
|
|
|
|
)
|
|
|
|
elif discount is not None:
|
2023-12-02 18:13:39 +01:00
|
|
|
support_view_request = SupportViewRequest(
|
|
|
|
support_type=SupportType.attach_discount,
|
|
|
|
discount=discount,
|
|
|
|
)
|
2023-12-04 22:44:04 +01:00
|
|
|
elif billing_modality is not None:
|
|
|
|
support_view_request = SupportViewRequest(
|
|
|
|
support_type=SupportType.update_billing_modality,
|
|
|
|
billing_modality=billing_modality,
|
|
|
|
)
|
2023-12-04 23:20:49 +01:00
|
|
|
elif modify_plan is not None:
|
|
|
|
support_view_request = SupportViewRequest(
|
|
|
|
support_type=SupportType.modify_plan,
|
|
|
|
plan_modification=modify_plan,
|
|
|
|
)
|
2023-12-02 18:13:39 +01:00
|
|
|
if support_view_request is not None:
|
2023-12-11 17:29:32 +01:00
|
|
|
if remote_realm_support_request:
|
|
|
|
success_message = RemoteRealmBillingSession(
|
|
|
|
support_staff=acting_user, remote_realm=remote_realm
|
|
|
|
).process_support_view_request(support_view_request)
|
|
|
|
else:
|
|
|
|
success_message = RemoteServerBillingSession(
|
|
|
|
support_staff=acting_user, remote_server=remote_server
|
|
|
|
).process_support_view_request(support_view_request)
|
2023-12-02 18:13:39 +01:00
|
|
|
context["success_message"] = success_message
|
|
|
|
|
2023-10-04 13:38:12 +02:00
|
|
|
email_to_search = None
|
|
|
|
hostname_to_search = None
|
|
|
|
if query:
|
|
|
|
if "@" in query:
|
|
|
|
email_to_search = query
|
|
|
|
else:
|
|
|
|
hostname_to_search = query
|
|
|
|
|
|
|
|
remote_servers = get_remote_servers_for_support(
|
|
|
|
email_to_search=email_to_search, hostname_to_search=hostname_to_search
|
|
|
|
)
|
2023-10-18 13:18:12 +02:00
|
|
|
remote_server_to_max_monthly_messages: Dict[int, Union[int, str]] = dict()
|
2023-12-13 18:23:05 +01:00
|
|
|
server_support_data: Dict[int, SupportData] = {}
|
|
|
|
realm_support_data: Dict[int, SupportData] = {}
|
2023-12-05 16:10:15 +01:00
|
|
|
remote_realms: Dict[int, List[RemoteRealm]] = {}
|
2023-10-18 13:18:12 +02:00
|
|
|
for remote_server in remote_servers:
|
2023-12-11 17:29:32 +01:00
|
|
|
# Get remote realms attached to remote server
|
2023-12-12 15:49:01 +01:00
|
|
|
remote_realms_for_server = list(
|
|
|
|
remote_server.remoterealm_set.exclude(is_system_bot_realm=True)
|
|
|
|
)
|
2023-12-05 16:10:15 +01:00
|
|
|
remote_realms[remote_server.id] = remote_realms_for_server
|
2023-12-11 17:29:32 +01:00
|
|
|
# Get plan data for remote realms
|
|
|
|
for remote_realm in remote_realms[remote_server.id]:
|
|
|
|
realm_billing_session = RemoteRealmBillingSession(remote_realm=remote_realm)
|
2023-12-13 18:23:05 +01:00
|
|
|
remote_realm_data = get_data_for_support_view(realm_billing_session)
|
|
|
|
realm_support_data[remote_realm.id] = remote_realm_data
|
2023-12-11 17:29:32 +01:00
|
|
|
# Get plan data for remote server
|
|
|
|
server_billing_session = RemoteServerBillingSession(remote_server=remote_server)
|
2023-12-13 18:23:05 +01:00
|
|
|
remote_server_data = get_data_for_support_view(server_billing_session)
|
|
|
|
server_support_data[remote_server.id] = remote_server_data
|
2023-12-11 17:29:32 +01:00
|
|
|
# Get max monthly messages
|
2023-10-18 13:18:12 +02:00
|
|
|
try:
|
|
|
|
remote_server_to_max_monthly_messages[remote_server.id] = compute_max_monthly_messages(
|
|
|
|
remote_server
|
|
|
|
)
|
|
|
|
except MissingDataError:
|
|
|
|
remote_server_to_max_monthly_messages[remote_server.id] = "Recent data missing"
|
|
|
|
|
2023-12-02 18:05:40 +01:00
|
|
|
context["remote_servers"] = remote_servers
|
2023-12-13 18:23:05 +01:00
|
|
|
context["remote_servers_support_data"] = server_support_data
|
2023-12-02 18:05:40 +01:00
|
|
|
context["remote_server_to_max_monthly_messages"] = remote_server_to_max_monthly_messages
|
2023-12-11 17:29:32 +01:00
|
|
|
context["remote_realms"] = remote_realms
|
2023-12-13 18:23:05 +01:00
|
|
|
context["remote_realms_support_data"] = realm_support_data
|
2023-12-05 13:09:33 +01:00
|
|
|
context["get_plan_type_name"] = get_plan_type_string
|
2023-12-05 16:10:15 +01:00
|
|
|
context["get_org_type_display_name"] = get_org_type_display_name
|
2023-12-11 17:09:36 +01:00
|
|
|
context["SPONSORED_PLAN_TYPE"] = RemoteZulipServer.PLAN_TYPE_COMMUNITY
|
2023-12-02 18:05:40 +01:00
|
|
|
|
2023-10-04 13:38:12 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"analytics/remote_server_support.html",
|
2023-12-02 18:05:40 +01:00
|
|
|
context=context,
|
2023-10-04 13:38:12 +02:00
|
|
|
)
|