validators: Use cleaner syntax for AfterValidator.

Created a function that returns an `AfterValidator` for `check_int_in`
and `check_string_in` instead of having to use a
`lambda` wraper everytime.
This commit is contained in:
Kenneth Rodrigues
2024-07-05 00:08:59 +05:30
committed by Tim Abbott
parent 0ec4b0285e
commit a7da24a36f
7 changed files with 64 additions and 44 deletions

View File

@@ -4,7 +4,7 @@ from typing import Annotated
from django.conf import settings
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from pydantic import AfterValidator, Json
from pydantic import Json
from corporate.lib.decorator import (
authenticated_remote_realm_management_endpoint,
@@ -25,7 +25,7 @@ from corporate.models import CustomerPlan
from zerver.decorator import require_organization_member, zulip_login_required
from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import typed_endpoint
from zerver.lib.typed_endpoint_validators import check_string_in
from zerver.lib.typed_endpoint_validators import check_string_in_validator
from zerver.models import UserProfile
from zilencer.lib.remote_counts import MissingDataError
@@ -38,17 +38,11 @@ def upgrade(
request: HttpRequest,
user: UserProfile,
*,
billing_modality: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_BILLING_MODALITY_VALUES))
],
schedule: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_BILLING_SCHEDULE_VALUES))
],
billing_modality: Annotated[str, check_string_in_validator(VALID_BILLING_MODALITY_VALUES)],
schedule: Annotated[str, check_string_in_validator(VALID_BILLING_SCHEDULE_VALUES)],
signed_seat_count: str,
salt: str,
license_management: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_LICENSE_MANAGEMENT_VALUES))
]
license_management: Annotated[str, check_string_in_validator(VALID_LICENSE_MANAGEMENT_VALUES)]
| None = None,
licenses: Json[int] | None = None,
tier: Json[int] = CustomerPlan.TIER_CLOUD_STANDARD,
@@ -94,17 +88,11 @@ def remote_realm_upgrade(
request: HttpRequest,
billing_session: RemoteRealmBillingSession,
*,
billing_modality: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_BILLING_MODALITY_VALUES))
],
schedule: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_BILLING_SCHEDULE_VALUES))
],
billing_modality: Annotated[str, check_string_in_validator(VALID_BILLING_MODALITY_VALUES)],
schedule: Annotated[str, check_string_in_validator(VALID_BILLING_SCHEDULE_VALUES)],
signed_seat_count: str,
salt: str,
license_management: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_LICENSE_MANAGEMENT_VALUES))
]
license_management: Annotated[str, check_string_in_validator(VALID_LICENSE_MANAGEMENT_VALUES)]
| None = None,
licenses: Json[int] | None = None,
remote_server_plan_start_date: str | None = None,
@@ -149,17 +137,11 @@ def remote_server_upgrade(
request: HttpRequest,
billing_session: RemoteServerBillingSession,
*,
billing_modality: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_BILLING_MODALITY_VALUES))
],
schedule: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_BILLING_SCHEDULE_VALUES))
],
billing_modality: Annotated[str, check_string_in_validator(VALID_BILLING_MODALITY_VALUES)],
schedule: Annotated[str, check_string_in_validator(VALID_BILLING_SCHEDULE_VALUES)],
signed_seat_count: str,
salt: str,
license_management: Annotated[
str, AfterValidator(lambda val: check_string_in(val, VALID_LICENSE_MANAGEMENT_VALUES))
]
license_management: Annotated[str, check_string_in_validator(VALID_LICENSE_MANAGEMENT_VALUES)]
| None = None,
licenses: Json[int] | None = None,
remote_server_plan_start_date: str | None = None,