Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
support: Add functionality to approve sponsorship requests.
Vishnu KS5b0b1efb155a
diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.pyindex 2bafd65f25b..5ef05fc8342 100644--- a/analytics/tests/test_views.py+++ b/analytics/tests/test_views.py@@ -10,13 +10,21 @@ from analytics.lib.counts import COUNT_STATS, CountStatfrom analytics.lib.time_utils import time_rangefrom analytics.models import FillState, RealmCount, UserCount, last_successful_fillfrom analytics.views import rewrite_client_arrays, sort_by_totals, sort_client_labels-from corporate.lib.stripe import add_months+from corporate.lib.stripe import add_months, update_sponsorship_statusfrom corporate.models import Customer, CustomerPlan, LicenseLedger, get_customer_by_realmfrom zerver.lib.actions import do_create_multiuse_invite_link, do_send_realm_reactivation_emailfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.test_helpers import reset_emails_in_zulip_realmfrom zerver.lib.timestamp import ceiling_to_day, ceiling_to_hour, datetime_to_timestamp-from zerver.models import Client, MultiuseInvite, PreregistrationUser, get_realm+from zerver.models import (+ Client,+ MultiuseInvite,+ PreregistrationUser,+ Realm,+ UserMessage,+ UserProfile,+ get_realm,+)class TestStatsEndpoint(ZulipTestCase):@@ -622,6 +630,36 @@ class TestSupportEndpoint(ZulipTestCase):assert(customer is not None)self.assertFalse(customer.sponsorship_pending)+ def test_approve_sponsorship(self) -> None:+ lear_realm = get_realm("lear")+ update_sponsorship_status(lear_realm, True)+ king_user = self.lear_user("king")+ king_user.role = UserProfile.ROLE_REALM_OWNER+ king_user.save()++ cordelia = self.example_user('cordelia')+ self.login_user(cordelia)++ result = self.client_post("/activity/support", {"realm_id": f"{lear_realm.id}",+ "approve_sponsorship": "approve_sponsorship"})+ self.assertEqual(result.status_code, 302)+ self.assertEqual(result["Location"], "/login/")++ iago = self.example_user("iago")+ self.login_user(iago)++ result = self.client_post("/activity/support", {"realm_id": f"{lear_realm.id}",+ "approve_sponsorship": "approve_sponsorship"})+ self.assert_in_success_response(["Sponsorship approved for Lear & Co."], result)+ lear_realm.refresh_from_db()+ self.assertEqual(lear_realm.plan_type, Realm.STANDARD_FREE)+ customer = get_customer_by_realm(lear_realm)+ assert(customer is not None)+ self.assertFalse(customer.sponsorship_pending)+ messages = UserMessage.objects.filter(user_profile=king_user)+ self.assertIn("request for sponsored hosting has been approved", messages[0].message.content)+ self.assertEqual(len(messages), 1)+def test_activate_or_deactivate_realm(self) -> None:cordelia = self.example_user('cordelia')lear_realm = get_realm('lear')diff --git a/analytics/views.py b/analytics/views.pyindex c2536dc9cc4..93aefd2acba 100644--- a/analytics/views.py+++ b/analytics/views.py@@ -71,6 +71,7 @@ from zerver.views.invite import get_invitee_emails_setif settings.BILLING_ENABLED:from corporate.lib.stripe import (+ approve_sponsorship,attach_discount_to_realm,get_current_plan_by_realm,get_customer_by_realm,@@ -1157,6 +1158,10 @@ def support(request: HttpRequest) -> HttpResponse:elif sponsorship_pending == "false":update_sponsorship_status(realm, False)context["message"] = f"{realm.name} is no longer pending sponsorship."+ elif request.POST.get('approve_sponsorship') is not None:+ if request.POST.get('approve_sponsorship') == "approve_sponsorship":+ approve_sponsorship(realm)+ context["message"] = f"Sponsorship approved for {realm.name}"elif request.POST.get("scrub_realm", None) is not None:if request.POST.get("scrub_realm") == "scrub_realm":do_scrub_realm(realm, acting_user=request.user)diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 21d7dca3145..19e2ccac252 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -12,6 +12,7 @@ from django.conf import settingsfrom django.core.signing import Signerfrom django.db import transactionfrom django.utils.timezone import now as timezone_now+from django.utils.translation import override as override_languagefrom django.utils.translation import ugettext as _from corporate.models import (@@ -25,7 +26,7 @@ from corporate.models import (from zerver.lib.logging_util import log_to_filefrom zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetimefrom zerver.lib.utils import generate_random_token-from zerver.models import Realm, RealmAuditLog, UserProfile+from zerver.models import Realm, RealmAuditLog, UserProfile, get_system_botfrom zproject.config import get_secretSTRIPE_PUBLISHABLE_KEY = get_secret('stripe_publishable_key')@@ -563,6 +564,24 @@ def update_sponsorship_status(realm: Realm, sponsorship_pending: bool) -> None:customer.sponsorship_pending = sponsorship_pendingcustomer.save(update_fields=["sponsorship_pending"])+def approve_sponsorship(realm: Realm) -> None:+ from zerver.lib.actions import do_change_plan_type, internal_send_private_message+ do_change_plan_type(realm, Realm.STANDARD_FREE)+ customer = get_customer_by_realm(realm)+ if customer is not None and customer.sponsorship_pending:+ customer.sponsorship_pending = False+ customer.save(update_fields=["sponsorship_pending"])+ notification_bot = get_system_bot(settings.NOTIFICATION_BOT)+ for billing_admin in realm.get_human_billing_admin_users():+ with override_language(billing_admin.default_language):+ # Using variable to make life easier for translators if these details change.+ plan_name = "Zulip Cloud Standard"+ emoji = ":tada:"+ message = _(+ f"Your organization's request for sponsored hosting has been approved! {emoji}.\n"+ f"You have been upgraded to {plan_name}, free of charge.")+ internal_send_private_message(billing_admin.realm, notification_bot, billing_admin, message)+def get_discount_for_realm(realm: Realm) -> Optional[Decimal]:customer = get_customer_by_realm(realm)if customer is not None:diff --git a/static/styles/portico/activity.scss b/static/styles/portico/activity.scssindex db7c0e338e5..a79c9219d2a 100644--- a/static/styles/portico/activity.scss+++ b/static/styles/portico/activity.scss@@ -89,6 +89,11 @@ tr.admin td:first-child {top: -50px;}+.approve-sponsorship-form {+ position: relative;+ top: -40px;+}+.scrub-realm-form {position: relative;top: -40px;diff --git a/templates/analytics/realm_details.html b/templates/analytics/realm_details.htmlindex 9672c135890..de40620b7bb 100644--- a/templates/analytics/realm_details.html+++ b/templates/analytics/realm_details.html@@ -40,6 +40,19 @@</select><button type="submit" class="button rounded small support-submit-button">Update</button></form>++{% if realm.customer and realm.customer.sponsorship_pending %}+<form method="POST" class="approve-sponsorship-form">+ {{ csrf_input }}+ <input type="hidden" name="realm_id" value="{{ realm.id }}" />+ <input type="hidden" name="approve_sponsorship" value="approve_sponsorship" />+ <button class="button rounded btn sea-green small approve-sponsorship-button">+ Approve full sponsorship+ </button>+ (will email organization owners).+</form>+{% endif %}+<form method="POST" class="support-discount-form"><b>Discount (use 85 for nonprofits)</b>:<br>{{ csrf_input }}diff --git a/zerver/models.py b/zerver/models.pyindex dce3d5cd6ea..08f55c0556d 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -501,6 +501,10 @@ class Realm(models.Model):role__in=[UserProfile.ROLE_REALM_ADMINISTRATOR,UserProfile.ROLE_REALM_OWNER])+ def get_human_billing_admin_users(self) -> Sequence['UserProfile']:+ return UserProfile.objects.filter(Q(role=UserProfile.ROLE_REALM_OWNER) | Q(is_billing_admin=True),+ realm=self, is_bot=False, is_active=True)+def get_active_users(self) -> Sequence['UserProfile']:# TODO: Change return type to QuerySet[UserProfile]return UserProfile.objects.filter(realm=self, is_active=True).select_related()