Skip to content

Commit

Permalink
support: Add ability to change subdomain of realms.
Browse files Browse the repository at this point in the history
  • Loading branch information
edith007 authored and alexmv committed Dec 6, 2020
1 parent 37e158b commit 6f962c1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
25 changes: 25 additions & 0 deletions analytics/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,31 @@ def test_activate_or_deactivate_realm(self) -> None:
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)
Expand Down
21 changes: 20 additions & 1 deletion analytics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from datetime import datetime, timedelta, timezone
from decimal import Decimal
from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple, Type, Union
from urllib.parse import urlencode

import pytz
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
from django.db import connection
from django.db.models.query import QuerySet
from django.http import HttpRequest, HttpResponse, HttpResponseNotFound
from django.http import HttpRequest, HttpResponse, HttpResponseNotFound, HttpResponseRedirect
from django.shortcuts import render
from django.template import loader
from django.urls import reverse
Expand Down Expand Up @@ -45,8 +46,10 @@
to_utc_datetime,
zulip_login_required,
)
from zerver.forms import check_subdomain_available
from zerver.lib.actions import (
do_change_plan_type,
do_change_realm_subdomain,
do_deactivate_realm,
do_scrub_realm,
do_send_realm_reactivation_email,
Expand Down Expand Up @@ -1108,6 +1111,11 @@ def get_confirmations(types: List[int], object_ids: List[int],
@require_server_admin
def 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.
Expand All @@ -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":
Expand Down
6 changes: 3 additions & 3 deletions static/styles/portico/activity.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ tr.admin td:first-child {

.support-plan-type-form {
position: relative;
top: -25px;
top: -10px;
}

.sponsorship-pending-form {
Expand All @@ -86,7 +86,7 @@ tr.admin td:first-child {

.current-plan-details {
position: relative;
top: -50px;
top: -15px;
}

.approve-sponsorship-form {
Expand Down Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions templates/analytics/realm_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ <h3><img src="{{ realm_icon_url(realm) }}" class="support-realm-icon"> {{ realm.
</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 }}
Expand Down
9 changes: 7 additions & 2 deletions templates/analytics/support.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<title>Info</title>
{% endblock %}


{% block content %}
<div class="container">
<br>
Expand All @@ -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 }}
Expand Down

0 comments on commit 6f962c1

Please sign in to comment.