Commit 6eda3ef7 by Sarina Canelake

Merge pull request #7672 from edx/feanil/new_mailchimp_sync

Update Mailchimp sync scripts to be within platform
parents 1bed97e9 b2d9d339
"""
mailchimp_id: Returns whether or not a given mailchimp key represents
a valid list.
"""
import sys
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from mailsnake import MailSnake
class Command(BaseCommand):
"""
Given a mailchimp key, validates that a list with that key
exists in mailchimp.
"""
args = '<mailchimp_key web_id>'
help = 'Get the list id from a web_id'
option_list = BaseCommand.option_list + (
make_option('--key', action='store', help='mailchimp api key'),
make_option('--webid', action='store', dest='web_id', type=int,
help='mailchimp list web id'),
)
def parse_options(self, options):
"""Parses `options` of the command."""
if not options['key']:
raise CommandError('missing key')
if not options['web_id']:
raise CommandError('missing list web id')
return options['key'], options['web_id']
def handle(self, *args, **options):
"""
Validates that the id passed in exists in mailchimp.
"""
key, web_id = self.parse_options(options)
mailchimp = MailSnake(key)
lists = mailchimp.lists()['data']
by_web_id = {l['web_id']: l for l in lists}
list_with_id = by_web_id.get(web_id, None)
if list_with_id:
print "id: {} for web_id: {}".format(list_with_id['id'], web_id)
print "list name: {}".format(list_with_id['name'])
else:
print "list with web_id: {} not found.".format(web_id)
sys.exit(1)
"""
Synchronizes the announcement list with all active students.
"""
import logging
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
from .mailchimp_sync_course import (
connect_mailchimp, get_cleaned,
get_subscribed, get_unsubscribed,
subscribe_with_data
)
log = logging.getLogger('edx.mailchimp')
class Command(BaseCommand):
"""
Synchronizes the announcement list with all active students.
"""
args = '<mailchimp_key mailchimp_list course_id>'
help = 'Synchronizes the announcement list with all active students.'
option_list = BaseCommand.option_list + (
make_option('--key', action='store', help='mailchimp api key'),
make_option('--list', action='store', dest='list_id',
help='mailchimp list id'),
)
def parse_options(self, options):
"""Parses `options` of the command."""
if not options['key']:
raise CommandError('missing key')
if not options['list_id']:
raise CommandError('missing list id')
return (options['key'], options['list_id'])
def handle(self, *args, **options):
key, list_id = self.parse_options(options)
log.info('Syncronizing announcement mailing list')
mailchimp = connect_mailchimp(key)
subscribed = get_subscribed(mailchimp, list_id)
unsubscribed = get_unsubscribed(mailchimp, list_id)
cleaned = get_cleaned(mailchimp, list_id)
non_subscribed = unsubscribed.union(cleaned)
enrolled = get_enrolled()
exclude = subscribed.union(non_subscribed)
to_subscribe = get_data(enrolled, exclude=exclude)
subscribe_with_data(mailchimp, list_id, to_subscribe)
def get_enrolled():
"""
Filter out all users who signed up via a Microsite, which UserSignupSource tracks
"""
## TODO (Feanil) This grabs all inactive students and MUST be changed (or, could exclude inactive users in get_data)
return User.objects.raw('SELECT * FROM auth_user where id not in (SELECT user_id from student_usersignupsource)')
def get_data(users, exclude=None):
"""
users: set of Django users
exclude [optional]: set of Django users to exclude
returns: {'EMAIL': u.email} for all users in users less those in `exclude`
"""
exclude = exclude if exclude else set()
emails = (u.email for u in users)
return ({'EMAIL': e} for e in emails if e not in exclude)
......@@ -1722,6 +1722,9 @@ INSTALLED_APPS = (
'openedx.core.djangoapps.content.course_structures',
'course_structure_api',
# Mailchimp Syncing
'mailing',
# CORS and cross-domain CSRF
'corsheaders',
'cors_csrf',
......
......@@ -149,3 +149,6 @@ testtools==0.9.34
# Used for Segment.io analytics
analytics-python==0.4.4
# Needed for mailchimp(mailing djangoapp)
mailsnake==1.6.2
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