Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
support: Add ability to change subdomain of realms.
Siddharth Asthana6f962c1815a3
diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.pyindex a9b5df167c5..d027342d39a 100644--- a/analytics/tests/test_views.py+++ b/analytics/tests/test_views.py@@ -714,6 +714,31 @@ class TestSupportEndpoint(ZulipTestCase):m.assert_called_once_with(lear_realm)self.assert_in_success_response(["Realm reactivation email sent to admins of lear"], result)+ def test_change_subdomain(self) -> None:+ cordelia = self.example_user('cordelia')+ lear_realm = get_realm('lear')+ self.login_user(cordelia)++ result = self.client_post("/activity/support", {"realm_id": f"{lear_realm.id}",+ "new_subdomain": "new_name"})+ self.assertEqual(result.status_code, 302)+ self.assertEqual(result["Location"], "/login/")+ self.login('iago')++ result = self.client_post("/activity/support", {"realm_id": f"{lear_realm.id}", "new_subdomain": "new-name"})+ self.assertEqual(result.status_code, 302)+ self.assertEqual(result["Location"], "/activity/support?q=new-name")+ realm_id = lear_realm.id+ lear_realm = get_realm('new-name')+ self.assertEqual(lear_realm.id, realm_id)+ self.assertFalse(Realm.objects.filter(string_id='lear').exists())++ result = self.client_post("/activity/support", {"realm_id": f"{lear_realm.id}", "new_subdomain": "new-name"})+ self.assert_in_success_response(["Subdomain unavailable. Please choose a different one."], result)++ result = self.client_post("/activity/support", {"realm_id": f"{lear_realm.id}", "new_subdomain": "zulip"})+ self.assert_in_success_response(["Subdomain unavailable. Please choose a different one."], result)+def test_downgrade_realm(self) -> None:cordelia = self.example_user('cordelia')self.login_user(cordelia)diff --git a/analytics/views.py b/analytics/views.pyindex b77b075c329..6770ce46573 100644--- a/analytics/views.py+++ b/analytics/views.py@@ -7,6 +7,7 @@ from collections import defaultdictfrom datetime import datetime, timedelta, timezonefrom decimal import Decimalfrom typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple, Type, Union+from urllib.parse import urlencodeimport pytzfrom django.conf import settings@@ -14,7 +15,7 @@ from django.core.exceptions import ValidationErrorfrom django.core.validators import URLValidatorfrom django.db import connectionfrom django.db.models.query import QuerySet-from django.http import HttpRequest, HttpResponse, HttpResponseNotFound+from django.http import HttpRequest, HttpResponse, HttpResponseNotFound, HttpResponseRedirectfrom django.shortcuts import renderfrom django.template import loaderfrom django.urls import reverse@@ -45,8 +46,10 @@ from zerver.decorator import (to_utc_datetime,zulip_login_required,)+from zerver.forms import check_subdomain_availablefrom zerver.lib.actions import (do_change_plan_type,+ do_change_realm_subdomain,do_deactivate_realm,do_scrub_realm,do_send_realm_reactivation_email,@@ -1108,6 +1111,11 @@ def get_confirmations(types: List[int], object_ids: List[int],@require_server_admindef support(request: HttpRequest) -> HttpResponse:context: Dict[str, Any] = {}++ if "success_message" in request.session:+ context["success_message"] = request.session["success_message"]+ del request.session["success_message"]+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.@@ -1132,6 +1140,17 @@ def support(request: HttpRequest) -> HttpResponse:attach_discount_to_realm(realm, new_discount)msg = f"Discount of {realm.string_id} changed to {new_discount} from {current_discount} "context["success_message"] = msg+ elif request.POST.get("new_subdomain", None) is not None:+ new_subdomain = request.POST.get("new_subdomain")+ old_subdomain = realm.string_id+ try:+ check_subdomain_available(new_subdomain)+ except ValidationError as error:+ context["error_message"] = error.message+ else:+ do_change_realm_subdomain(realm, new_subdomain)+ request.session["success_message"] = f"Subdomain changed from {old_subdomain} to {new_subdomain}"+ return HttpResponseRedirect(reverse('support') + '?' + urlencode({'q': new_subdomain}))elif request.POST.get("status", None) is not None:status = request.POST.get("status")if status == "active":diff --git a/static/styles/portico/activity.css b/static/styles/portico/activity.cssindex 4e71acea8e6..b1df095ccee 100644--- a/static/styles/portico/activity.css+++ b/static/styles/portico/activity.css@@ -76,7 +76,7 @@ tr.admin td:first-child {.support-plan-type-form {position: relative;- top: -25px;+ top: -10px;}.sponsorship-pending-form {@@ -86,7 +86,7 @@ tr.admin td:first-child {.current-plan-details {position: relative;- top: -50px;+ top: -15px;}.approve-sponsorship-form {@@ -120,7 +120,7 @@ tr.admin td:first-child {.support-submit-button {position: relative;- top: -5px;+ top: -3px;border-color: hsl(0, 0%, 83%);border-radius: 2px;}diff --git a/templates/analytics/realm_details.html b/templates/analytics/realm_details.htmlindex aa00338ef58..9be5b16e472 100644--- a/templates/analytics/realm_details.html+++ b/templates/analytics/realm_details.html@@ -18,6 +18,13 @@</select><button type="submit" class="button rounded small support-submit-button">Update</button></form>+<form method="POST">+ <b>New subdomain</b>:<br>+ {{ csrf_input }}+ <input type="hidden" name="realm_id" value="{{ realm.id }}" />+ <input type="text" name="new_subdomain" required>+ <button type="submit" class="button rounded small support-submit-button">Update</button>+</form><form method="POST" class="support-plan-type-form"><b>Plan type</b>:<br>{{ csrf_input }}diff --git a/templates/analytics/support.html b/templates/analytics/support.htmlindex beda625a0fe..4bd256c70bc 100644--- a/templates/analytics/support.html+++ b/templates/analytics/support.html@@ -7,7 +7,6 @@<title>Info</title>{% endblock %}-{% block content %}<div class="container"><br>@@ -18,7 +17,13 @@</center></form>- {% if success_message %}+ {% if error_message %}+ <div class="alert alert-danger">+ <center>+ {{ error_message }}+ </center>+ </div>+ {% elif success_message %}<div class="alert alert-success"><center>{{ success_message }}