Mailing management command cleanup for Django 1.11
This commit is contained in:
@@ -2,10 +2,11 @@
|
||||
mailchimp_id: Returns whether or not a given mailchimp key represents
|
||||
a valid list.
|
||||
"""
|
||||
import sys
|
||||
from optparse import make_option
|
||||
from __future__ import print_function
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
import sys
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from mailsnake import MailSnake
|
||||
|
||||
|
||||
@@ -14,30 +15,24 @@ 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 add_arguments(self, parser):
|
||||
parser.add_argument('--key',
|
||||
required=True,
|
||||
help='mailchimp api key')
|
||||
parser.add_argument('--webid',
|
||||
dest='web_id',
|
||||
type=int,
|
||||
required=True,
|
||||
help='mailchimp list web id')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""
|
||||
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)
|
||||
|
||||
@@ -47,8 +42,8 @@ class Command(BaseCommand):
|
||||
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'])
|
||||
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)
|
||||
print("list with web_id: {} not found.".format(web_id))
|
||||
sys.exit(1)
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
Synchronizes the announcement list with all active students.
|
||||
"""
|
||||
import logging
|
||||
from optparse import make_option
|
||||
|
||||
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
|
||||
|
||||
@@ -16,27 +15,20 @@ 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 add_arguments(self, parser):
|
||||
parser.add_argument('--key',
|
||||
required=True,
|
||||
help='mailchimp api key')
|
||||
parser.add_argument('--list',
|
||||
dest='list_id',
|
||||
required=True,
|
||||
help='mailchimp list id')
|
||||
|
||||
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')
|
||||
|
||||
|
||||
@@ -7,12 +7,10 @@ import math
|
||||
import random
|
||||
from collections import namedtuple
|
||||
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 opaque_keys.edx.keys import CourseKey
|
||||
|
||||
from student.models import UserProfile, unique_id_for_user
|
||||
|
||||
BATCH_SIZE = 15000
|
||||
@@ -29,38 +27,32 @@ class Command(BaseCommand):
|
||||
"""
|
||||
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.'
|
||||
|
||||
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'),
|
||||
make_option('--course', action='store', dest='course_id',
|
||||
help='xmodule course_id'),
|
||||
|
||||
make_option('--segments', action='store', dest='segments',
|
||||
default=0, type=int,
|
||||
help='number of static random segments to create'),
|
||||
)
|
||||
|
||||
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')
|
||||
|
||||
if not options['course_id']:
|
||||
raise CommandError('missing course id')
|
||||
|
||||
return (options['key'], options['list_id'],
|
||||
options['course_id'], options['segments'])
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--key',
|
||||
required=True,
|
||||
help='mailchimp api key')
|
||||
parser.add_argument('--list',
|
||||
dest='list_id',
|
||||
required=True,
|
||||
help='mailchimp list id')
|
||||
parser.add_argument('--course',
|
||||
dest='course_id',
|
||||
required=True,
|
||||
help='edx course_id')
|
||||
parser.add_argument('--segments',
|
||||
dest='num_segments',
|
||||
type=int,
|
||||
default=0,
|
||||
help='number of static random segments to create')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""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)
|
||||
|
||||
@@ -87,7 +79,7 @@ class Command(BaseCommand):
|
||||
unsubscribe(mailchimp, list_id, non_enrolled_emails)
|
||||
|
||||
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):
|
||||
|
||||
Reference in New Issue
Block a user