billing: Migrate to @typed_endpoint.

migrate update_plan(), update_plan_for_remote_realm(),
and update_plan_for_remote_server() to @typed_endpoint.
This commit is contained in:
bedo 2024-07-05 19:00:27 +03:00 committed by Tim Abbott
parent 1410a0acb3
commit db5c272f82
1 changed files with 33 additions and 35 deletions

View File

@ -5,6 +5,8 @@ from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed, HttpR
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse from django.urls import reverse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from pydantic import AfterValidator, Json
from typing_extensions import Annotated
from corporate.lib.decorator import ( from corporate.lib.decorator import (
authenticated_remote_realm_management_endpoint, authenticated_remote_realm_management_endpoint,
@ -21,10 +23,9 @@ from corporate.lib.stripe import (
from corporate.models import CustomerPlan, get_current_plan_by_customer, get_customer_by_realm from corporate.models import CustomerPlan, get_current_plan_by_customer, get_customer_by_realm
from zerver.decorator import process_as_post, require_billing_access, zulip_login_required from zerver.decorator import process_as_post, require_billing_access, zulip_login_required
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import typed_endpoint from zerver.lib.typed_endpoint import typed_endpoint
from zerver.lib.validator import check_int, check_int_in from zerver.lib.typed_endpoint_validators import check_int_in
from zerver.models import UserProfile from zerver.models import UserProfile
from zilencer.lib.remote_counts import MissingDataError from zilencer.lib.remote_counts import MissingDataError
from zilencer.models import RemoteRealm, RemoteZulipServer from zilencer.models import RemoteRealm, RemoteZulipServer
@ -233,20 +234,19 @@ def remote_server_billing_page(
@require_billing_access @require_billing_access
@has_request_variables @typed_endpoint
def update_plan( def update_plan(
request: HttpRequest, request: HttpRequest,
user: UserProfile, user: UserProfile,
status: Optional[int] = REQ( *,
"status", status: Optional[
json_validator=check_int_in(ALLOWED_PLANS_API_STATUS_VALUES), Annotated[
default=None, Json[int], AfterValidator(lambda x: check_int_in(x, ALLOWED_PLANS_API_STATUS_VALUES))
), ]
licenses: Optional[int] = REQ("licenses", json_validator=check_int, default=None), ] = None,
licenses_at_next_renewal: Optional[int] = REQ( licenses: Optional[Json[int]] = None,
"licenses_at_next_renewal", json_validator=check_int, default=None licenses_at_next_renewal: Optional[Json[int]] = None,
), schedule: Optional[Json[int]] = None,
schedule: Optional[int] = REQ("schedule", json_validator=check_int, default=None),
) -> HttpResponse: ) -> HttpResponse:
update_plan_request = UpdatePlanRequest( update_plan_request = UpdatePlanRequest(
status=status, status=status,
@ -261,20 +261,19 @@ def update_plan(
@authenticated_remote_realm_management_endpoint @authenticated_remote_realm_management_endpoint
@process_as_post @process_as_post
@has_request_variables @typed_endpoint
def update_plan_for_remote_realm( def update_plan_for_remote_realm(
request: HttpRequest, request: HttpRequest,
billing_session: RemoteRealmBillingSession, billing_session: RemoteRealmBillingSession,
status: Optional[int] = REQ( *,
"status", status: Optional[
json_validator=check_int_in(ALLOWED_PLANS_API_STATUS_VALUES), Annotated[
default=None, Json[int], AfterValidator(lambda x: check_int_in(x, ALLOWED_PLANS_API_STATUS_VALUES))
), ]
licenses: Optional[int] = REQ("licenses", json_validator=check_int, default=None), ] = None,
licenses_at_next_renewal: Optional[int] = REQ( licenses: Optional[Json[int]] = None,
"licenses_at_next_renewal", json_validator=check_int, default=None licenses_at_next_renewal: Optional[Json[int]] = None,
), schedule: Optional[Json[int]] = None,
schedule: Optional[int] = REQ("schedule", json_validator=check_int, default=None),
) -> HttpResponse: ) -> HttpResponse:
update_plan_request = UpdatePlanRequest( update_plan_request = UpdatePlanRequest(
status=status, status=status,
@ -288,20 +287,19 @@ def update_plan_for_remote_realm(
@authenticated_remote_server_management_endpoint @authenticated_remote_server_management_endpoint
@process_as_post @process_as_post
@has_request_variables @typed_endpoint
def update_plan_for_remote_server( def update_plan_for_remote_server(
request: HttpRequest, request: HttpRequest,
billing_session: RemoteServerBillingSession, billing_session: RemoteServerBillingSession,
status: Optional[int] = REQ( *,
"status", status: Optional[
json_validator=check_int_in(ALLOWED_PLANS_API_STATUS_VALUES), Annotated[
default=None, Json[int], AfterValidator(lambda x: check_int_in(x, ALLOWED_PLANS_API_STATUS_VALUES))
), ]
licenses: Optional[int] = REQ("licenses", json_validator=check_int, default=None), ] = None,
licenses_at_next_renewal: Optional[int] = REQ( licenses: Optional[Json[int]] = None,
"licenses_at_next_renewal", json_validator=check_int, default=None licenses_at_next_renewal: Optional[Json[int]] = None,
), schedule: Optional[Json[int]] = None,
schedule: Optional[int] = REQ("schedule", json_validator=check_int, default=None),
) -> HttpResponse: ) -> HttpResponse:
update_plan_request = UpdatePlanRequest( update_plan_request = UpdatePlanRequest(
status=status, status=status,