Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion common/djangoapps/student/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
from util.model_utils import emit_field_changed_events, get_changed_fields_dict
from util.query import use_read_replica_if_available

# Mailchimp user subscription
import os
import sys
from mailsnake import MailSnake

UNENROLL_DONE = Signal(providing_args=["course_enrollment", "skip_refund"])
ENROLL_STATUS_CHANGE = Signal(providing_args=["event", "user", "course_id", "mode", "cost", "currency"])
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -508,7 +513,6 @@ class UserTestGroup(models.Model):
name = models.CharField(blank=False, max_length=32, db_index=True)
description = models.TextField(blank=True)


class Registration(models.Model):
''' Allows us to wait for e-mail before user is registered. A
registration profile is created when the user creates an
Expand All @@ -532,6 +536,8 @@ def activate(self):
self._track_activation()
self.user.save()
log.info(u'User %s (%s) account is successfully activated.', self.user.username, self.user.email)
# Adds user to $MAILCHIMP_LIST_ID email list using $MAILCHIMP_API_KEY
self._subscribe_to_mailchimp(self.user.email, self.user.username)

def _track_activation(self):
""" Update the isActive flag in mailchimp for activated users."""
Expand All @@ -553,6 +559,39 @@ def _track_activation(self):
]
analytics.identify(*identity_args)

def _subscribe_to_mailchimp(self, user_email, user_name):
"""Subscribes user to Cognitive Class mailchimp mailing list"""

try:
# Riases KeyError if environment variables
# $MAILCHIMP_API_KEY or $MAILCHIMP_LIST_ID are not set
MAILCHIMP_API_KEY = os.environ['MAILCHIMP_API_KEY']
MAILCHIMP_LIST_ID = os.environ['MAILCHIMP_LIST_ID']

ms = MailSnake(MAILCHIMP_API_KEY)
ms.listSubscribe(
id = MAILCHIMP_LIST_ID,
email_address = user_email,
merge_vars = {
'FNAME': user_name,
'LNAME': '',
},
update_existing = True,
double_optin = False,
)
except KeyError:
log.info(
"Warning: environment variables MAILCHIMP_API_KEY or " +
"MAILCHIMP_LIST_ID or both not set. User will not be " +
"subscribed to Cognitive Class mailchimp email list. "
)
except:
log.info(
"An error has occurred. " +
"This might be caused because the user has already " +
"been have subscribed to Cognitive Class email list. "
)


class PendingNameChange(models.Model):
user = models.OneToOneField(User, unique=True, db_index=True)
Expand Down