Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

support: Get plan data via BillingSession for support views.

Lauryn Menard6d66248d3d8d
diff --git a/analytics/views/support.py b/analytics/views/support.py
index 5fe128c469e..f89af878085 100644
--- a/analytics/views/support.py
+++ b/analytics/views/support.py
@@ -1,6 +1,5 @@
import urllib
from contextlib import suppress
-from dataclasses import dataclass
from datetime import timedelta
from decimal import Decimal
from typing import Any, Dict, Iterable, List, Optional, Union
@@ -53,19 +52,13 @@ if settings.ZILENCER_ENABLED:
from zilencer.models import RemoteZulipServer
if settings.BILLING_ENABLED:
- from corporate.lib.stripe import (
- RealmBillingSession,
- SupportType,
- SupportViewRequest,
- get_latest_seat_count,
- )
- from corporate.lib.support import get_customer_discount_for_support_view
- from corporate.models import (
- Customer,
- CustomerPlan,
- get_current_plan_by_realm,
- get_customer_by_realm,
+ from corporate.lib.stripe import RealmBillingSession, SupportType, SupportViewRequest
+ from corporate.lib.support import (
+ PlanData,
+ get_current_plan_data_for_support_view,
+ get_customer_discount_for_support_view,
)
+ from corporate.models import CustomerPlan
def get_plan_name(plan_type: int) -> str:
@@ -141,14 +134,6 @@ VALID_BILLING_MODALITY_VALUES = [
]
-@dataclass
-class PlanData:
- customer: Optional["Customer"] = None
- current_plan: Optional["CustomerPlan"] = None
- licenses: Optional[int] = None
- licenses_used: Optional[int] = None
-
-
@require_server_admin
@has_request_variables
def support(
@@ -343,23 +328,9 @@ def support(
)
plan_data: Dict[int, PlanData] = {}
for realm in all_realms:
- current_plan = get_current_plan_by_realm(realm)
- plan_data[realm.id] = PlanData(
- customer=get_customer_by_realm(realm),
- current_plan=current_plan,
- )
- if current_plan is not None:
- billing_session = RealmBillingSession(user=None, realm=realm)
- new_plan, last_ledger_entry = billing_session.make_end_of_cycle_updates_if_needed(
- current_plan, timezone_now()
- )
- if last_ledger_entry is not None:
- if new_plan is not None:
- plan_data[realm.id].current_plan = new_plan
- else:
- plan_data[realm.id].current_plan = current_plan
- plan_data[realm.id].licenses = last_ledger_entry.licenses
- plan_data[realm.id].licenses_used = get_latest_seat_count(realm)
+ 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
context["plan_data"] = plan_data
def get_realm_owner_emails_as_string(realm: Realm) -> str:
diff --git a/corporate/lib/support.py b/corporate/lib/support.py
index b5fc8d7d1e9..4263ad4d08f 100644
--- a/corporate/lib/support.py
+++ b/corporate/lib/support.py
@@ -1,14 +1,25 @@
+from dataclasses import dataclass
from decimal import Decimal
from typing import Optional
from urllib.parse import urlencode, urljoin, urlunsplit
from django.conf import settings
from django.urls import reverse
+from django.utils.timezone import now as timezone_now
-from corporate.models import Customer
+from corporate.lib.stripe import BillingSession
+from corporate.models import Customer, CustomerPlan, get_current_plan_by_customer
from zerver.models import Realm, get_realm
+@dataclass
+class PlanData:
+ customer: Optional["Customer"] = None
+ current_plan: Optional["CustomerPlan"] = None
+ licenses: Optional[int] = None
+ licenses_used: Optional[int] = None
+
+
def get_support_url(realm: Realm) -> str:
support_realm_uri = get_realm(settings.STAFF_SUBDOMAIN).uri
support_url = urljoin(
@@ -24,3 +35,25 @@ def get_customer_discount_for_support_view(
if customer is None:
return None
return customer.default_discount
+
+
+def get_current_plan_data_for_support_view(billing_session: BillingSession) -> PlanData:
+ customer = billing_session.get_customer()
+ plan = None
+ if customer is not None:
+ plan = get_current_plan_by_customer(customer)
+ plan_data = PlanData(
+ customer=customer,
+ current_plan=plan,
+ )
+ if plan is not None:
+ new_plan, last_ledger_entry = billing_session.make_end_of_cycle_updates_if_needed(
+ plan, timezone_now()
+ )
+ if last_ledger_entry is not None:
+ if new_plan is not None:
+ plan_data.current_plan = new_plan # nocoverage
+ plan_data.licenses = last_ledger_entry.licenses
+ plan_data.licenses_used = billing_session.current_count_for_billed_licenses()
+
+ return plan_data