33"""
44import logging
55from collections .abc import Mapping
6- from functools import cache
76from typing import TypedDict , Unpack , cast
87
98import stripe
@@ -56,8 +55,6 @@ class FieldValidationResult(TypedDict):
5655 developer_message : str | None
5756
5857
59- # Basic in-memory cache to prevent multiple API calls within a single request.
60- @cache
6158def _get_lms_user_id (email : str | None ) -> int | None :
6259 """
6360 Return the LMS user ID for an existing user with a specific email, or None if no user with that email exists.
@@ -80,6 +77,11 @@ class CheckoutSessionInputValidator():
8077 https://github.com/openedx/edx-platform/blob/f90e59e5/openedx/core/djangoapps/user_authn/views/register.py#L727
8178 """
8279
80+ def get_lms_user_id (self , email ):
81+ if not hasattr (self , '_cached_lms_user_id' ):
82+ self ._cached_lms_user_id = _get_lms_user_id (email ) # pylint: disable=attribute-defined-outside-init
83+ return self ._cached_lms_user_id
84+
8385 def handle_admin_email (self , input_data : CheckoutSessionInputValidatorData ) -> FieldValidationResult :
8486 """
8587 Ensure the provided email is registered.
@@ -103,7 +105,7 @@ def handle_admin_email(self, input_data: CheckoutSessionInputValidatorData) -> F
103105 return {'error_code' : error_code , 'developer_message' : developer_message }
104106
105107 # Given email must be registered.
106- lms_user_id = _get_lms_user_id (email = admin_email )
108+ lms_user_id = self . get_lms_user_id (email = admin_email )
107109 if not lms_user_id :
108110 error_code , developer_message = CHECKOUT_SESSION_ERROR_CODES ['admin_email' ]['NOT_REGISTERED' ]
109111 logger .info (f'admin_email invalid: "{ admin_email } ". { developer_message } ' )
@@ -276,7 +278,7 @@ def handle_user(self, input_data: CheckoutSessionInputValidatorData) -> FieldVal
276278 **and** the lms_user_id is known and present in the User table.
277279 """
278280 if not (user_record := input_data .get ('user' )):
279- lms_user_id = _get_lms_user_id (input_data .get ('admin_email' ))
281+ lms_user_id = self . get_lms_user_id (input_data .get ('admin_email' ))
280282 user_record = User .objects .filter (lms_user_id = lms_user_id ).first ()
281283 if not user_record :
282284 error_code , developer_message = CHECKOUT_SESSION_ERROR_CODES ['user' ]['IS_NULL' ]
0 commit comments