Commit 8309930b by Simon Chen

Add the email marketing welcome email delay ECOM-6760

parent 7be0d675
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('email_marketing', '0003_auto_20160715_1145'),
]
operations = [
migrations.AddField(
model_name='emailmarketingconfiguration',
name='welcome_email_send_delay',
field=models.IntegerField(default=600, help_text='Number of seconds to delay the sending of User Welcome email after user has been activated'),
),
]
...@@ -127,6 +127,16 @@ class EmailMarketingConfiguration(ConfigurationModel): ...@@ -127,6 +127,16 @@ class EmailMarketingConfiguration(ConfigurationModel):
) )
) )
# The number of seconds to delay for welcome emails sending. This is needed to acommendate those
# learners who created user account during course enrollment so we can send a different message
# in our welcome email.
welcome_email_send_delay = models.fields.IntegerField(
default=600,
help_text=_(
"Number of seconds to delay the sending of User Welcome email after user has been activated"
)
)
def __unicode__(self): def __unicode__(self):
return u"Email marketing configuration: New user list %s, Activation template: %s" % \ return u"Email marketing configuration: New user list %s, Activation template: %s" % \
(self.sailthru_new_user_list, self.sailthru_activation_template) (self.sailthru_new_user_list, self.sailthru_activation_template)
...@@ -3,6 +3,7 @@ This file contains celery tasks for email marketing signal handler. ...@@ -3,6 +3,7 @@ This file contains celery tasks for email marketing signal handler.
""" """
import logging import logging
import time import time
from datetime import datetime, timedelta
from celery import task from celery import task
from django.core.cache import cache from django.core.cache import cache
...@@ -56,10 +57,16 @@ def update_user(self, sailthru_vars, email, site=None, new_user=False, activatio ...@@ -56,10 +57,16 @@ def update_user(self, sailthru_vars, email, site=None, new_user=False, activatio
# if activating user, send welcome email # if activating user, send welcome email
if activation and email_config.sailthru_activation_template: if activation and email_config.sailthru_activation_template:
scheduled_datetime = datetime.utcnow() + timedelta(seconds=email_config.welcome_email_send_delay)
try: try:
sailthru_response = sailthru_client.api_post("send", sailthru_response = sailthru_client.api_post(
{"email": email, "send",
"template": email_config.sailthru_activation_template}) {
"email": email,
"template": email_config.sailthru_activation_template,
"schedule_time": scheduled_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')
}
)
except SailthruClientError as exc: except SailthruClientError as exc:
log.error("Exception attempting to send welcome email to user %s in Sailthru - %s", email, unicode(exc)) log.error("Exception attempting to send welcome email to user %s in Sailthru - %s", email, unicode(exc))
raise self.retry(exc=exc, raise self.retry(exc=exc,
......
"""Tests of email marketing signal handlers.""" """Tests of email marketing signal handlers."""
import ddt import ddt
import logging import logging
import datetime
from django.test import TestCase from django.test import TestCase
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser
...@@ -45,6 +46,7 @@ def update_email_marketing_config(enabled=True, key='badkey', secret='badsecret' ...@@ -45,6 +46,7 @@ def update_email_marketing_config(enabled=True, key='badkey', secret='badsecret'
sailthru_get_tags_from_sailthru=False, sailthru_get_tags_from_sailthru=False,
sailthru_enroll_cost=enroll_cost, sailthru_enroll_cost=enroll_cost,
sailthru_max_retries=0, sailthru_max_retries=0,
welcome_email_send_delay=600
) )
...@@ -168,12 +170,14 @@ class EmailMarketingTests(TestCase): ...@@ -168,12 +170,14 @@ class EmailMarketingTests(TestCase):
""" """
mock_sailthru_post.return_value = SailthruResponse(JsonResponse({'ok': True})) mock_sailthru_post.return_value = SailthruResponse(JsonResponse({'ok': True}))
mock_sailthru_get.return_value = SailthruResponse(JsonResponse({'lists': [{'name': 'new list'}], 'ok': True})) mock_sailthru_get.return_value = SailthruResponse(JsonResponse({'lists': [{'name': 'new list'}], 'ok': True}))
expected_schedule = datetime.datetime.utcnow() + datetime.timedelta(seconds=600)
update_user.delay({}, self.user.email, new_user=True, activation=True) update_user.delay({}, self.user.email, new_user=True, activation=True)
# look for call args for 2nd call # look for call args for 2nd call
self.assertEquals(mock_sailthru_post.call_args[0][0], "send") self.assertEquals(mock_sailthru_post.call_args[0][0], "send")
userparms = mock_sailthru_post.call_args[0][1] userparms = mock_sailthru_post.call_args[0][1]
self.assertEquals(userparms['email'], TEST_EMAIL) self.assertEquals(userparms['email'], TEST_EMAIL)
self.assertEquals(userparms['template'], "Activation") self.assertEquals(userparms['template'], "Activation")
self.assertEquals(userparms['schedule_time'], expected_schedule.strftime('%Y-%m-%dT%H:%M:%SZ'))
@patch('email_marketing.tasks.log.error') @patch('email_marketing.tasks.log.error')
@patch('email_marketing.tasks.SailthruClient.api_post') @patch('email_marketing.tasks.SailthruClient.api_post')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment