示例#1
0
def _record_purchase(sailthru_client, email, item, purchase_incomplete,
                     message_id, options):
    """Record a purchase in Sailthru

    Arguments:
        sailthru_client (object): SailthruClient
        email (str): user's email address
        item (dict): Sailthru required information about the course
        purchase_incomplete (boolean): True if adding item to shopping cart
        message_id (str): Cookie used to identify marketing campaign
        options (dict): Sailthru purchase API options (e.g. template name)

    Returns:
        False if retryable error, else True
    """
    try:
        sailthru_response = sailthru_client.purchase(
            email, [item],
            incomplete=purchase_incomplete,
            message_id=message_id,
            options=options)

        if not sailthru_response.is_ok():
            error = sailthru_response.get_error()
            logger.error("Error attempting to record purchase in Sailthru: %s",
                         error.get_message())
            return not can_retry_sailthru_request(error)

    except SailthruClientError as exc:
        logger.exception(
            "Exception attempting to record purchase for %s in Sailthru - %s",
            email, text_type(exc))
        return False

    return True
示例#2
0
 def _is_eligible_for_retry(self, response):
     """
     Return a bool whether this task is eligible for retry or not.
     Also log the appropriate message according to occurred error.
     """
     is_eligible_for_retry = False
     error = response.get_error()
     log.error(
         '[{logger_prefix}] A {token_error_code} - {token_error_message} error occurred while attempting to send a '
         'notification. Message: {message}'.format(
             logger_prefix=self.logger_prefix,
             message=self.email_vars.get('email_body'),
             token_error_code=error.get_error_code(),
             token_error_message=error.get_message()
         )
     )
     if can_retry_sailthru_request(error):
         log.info(
             '[{logger_prefix}] An attempt will be made to resend the notification.'
             ' Message: {message}'.format(
                 logger_prefix=self.logger_prefix,
                 message=self.email_vars.get('email_body')
             )
         )
         is_eligible_for_retry = True
     else:
         log.warning(
             '[{logger_prefix}] No further attempts will be made to send the notification.'
             ' Failed Message: {message}'.format(
                 logger_prefix=self.logger_prefix,
                 message=self.email_vars.get('email_body')
             )
         )
     return is_eligible_for_retry
示例#3
0
def _send_offer_assignment_notification_email(config, user_email, subject,
                                              email_body, site_code, task):
    """Handles sending offer assignment notification emails and retrying failed emails when appropriate."""
    try:
        sailthru_client = get_sailthru_client(site_code)
    except SailthruError:
        logger.exception(
            '[Offer Assignment] A client error occurred while attempting to send a offer assignment notification.'
            ' Message: {message}'.format(message=email_body))
        return None
    email_vars = {
        'subject': subject,
        'email_body': email_body,
    }
    try:
        response = sailthru_client.send(
            template=config['templates']['assignment_email'],
            email=user_email,
            _vars=email_vars)
    except SailthruClientError:
        logger.exception(
            '[Offer Assignment] A client error occurred while attempting to send a offer assignment notification.'
            ' Message: {message}'.format(message=email_body))
        return None

    if not response.is_ok():
        error = response.get_error()
        logger.error(
            '[Offer Assignment] A {token_error_code} - {token_error_message} error occurred'
            ' while attempting to send a offer assignment notification.'
            ' Message: {message}'.format(
                message=email_body,
                token_error_code=error.get_error_code(),
                token_error_message=error.get_message()))
        if can_retry_sailthru_request(error):
            logger.info(
                '[Offer Assignment] An attempt will be made to resend the offer assignment notification.'
                ' Message: {message}'.format(message=email_body))
            schedule_retry(task, config)
        else:
            logger.warning(
                '[Offer Assignment] No further attempts will be made to send the offer assignment notification.'
                ' Failed Message: {message}'.format(message=email_body))

    return response
示例#4
0
def send_course_refund_email(self,
                             email,
                             refund_id,
                             amount,
                             course_name,
                             order_number,
                             order_url,
                             site_code=None):
    """ Sends the course refund email.

    Args:
        self: Ignore.
        email (str): Recipient's email address.
        refund_id (int): ID of the refund that initiated this task.
        amount (str): Formatted amount of the refund.
        course_name (str): Name of the course for which payment was refunded.
        order_number (str): Order number of the order that was refunded.
        order_url (str): Receipt URL of the refunded order.
        site_code (str): Identifier of the site sending the email.
    """
    config = get_sailthru_configuration(site_code)

    try:
        sailthru_client = get_sailthru_client(site_code)
    except SailthruError:
        # NOTE: We rely on the function to log the error for us
        return

    email_vars = {
        'amount': amount,
        'course_name': course_name,
        'order_number': order_number,
        'order_url': order_url,
    }

    try:
        response = sailthru_client.send(
            template=config['templates']['course_refund'],
            email=email,
            _vars=email_vars)
    except SailthruClientError:
        logger.exception(
            'A client error occurred while attempting to send a course refund notification for refund [%d].',
            refund_id)
        return

    if response.is_ok():
        logger.info('Course refund notification sent for refund %d.',
                    refund_id)
    else:
        error = response.get_error()
        logger.error(
            'An error occurred while attempting to send a course refund notification for refund [%d]: %d - %s',
            refund_id, error.get_error_code(), error.get_message())

        if can_retry_sailthru_request(error):
            logger.info(
                'An attempt will be made again to send a course refund notification for refund [%d].',
                refund_id)
            schedule_retry(self, config)
        else:
            logger.warning(
                'No further attempts will be made to send a course refund notification for refund [%d].',
                refund_id)
示例#5
0
def _update_unenrolled_list(sailthru_client, email, course_url, unenroll):
    """Maintain a list of courses the user has unenrolled from in the Sailthru user record

    Arguments:
        sailthru_client (object): SailthruClient
        email (str): user's email address
        course_url (str): LMS url for course info page.
        unenroll (boolean): True if unenrolling, False if enrolling

    Returns:
        False if retryable error, else True
    """
    try:
        # get the user 'vars' values from sailthru
        sailthru_response = sailthru_client.api_get("user", {
            "id": email,
            "fields": {
                "vars": 1
            }
        })
        if not sailthru_response.is_ok():
            error = sailthru_response.get_error()
            logger.error(
                "Error attempting to read user record from Sailthru: %s",
                error.get_message())
            return not can_retry_sailthru_request(error)

        response_json = sailthru_response.json

        unenroll_list = []
        if response_json and "vars" in response_json and response_json["vars"] \
           and "unenrolled" in response_json["vars"]:
            unenroll_list = response_json["vars"]["unenrolled"]

        changed = False
        # if unenrolling, add course to unenroll list
        if unenroll:
            if course_url not in unenroll_list:
                unenroll_list.append(course_url)
                changed = True

        # if enrolling, remove course from unenroll list
        elif course_url in unenroll_list:
            unenroll_list.remove(course_url)
            changed = True

        if changed:
            # write user record back
            sailthru_response = sailthru_client.api_post(
                'user', {
                    'id': email,
                    'key': 'email',
                    'vars': {
                        'unenrolled': unenroll_list
                    }
                })

            if not sailthru_response.is_ok():
                error = sailthru_response.get_error()
                logger.error(
                    "Error attempting to update user record in Sailthru: %s",
                    error.get_message())
                return not can_retry_sailthru_request(error)

        return True

    except SailthruClientError as exc:
        logger.exception(
            "Exception attempting to update user record for %s in Sailthru - %s",
            email, text_type(exc))
        return False