From e8500fbdb0ae79fa64978095cbfdf66534770e5b Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Wed, 13 Dec 2023 18:23:05 +0100 Subject: [PATCH] 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. --- analytics/views/support.py | 19 +++-- corporate/lib/support.py | 82 ++++++++++++++++++- templates/analytics/remote_realm_details.html | 11 ++- .../analytics/remote_server_support.html | 13 ++- .../analytics/sponsorship_forms_support.html | 10 +-- 5 files changed, 105 insertions(+), 30 deletions(-) diff --git a/analytics/views/support.py b/analytics/views/support.py index b4920e198b..20f2bab4dc 100644 --- a/analytics/views/support.py +++ b/analytics/views/support.py @@ -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 diff --git a/corporate/lib/support.py b/corporate/lib/support.py index 02a9a6db08..eb4ffa5c80 100644 --- a/corporate/lib/support.py +++ b/corporate/lib/support.py @@ -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, + ) diff --git a/templates/analytics/remote_realm_details.html b/templates/analytics/remote_realm_details.html index b00222d9bc..5eb0b9e0b6 100644 --- a/templates/analytics/remote_realm_details.html +++ b/templates/analytics/remote_realm_details.html @@ -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 %}
{% 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 %}
{% 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' %} diff --git a/templates/analytics/remote_server_support.html b/templates/analytics/remote_server_support.html index 9bc2354863..74aac5705c 100644 --- a/templates/analytics/remote_server_support.html +++ b/templates/analytics/remote_server_support.html @@ -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 %}
{% 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 %}
{% 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 @@
{% 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 %} diff --git a/templates/analytics/sponsorship_forms_support.html b/templates/analytics/sponsorship_forms_support.html index e0cb18463b..4513f84d63 100644 --- a/templates/analytics/sponsorship_forms_support.html +++ b/templates/analytics/sponsorship_forms_support.html @@ -3,13 +3,13 @@ {{ csrf_input }} -{% if customer and customer.sponsorship_pending %} +{% if sponsorship_data.sponsorship_pending %}
{{ csrf_input }} @@ -25,10 +25,10 @@ {{ csrf_input }} {% if has_fixed_price %} - + {% else %} - + {% endif %}