Beispiel #1
0
def send_auto_applied_license_email_task(enterprise_customer_uuid, user_email):
    """
    Asynchronously sends onboarding email to learner. Intended for use following automatic license activation.

    Uses Braze client to send email via Braze campaign.
    """
    try:
        # Get some info about the enterprise customer
        enterprise_api_client = EnterpriseApiClient()
        enterprise_customer = enterprise_api_client.get_enterprise_customer_data(
            enterprise_customer_uuid)
        enterprise_slug = enterprise_customer.get('slug')
        enterprise_name = enterprise_customer.get('name')
        learner_portal_search_enabled = enterprise_customer.get(
            'enable_integrated_customer_learner_portal_search')
        identity_provider = enterprise_customer.get('identity_provider')
        enterprise_sender_alias = get_enterprise_sender_alias(
            enterprise_customer)
        enterprise_contact_email = enterprise_customer.get('contact_email')
    except Exception:  # pylint: disable=broad-except
        message = (
            f'Error getting data about the enterprise_customer {enterprise_customer_uuid}. '
            f'Onboarding email to {user_email} for auto applied license failed.'
        )
        logger.error(message, exc_info=True)
        return

    # Determine which email campaign to use
    if identity_provider and learner_portal_search_enabled is False:
        braze_campaign_id = settings.AUTOAPPLY_NO_LEARNER_PORTAL_CAMPAIGN
    else:
        braze_campaign_id = settings.AUTOAPPLY_WITH_LEARNER_PORTAL_CAMPAIGN

    # Form data we want to hand to the campaign's email template
    braze_trigger_properties = {
        'enterprise_customer_slug': enterprise_slug,
        'enterprise_customer_name': enterprise_name,
        'enterprise_sender_alias': enterprise_sender_alias,
        'enterprise_contact_email': enterprise_contact_email,
    }
    recipient = _aliased_recipient_object_from_email(user_email)

    try:
        # Hit the Braze api to send the email
        braze_client_instance = BrazeApiClient()
        braze_client_instance.create_braze_alias(
            [user_email],
            ENTERPRISE_BRAZE_ALIAS_LABEL,
        )
        braze_client_instance.send_campaign_message(
            braze_campaign_id,
            recipients=[recipient],
            trigger_properties=braze_trigger_properties,
        )
    except BrazeClientError:
        message = (
            'Error hitting Braze API. '
            f'Onboarding email to {user_email} for auto applied license failed.'
        )
        logger.error(message, exc_info=True)
def send_onboarding_email(enterprise_customer_uuid, user_email):
    """
    Sends onboarding email to learner. Intended for use following license activation.

    Arguments:
        enterprise_customer_uuid (UUID): unique identifier of the EnterpriseCustomer
        that is linked to the SubscriptionPlan associated with the activated license
        user_email (str): email of the learner whose license has just been activated
    """
    enterprise_customer = EnterpriseApiClient().get_enterprise_customer_data(
        enterprise_customer_uuid)
    enterprise_name = enterprise_customer.get('name')
    enterprise_slug = enterprise_customer.get('slug')
    enterprise_sender_alias = get_enterprise_sender_alias(enterprise_customer)

    context = {
        'subject': ONBOARDING_EMAIL_SUBJECT,
        'template_name': ONBOARDING_EMAIL_TEMPLATE,
        'ENTERPRISE_NAME': enterprise_name,
        'ENTERPRISE_SLUG': enterprise_slug,
        'HELP_CENTER_URL': settings.SUPPORT_SITE_URL,
        'LEARNER_PORTAL_LINK': get_learner_portal_url(enterprise_slug),
        'MOBILE_STORE_URLS': settings.MOBILE_STORE_URLS,
        'RECIPIENT_EMAIL': user_email,
        'SOCIAL_MEDIA_FOOTER_URLS': settings.SOCIAL_MEDIA_FOOTER_URLS,
    }
    email = _message_from_context_and_template(context,
                                               enterprise_sender_alias)
    email.send()
def activation_email_task(custom_template_text, email_recipient_list, subscription_uuid):
    """
    Sends license activation email(s) asynchronously, and creates pending enterprise users to link the email recipients
    to the subscription's enterprise.

    Arguments:
        custom_template_text (dict): Dictionary containing `greeting` and `closing` keys to be used for customizing
            the email template.
        email_recipient_list (list of str): List of recipients to send the emails to.
        subscription_uuid (str): UUID (string representation) of the subscription that the recipients are associated
            with or will be associated with.
    """
    subscription_plan = SubscriptionPlan.objects.get(uuid=subscription_uuid)
    pending_licenses = subscription_plan.licenses.filter(user_email__in=email_recipient_list).order_by('uuid')
    enterprise_api_client = EnterpriseApiClient()
    enterprise_customer = enterprise_api_client.get_enterprise_customer_data(subscription_plan.enterprise_customer_uuid)
    enterprise_slug = enterprise_customer.get('slug')
    enterprise_name = enterprise_customer.get('name')
    enterprise_sender_alias = get_enterprise_sender_alias(enterprise_customer)

    try:
        send_activation_emails(
            custom_template_text, pending_licenses, enterprise_slug, enterprise_name, enterprise_sender_alias
        )
    except SMTPException:
        msg = 'License manager activation email sending received an exception for enterprise: {}.'.format(
            enterprise_name
        )
        logger.error(msg, exc_info=True)
        return
