def get_valid_student_email(identifier): """ Helper function to get an user email from an identifier and validate it. In the UI a Coach can enroll users using both an email and an username. This function takes care of: - in case the identifier is an username, extracting the user object from the DB and then the associated email - validating the email Arguments: identifier (str): Username or email of the user to enroll Returns: str: A validated email for the user to enroll Raises: CCXUserValidationException: if the username is not found or the email is not valid. """ user = email = None try: user = get_student_from_identifier(identifier) except User.DoesNotExist: email = identifier else: email = user.email try: validate_email(email) except ValidationError: raise CCXUserValidationException( 'Could not find a user with name or email "{0}" '.format( identifier)) return email
def get_valid_student_with_email(identifier): """ Helper function to get an user email from an identifier and validate it. In the UI a Coach can enroll users using both an email and an username. This function takes care of: - in case the identifier is an username, extracting the user object from the DB and then the associated email - validating the email Arguments: identifier (str): Username or email of the user to enroll Returns: (tuple): tuple containing: email (str): A validated email for the user to enroll. user (User): A valid User object or None. Raises: CCXUserValidationException: if the username is not found or the email is not valid. """ user = email = None try: user = get_student_from_identifier(identifier) except User.DoesNotExist: email = identifier else: email = user.email try: validate_email(email) except ValidationError: raise CCXUserValidationException( f'Could not find a user with name or email "{identifier}" ') # lint-amnesty, pylint: disable=raise-missing-from return email, user