support: Include sponsorship request for remote server support view.

Preparation for updating the sponsorship forms template to include
information about the latest sponsorship request if sponsorship is
pending.
This commit is contained in:
Lauryn Menard 2023-12-13 18:23:05 +01:00 committed by Tim Abbott
parent 35b644c564
commit e8500fbdb0
5 changed files with 105 additions and 30 deletions

View File

@ -60,8 +60,10 @@ if settings.BILLING_ENABLED:
)
from corporate.lib.support import (
PlanData,
SupportData,
get_current_plan_data_for_support_view,
get_customer_discount_for_support_view,
get_data_for_support_view,
)
from corporate.models import CustomerPlan
@ -476,8 +478,8 @@ def remote_servers_support(
email_to_search=email_to_search, hostname_to_search=hostname_to_search
)
remote_server_to_max_monthly_messages: Dict[int, Union[int, str]] = dict()
server_plan_data: Dict[int, PlanData] = {}
realm_plan_data: Dict[int, PlanData] = {}
server_support_data: Dict[int, SupportData] = {}
realm_support_data: Dict[int, SupportData] = {}
remote_realms: Dict[int, List[RemoteRealm]] = {}
for remote_server in remote_servers:
# Get remote realms attached to remote server
@ -488,12 +490,12 @@ def remote_servers_support(
# Get plan data for remote realms
for remote_realm in remote_realms[remote_server.id]:
realm_billing_session = RemoteRealmBillingSession(remote_realm=remote_realm)
remote_realm_plan_data = get_current_plan_data_for_support_view(realm_billing_session)
realm_plan_data[remote_realm.id] = remote_realm_plan_data
remote_realm_data = get_data_for_support_view(realm_billing_session)
realm_support_data[remote_realm.id] = remote_realm_data
# Get plan data for remote server
server_billing_session = RemoteServerBillingSession(remote_server=remote_server)
remote_server_plan_data = get_current_plan_data_for_support_view(server_billing_session)
server_plan_data[remote_server.id] = remote_server_plan_data
remote_server_data = get_data_for_support_view(server_billing_session)
server_support_data[remote_server.id] = remote_server_data
# Get max monthly messages
try:
remote_server_to_max_monthly_messages[remote_server.id] = compute_max_monthly_messages(
@ -503,11 +505,10 @@ def remote_servers_support(
remote_server_to_max_monthly_messages[remote_server.id] = "Recent data missing"
context["remote_servers"] = remote_servers
context["remote_servers_plan_data"] = server_plan_data
context["remote_servers_support_data"] = server_support_data
context["remote_server_to_max_monthly_messages"] = remote_server_to_max_monthly_messages
context["remote_realms"] = remote_realms
context["remote_realms_plan_data"] = realm_plan_data
context["get_discount"] = get_customer_discount_for_support_view
context["remote_realms_support_data"] = realm_support_data
context["get_plan_type_name"] = get_plan_type_string
context["get_org_type_display_name"] = get_org_type_display_name
context["SPONSORED_PLAN_TYPE"] = RemoteZulipServer.PLAN_TYPE_COMMUNITY

View File

@ -1,6 +1,6 @@
from dataclasses import dataclass
from decimal import Decimal
from typing import Optional
from typing import Optional, TypedDict
from urllib.parse import urlencode, urljoin, urlunsplit
from django.conf import settings
@ -8,11 +8,33 @@ from django.urls import reverse
from django.utils.timezone import now as timezone_now
from corporate.lib.stripe import BillingSession
from corporate.models import Customer, CustomerPlan, get_current_plan_by_customer
from zerver.models import Realm, get_realm
from corporate.models import (
Customer,
CustomerPlan,
ZulipSponsorshipRequest,
get_current_plan_by_customer,
)
from zerver.models import Realm, get_org_type_display_name, get_realm
from zilencer.lib.remote_counts import MissingDataError
class SponsorshipRequestDict(TypedDict):
org_type: str
org_website: str
org_description: str
total_users: str
paid_users: str
paid_users_description: str
requested_plan: str
@dataclass
class SponsorshipData:
sponsorship_pending: bool = False
default_discount: Optional[Decimal] = None
latest_sponsorship_request: Optional[SponsorshipRequestDict] = None
@dataclass
class PlanData:
customer: Optional["Customer"] = None
@ -24,6 +46,12 @@ class PlanData:
warning: Optional[str] = None
@dataclass
class SupportData:
plan_data: PlanData
sponsorship_data: SponsorshipData
def get_realm_support_url(realm: Realm) -> str:
support_realm_uri = get_realm(settings.STAFF_SUBDOMAIN).uri
support_url = urljoin(
@ -41,6 +69,40 @@ def get_customer_discount_for_support_view(
return customer.default_discount
def get_customer_sponsorship_data(customer: Customer) -> SponsorshipData:
pending = customer.sponsorship_pending
discount = customer.default_discount
sponsorship_request = None
if pending:
last_sponsorship_request = (
ZulipSponsorshipRequest.objects.filter(customer=customer).order_by("id").last()
)
if last_sponsorship_request is not None:
org_type_name = get_org_type_display_name(last_sponsorship_request.org_type)
if (
last_sponsorship_request.org_website is None
or last_sponsorship_request.org_website == ""
):
website = "No website submitted"
else:
website = last_sponsorship_request.org_website
sponsorship_request = SponsorshipRequestDict(
org_type=org_type_name,
org_website=website,
org_description=last_sponsorship_request.org_description,
total_users=last_sponsorship_request.expected_total_users,
paid_users=last_sponsorship_request.paid_users_count,
paid_users_description=last_sponsorship_request.paid_users_description,
requested_plan=last_sponsorship_request.requested_plan,
)
return SponsorshipData(
sponsorship_pending=pending,
default_discount=discount,
latest_sponsorship_request=sponsorship_request,
)
def get_current_plan_data_for_support_view(billing_session: BillingSession) -> PlanData:
customer = billing_session.get_customer()
plan = None
@ -69,3 +131,17 @@ def get_current_plan_data_for_support_view(billing_session: BillingSession) -> P
plan_data.has_fixed_price = plan_data.current_plan.fixed_price is not None
return plan_data
def get_data_for_support_view(billing_session: BillingSession) -> SupportData:
plan_data = get_current_plan_data_for_support_view(billing_session)
customer = billing_session.get_customer()
if customer is not None:
sponsorship_data = get_customer_sponsorship_data(customer)
else:
sponsorship_data = SponsorshipData()
return SupportData(
plan_data=plan_data,
sponsorship_data=sponsorship_data,
)

View File

@ -11,25 +11,24 @@
{% if remote_realm.plan_type != SPONSORED_PLAN_TYPE %}
{% with %}
{% set customer = plan_data[remote_realm.id].customer %}
{% set sponsorship_data = support_data[remote_realm.id].sponsorship_data %}
{% set remote_id = remote_realm.id %}
{% set remote_type = "remote_realm_id" %}
{% set has_fixed_price = plan_data[remote_realm.id].has_fixed_price %}
{% set get_discount = get_discount %}
{% set has_fixed_price = support_data[remote_realm.id].plan_data.has_fixed_price %}
{% include 'analytics/sponsorship_forms_support.html' %}
{% endwith %}
{% endif %}
{% if plan_data[remote_realm.id].current_plan %}
{% if support_data[remote_realm.id].plan_data.current_plan %}
<div class="remote-realm-information">
{% with %}
{% set plan_data = plan_data[remote_realm.id] %}
{% set plan_data = support_data[remote_realm.id].plan_data %}
{% include 'analytics/current_plan_details.html' %}
{% endwith %}
</div>
{% with %}
{% set current_plan = plan_data[remote_realm.id].current_plan %}
{% set current_plan = support_data[remote_realm.id].plan_data.current_plan %}
{% set remote_id = remote_realm.id %}
{% set remote_type = "remote_realm_id" %}
{% include 'analytics/current_plan_forms_support.html' %}

View File

@ -54,25 +54,24 @@
{% if remote_server.plan_type != SPONSORED_PLAN_TYPE %}
{% with %}
{% set customer = remote_servers_plan_data[remote_server.id].customer %}
{% set sponsorship_data = remote_servers_support_data[remote_server.id].sponsorship_data %}
{% set remote_id = remote_server.id %}
{% set remote_type = "remote_server_id" %}
{% set has_fixed_price = remote_servers_plan_data[remote_server.id].has_fixed_price %}
{% set get_discount = get_discount %}
{% set has_fixed_price = remote_servers_support_data[remote_server.id].plan_data.has_fixed_price %}
{% include 'analytics/sponsorship_forms_support.html' %}
{% endwith %}
{% endif %}
{% if remote_servers_plan_data[remote_server.id].current_plan %}
{% if remote_servers_support_data[remote_server.id].plan_data.current_plan %}
<div class="remote-server-information">
{% with %}
{% set plan_data = remote_servers_plan_data[remote_server.id] %}
{% set plan_data = remote_servers_support_data[remote_server.id].plan_data %}
{% include 'analytics/current_plan_details.html' %}
{% endwith %}
</div>
{% with %}
{% set current_plan = remote_servers_plan_data[remote_server.id].current_plan %}
{% set current_plan = remote_servers_support_data[remote_server.id].plan_data.current_plan %}
{% set remote_id = remote_server.id %}
{% set remote_type = "remote_server_id" %}
{% include 'analytics/current_plan_forms_support.html' %}
@ -83,7 +82,7 @@
<hr />
<div>
{% with %}
{% set plan_data = remote_realms_plan_data %}
{% set support_data = remote_realms_support_data %}
{% set get_discount = get_discount %}
{% include "analytics/remote_realm_details.html" %}
{% endwith %}

View File

@ -3,13 +3,13 @@
{{ csrf_input }}
<input type="hidden" name="{{ remote_type }}" value="{{ remote_id }}" />
<select name="sponsorship_pending">
<option value="true" {% if customer and customer.sponsorship_pending %}selected{% endif %}>Yes</option>
<option value="false" {% if not customer or not customer.sponsorship_pending %}selected{% endif %}>No</option>
<option value="true" {% if sponsorship_data.sponsorship_pending %}selected{% endif %}>Yes</option>
<option value="false" {% if not sponsorship_data.sponsorship_pending %}selected{% endif %}>No</option>
</select>
<button type="submit" class="btn btn-default support-submit-button">Update</button>
</form>
{% if customer and customer.sponsorship_pending %}
{% if sponsorship_data.sponsorship_pending %}
<form method="POST" class="">
{{ csrf_input }}
<input type="hidden" name="{{ remote_type }}" value="{{ remote_id }}" />
@ -25,10 +25,10 @@
{{ csrf_input }}
<input type="hidden" name="{{ remote_type }}" value="{{ remote_id }}" />
{% if has_fixed_price %}
<input type="number" name="discount" value="{{ get_discount(customer) }}" disabled />
<input type="number" name="discount" value="{{ sponsorship_data.default_discount }}" disabled />
<button type="submit" class="btn btn-default support-submit-button" disabled>Update</button>
{% else %}
<input type="number" name="discount" value="{{ get_discount(customer) }}" required />
<input type="number" name="discount" value="{{ sponsorship_data.default_discount }}" required />
<button type="submit" class="btn btn-default support-submit-button">Update</button>
{% endif %}
</form>