Beispiel #4
0
def send_reminder_email_task(custom_template_text, email_recipient_list,
                             subscription_uuid):
    """
    Sends license activation reminder email(s) asynchronously.

    Arguments:
        custom_template_text (dict): Dictionary containing `greeting` and `closing` keys to be used for customizing
            the email template.
        email_recipient_list (list of str): List of recipients to send the emails to.
        subscription_uuid (str): UUID (string representation) of the subscription that the recipients are associated
            with or will be associated with.
    """
    subscription_plan = SubscriptionPlan.objects.get(uuid=subscription_uuid)
    pending_licenses = subscription_plan.licenses.filter(
        user_email__in=email_recipient_list).order_by('uuid')
    enterprise_api_client = EnterpriseApiClient()
    enterprise_customer = enterprise_api_client.get_enterprise_customer_data(
        subscription_plan.enterprise_customer_uuid)
    enterprise_slug = enterprise_customer.get('slug')
    enterprise_name = enterprise_customer.get('name')
    enterprise_sender_alias = get_enterprise_sender_alias(enterprise_customer)
    enterprise_contact_email = enterprise_customer.get('contact_email')

    # We need to send these emails individually, because each email's text must be
    # generated for every single user/activation_key
    for pending_license in pending_licenses:
        user_email = pending_license.user_email
        license_activation_key = str(pending_license.activation_key)
        braze_campaign_id = settings.BRAZE_REMIND_EMAIL_CAMPAIGN
        braze_trigger_properties = {
            'TEMPLATE_GREETING': custom_template_text['greeting'],
            'TEMPLATE_CLOSING': custom_template_text['closing'],
            'license_activation_key': license_activation_key,
            'enterprise_customer_slug': enterprise_slug,
            'enterprise_customer_name': enterprise_name,
            'enterprise_sender_alias': enterprise_sender_alias,
            'enterprise_contact_email': enterprise_contact_email,
        }
        recipient = _aliased_recipient_object_from_email(user_email)

        try:
            braze_client_instance = BrazeApiClient()
            braze_client_instance.create_braze_alias(
                [user_email],
                ENTERPRISE_BRAZE_ALIAS_LABEL,
            )
            braze_client_instance.send_campaign_message(
                braze_campaign_id,
                recipients=[recipient],
                trigger_properties=braze_trigger_properties,
            )

        except BrazeClientError as exc:
            message = ('Error hitting Braze API. '
                       f'reminder email to {user_email} for license failed.')
            logger.exception(message)
            raise exc

    License.set_date_fields_to_now(pending_licenses, ['last_remind_date'])
def send_revocation_cap_notification_email_task(subscription_uuid):
    """
    Sends revocation cap email notification to ECS asynchronously.

    Arguments:
        subscription_uuid (str): UUID (string representation) of the subscription that has reached its recovation cap.
    """
    subscription_plan = SubscriptionPlan.objects.get(uuid=subscription_uuid)
    enterprise_api_client = EnterpriseApiClient()
    enterprise_customer = enterprise_api_client.get_enterprise_customer_data(subscription_plan.enterprise_customer_uuid)
    enterprise_name = enterprise_customer.get('name')
    enterprise_sender_alias = get_enterprise_sender_alias(enterprise_customer)

    try:
        send_revocation_cap_notification_email(
            subscription_plan,
            enterprise_name,
            enterprise_sender_alias,
        )
    except SMTPException:
        logger.error('Revocation cap notification email sending received an exception.', exc_info=True)
Beispiel #6
0
def send_post_activation_email_task(enterprise_customer_uuid, user_email):
    """
    Asynchronously sends post license activation email to learner.
    """
    enterprise_customer = EnterpriseApiClient().get_enterprise_customer_data(
        enterprise_customer_uuid)
    enterprise_name = enterprise_customer.get('name')
    enterprise_slug = enterprise_customer.get('slug')
    enterprise_sender_alias = get_enterprise_sender_alias(enterprise_customer)
    enterprise_contact_email = enterprise_customer.get('contact_email')

    braze_campaign_id = settings.BRAZE_ACTIVATION_EMAIL_CAMPAIGN
    braze_trigger_properties = {
        'enterprise_customer_slug': enterprise_slug,
        'enterprise_customer_name': enterprise_name,
        'enterprise_sender_alias': enterprise_sender_alias,
        'enterprise_contact_email': enterprise_contact_email,
    }
    recipient = _aliased_recipient_object_from_email(user_email)

    try:
        braze_client_instance = BrazeApiClient()
        braze_client_instance.create_braze_alias(
            [user_email],
            ENTERPRISE_BRAZE_ALIAS_LABEL,
        )
        braze_client_instance.send_campaign_message(
            braze_campaign_id,
            recipients=[recipient],
            trigger_properties=braze_trigger_properties,
        )
    except BrazeClientError as exc:
        message = ('Error hitting Braze API. '
                   f'Onboarding email to {user_email} for license failed.')
        logger.exception(message)
        raise exc