Unverified Commit c866b526 by Troy Sankey Committed by GitHub

Merge pull request #16426 from edx/pwnage101/mailing_mgmt_cleanup

Mailing management command cleanup for Django 1.11
parents a711fb81 e0697905
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
mailchimp_id: Returns whether or not a given mailchimp key represents mailchimp_id: Returns whether or not a given mailchimp key represents
a valid list. a valid list.
""" """
from __future__ import print_function
import sys import sys
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from mailsnake import MailSnake from mailsnake import MailSnake
...@@ -14,30 +15,24 @@ class Command(BaseCommand): ...@@ -14,30 +15,24 @@ class Command(BaseCommand):
Given a mailchimp key, validates that a list with that key Given a mailchimp key, validates that a list with that key
exists in mailchimp. exists in mailchimp.
""" """
args = '<mailchimp_key web_id>'
help = 'Get the list id from a web_id' help = 'Get the list id from a web_id'
option_list = BaseCommand.option_list + ( def add_arguments(self, parser):
make_option('--key', action='store', help='mailchimp api key'), parser.add_argument('--key',
make_option('--webid', action='store', dest='web_id', type=int, required=True,
help='mailchimp list web id'), help='mailchimp api key')
) parser.add_argument('--webid',
dest='web_id',
def parse_options(self, options): type=int,
"""Parses `options` of the command.""" required=True,
if not options['key']: help='mailchimp list web id')
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): def handle(self, *args, **options):
""" """
Validates that the id passed in exists in mailchimp. Validates that the id passed in exists in mailchimp.
""" """
key, web_id = self.parse_options(options) key = options['key']
web_id = options['web_id']
mailchimp = MailSnake(key) mailchimp = MailSnake(key)
...@@ -47,8 +42,8 @@ class Command(BaseCommand): ...@@ -47,8 +42,8 @@ class Command(BaseCommand):
list_with_id = by_web_id.get(web_id, None) list_with_id = by_web_id.get(web_id, None)
if list_with_id: if list_with_id:
print "id: {} for web_id: {}".format(list_with_id['id'], web_id) print("id: {} for web_id: {}".format(list_with_id['id'], web_id))
print "list name: {}".format(list_with_id['name']) print("list name: {}".format(list_with_id['name']))
else: else:
print "list with web_id: {} not found.".format(web_id) print("list with web_id: {} not found.".format(web_id))
sys.exit(1) sys.exit(1)
...@@ -2,10 +2,9 @@ ...@@ -2,10 +2,9 @@
Synchronizes the announcement list with all active students. Synchronizes the announcement list with all active students.
""" """
import logging import logging
from optparse import make_option
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from .mailchimp_sync_course import connect_mailchimp, get_cleaned, get_subscribed, get_unsubscribed, subscribe_with_data from .mailchimp_sync_course import connect_mailchimp, get_cleaned, get_subscribed, get_unsubscribed, subscribe_with_data
...@@ -16,27 +15,20 @@ class Command(BaseCommand): ...@@ -16,27 +15,20 @@ class Command(BaseCommand):
""" """
Synchronizes the announcement list with all active students. Synchronizes the announcement list with all active students.
""" """
args = '<mailchimp_key mailchimp_list course_id>'
help = 'Synchronizes the announcement list with all active students.' help = 'Synchronizes the announcement list with all active students.'
option_list = BaseCommand.option_list + ( def add_arguments(self, parser):
make_option('--key', action='store', help='mailchimp api key'), parser.add_argument('--key',
make_option('--list', action='store', dest='list_id', required=True,
help='mailchimp list id'), help='mailchimp api key')
) parser.add_argument('--list',
dest='list_id',
def parse_options(self, options): required=True,
"""Parses `options` of the command.""" help='mailchimp list id')
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): def handle(self, *args, **options):
key, list_id = self.parse_options(options) key = options['key']
list_id = options['list_id']
log.info('Syncronizing announcement mailing list') log.info('Syncronizing announcement mailing list')
......
...@@ -7,12 +7,10 @@ import math ...@@ -7,12 +7,10 @@ import math
import random import random
from collections import namedtuple from collections import namedtuple
from itertools import chain from itertools import chain
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from mailsnake import MailSnake from mailsnake import MailSnake
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from student.models import UserProfile, unique_id_for_user from student.models import UserProfile, unique_id_for_user
BATCH_SIZE = 15000 BATCH_SIZE = 15000
...@@ -29,38 +27,32 @@ class Command(BaseCommand): ...@@ -29,38 +27,32 @@ class Command(BaseCommand):
""" """
Synchronizes a mailchimp list with the students of a course. Synchronizes a mailchimp list with the students of a course.
""" """
args = '<mailchimp_key mailchimp_list course_id>'
help = 'Synchronizes a mailchimp list with the students of a course.' help = 'Synchronizes a mailchimp list with the students of a course.'
option_list = BaseCommand.option_list + ( def add_arguments(self, parser):
make_option('--key', action='store', help='mailchimp api key'), parser.add_argument('--key',
make_option('--list', action='store', dest='list_id', required=True,
help='mailchimp list id'), help='mailchimp api key')
make_option('--course', action='store', dest='course_id', parser.add_argument('--list',
help='xmodule course_id'), dest='list_id',
required=True,
make_option('--segments', action='store', dest='segments', help='mailchimp list id')
default=0, type=int, parser.add_argument('--course',
help='number of static random segments to create'), dest='course_id',
) required=True,
help='edx course_id')
def parse_options(self, options): parser.add_argument('--segments',
"""Parses `options` of the command.""" dest='num_segments',
if not options['key']: type=int,
raise CommandError('missing key') default=0,
help='number of static random segments to create')
if not options['list_id']:
raise CommandError('missing list id')
if not options['course_id']:
raise CommandError('missing course id')
return (options['key'], options['list_id'],
options['course_id'], options['segments'])
def handle(self, *args, **options): def handle(self, *args, **options):
"""Synchronizes a mailchimp list with the students of a course.""" """Synchronizes a mailchimp list with the students of a course."""
key, list_id, course_id, nsegments = self.parse_options(options) key = options['key']
list_id = options['list_id']
course_id = options['course_id']
num_segments = options['num_segments']
log.info('Syncronizing email list for %s', course_id) log.info('Syncronizing email list for %s', course_id)
...@@ -87,7 +79,7 @@ class Command(BaseCommand): ...@@ -87,7 +79,7 @@ class Command(BaseCommand):
unsubscribe(mailchimp, list_id, non_enrolled_emails) unsubscribe(mailchimp, list_id, non_enrolled_emails)
subscribed = subscribed.union(set(d['EMAIL'] for d in to_subscribe)) subscribed = subscribed.union(set(d['EMAIL'] for d in to_subscribe))
make_segments(mailchimp, list_id, nsegments, subscribed) make_segments(mailchimp, list_id, num_segments, subscribed)
def connect_mailchimp(api_key): def connect_mailchimp(api_key):
......
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