Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
remote_billing_page: Extract method to get RemoteBillingIdentityDict.
Aman Agrawalace14e5a7d1d
diff --git a/corporate/lib/remote_billing_util.py b/corporate/lib/remote_billing_util.pynew file mode 100644index 00000000000..7fb9f261006--- /dev/null+++ b/corporate/lib/remote_billing_util.py@@ -0,0 +1,30 @@+import logging+from typing import Optional, TypedDict++from django.http import HttpRequest+from django.utils.translation import gettext as _++billing_logger = logging.getLogger("corporate.stripe")+++class RemoteBillingIdentityDict(TypedDict):+ user_uuid: str+ user_email: str+ user_full_name: str+ remote_server_uuid: str+ remote_realm_uuid: str+++def get_identity_dict_from_session(+ request: HttpRequest,+ realm_uuid: Optional[str],+ server_uuid: Optional[str],+) -> Optional[RemoteBillingIdentityDict]:+ authed_uuid = realm_uuid or server_uuid+ assert authed_uuid is not None++ identity_dicts = request.session.get("remote_billing_identities")+ if identity_dicts is not None:+ return identity_dicts.get(authed_uuid)++ return Nonediff --git a/corporate/views/remote_billing_page.py b/corporate/views/remote_billing_page.pyindex 2e4212d8fc3..b0a5de694ed 100644--- a/corporate/views/remote_billing_page.py+++ b/corporate/views/remote_billing_page.py@@ -1,5 +1,5 @@import logging-from typing import Optional, TypedDict+from typing import Optionalfrom django.conf import settingsfrom django.core import signing@@ -10,6 +10,10 @@ from django.utils.translation import gettext as _from django.views.decorators.csrf import csrf_exemptfrom pydantic import Json+from corporate.lib.remote_billing_util import (+ RemoteBillingIdentityDict,+ get_identity_dict_from_session,+)from zerver.decorator import self_hosting_management_endpointfrom zerver.lib.exceptions import JsonableError, MissingRemoteRealmErrorfrom zerver.lib.remote_server import RealmDataForAnalytics, UserDataForRemoteBilling@@ -20,14 +24,6 @@ from zilencer.models import RemoteRealm, RemoteZulipServerbilling_logger = logging.getLogger("corporate.stripe")-class RemoteBillingIdentityDict(TypedDict):- user_uuid: str- user_email: str- user_full_name: str- remote_server_uuid: str- remote_realm_uuid: str--@csrf_exempt@typed_endpointdef remote_server_billing_entry(@@ -93,14 +89,9 @@ def remote_server_billing_finalize_login(def render_tmp_remote_billing_page(request: HttpRequest, realm_uuid: Optional[str], server_uuid: Optional[str]) -> HttpResponse:- authed_uuid = realm_uuid or server_uuid- assert authed_uuid is not None-- identity_dict = None-- identity_dicts = request.session.get("remote_billing_identities")- if identity_dicts is not None:- identity_dict = identity_dicts.get(authed_uuid)+ identity_dict = get_identity_dict_from_session(+ request, realm_uuid=realm_uuid, server_uuid=server_uuid+ )if identity_dict is None:raise JsonableError(_("User not authenticated"))