Commit e55d85a6 by Chris Dodge

Put a force send notifications digest in edx-platform

parent 9eda17dd
"""
Django management command to force-send the daily/weekly digest emails
"""
import logging
import logging.config
import sys
# Have all logging go to stdout with management commands
# this must be up at the top otherwise the
# configuration does not appear to take affect
import datetime
import pytz
from django.conf import settings
from edx_notifications import const
from edx_notifications.digests import send_notifications_digest, send_notifications_namespace_digest
LOGGING = {
'version': 1,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'stream': sys.stdout,
}
},
'root': {
'handlers': ['console'],
'level': 'INFO'
}
}
logging.config.dictConfig(LOGGING)
from django.core.management.base import BaseCommand, CommandError
log = logging.getLogger(__file__)
from optparse import make_option, OptionParser
class Command(BaseCommand):
"""
Django management command to force-send the daily/weekly digest emails
"""
help = 'Command to force-send the daily/weekly digest emails'
option_list = BaseCommand.option_list + (
make_option(
'--daily',
action='store_true',
dest='send_daily_digest',
default=False,
help='Force send daily digest email.'
),
)
option_list = option_list + (
make_option(
'--weekly',
action='store_true',
dest='send_weekly_digest',
default=False,
help='Force send weekly digest email.'
),
)
option_list = option_list + (
make_option(
'--ns',
dest='namespace',
default='All',
help='Specify the namespace. Default = All.'
),
)
def send_daily_digest(self, namespace='All'):
"""
Sends the daily digest.
"""
from_timestamp = datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=1)
to_timestamp = datetime.datetime.now(pytz.UTC)
preference_name = const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME
subject = const.NOTIFICATION_DAILY_DIGEST_SUBJECT
from_email = const.NOTIFICATION_DIGEST_FROM_ADDRESS
if namespace == "All":
digests_sent = send_notifications_digest(from_timestamp, to_timestamp, preference_name, subject, from_email)
else:
digests_sent = send_notifications_namespace_digest(
namespace, from_timestamp, to_timestamp, preference_name, subject, from_email
)
return digests_sent
def send_weekly_digest(self, namespace='All'):
"""
Sends the weekly digest.
"""
from_timestamp = datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=7)
to_timestamp = datetime.datetime.now(pytz.UTC)
preference_name = const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME
subject = const.NOTIFICATION_WEEKLY_DIGEST_SUBJECT
from_email = const.NOTIFICATION_DIGEST_FROM_ADDRESS
if namespace == "All":
digests_sent = send_notifications_digest(from_timestamp, to_timestamp, preference_name, subject, from_email)
else:
digests_sent = send_notifications_namespace_digest(
namespace, from_timestamp, to_timestamp, preference_name, subject, from_email
)
return digests_sent
def handle(self, *args, **options):
"""
Management command entry point, simply call into the send_notifications_digest or the
send_notifications_namespace_digest depending on the passed the parameters.
The expected command line arguments are:
--daily: Sends the daily digest.
--weekly: Sends the weekly digest.
--ns=NAMESPACE : Sends the notifications for the particular NAMESPACE.
"""
if not settings.FEATURES.get('ENABLE_NOTIFICATIONS', False):
print 'ENABLE_NOTIFICATIONS not set to "true". Stopping...'
return
usage = "usage: %prog [--daily] [--weekly] [--ns=NAMESPACE]"
parser = OptionParser(usage=usage)
log.info("Running management command ...")
if options['send_daily_digest']:
log.info("Sending the daily digest with namespace=%s...", options['namespace'])
weekly_digests_sent = self.send_daily_digest(options['namespace'])
log.info("Successfully sent %s digests...", weekly_digests_sent)
if options['send_weekly_digest']:
log.info("Sending the weekly digest with namespace=%s...", options['namespace'])
daily_digests_sent = self.send_weekly_digest(options['namespace'])
log.info("Successfully sent %s digests...", daily_digests_sent)
if not options['send_weekly_digest'] and not options['send_daily_digest']:
parser.print_help()
raise CommandError("Neither Daily, nor Weekly digest specified.")
log.info("Completed .")
"""
Tests for the Django management command force_send_digest
"""
import mock
from django.conf import settings
from django.test import TestCase
from student.management.commands import force_send_notification_digest
@mock.patch.dict(settings.FEATURES, {'ENABLE_NOTIFICATIONS': True})
class ForceSendDigestCommandTest(TestCase):
def test_command_check(self):
# run the management command for purging notifications.
force_send_notification_digest.Command().handle(**{'send_daily_digest': True, 'send_weekly_digest': True, 'namespace': 'All'})
force_send_notification_digest.Command().handle(**{'send_daily_digest': True, 'send_weekly_digest': True, 'namespace': 'ABC'})
# force_send_digest.Command().handle(**{'send_daily_digest': False, 'send_weekly_digest': False, 'namespace': 'ABC'})
......@@ -10,5 +10,5 @@
-e git+https://github.com/edx-solutions/xblock-adventure.git@f908e087923231477499e2c455d356d286293641#egg=xblock-adventure
-e git+https://github.com/mckinseyacademy/xblock-poll.git@ca0e6eb4ef10c128d573c3cec015dcfee7984730#egg=xblock-poll
-e git+https://github.com/OfficeDev/xblock-officemix/@86238f5968a08db005717dbddc346808f1ed3716#egg=xblock_officemix-master
-e git+https://github.com/edx/edx-notifications.git@1496385f16d2571bc5d958c17e30f3eac3869b8e#egg=edx-notifications
-e git+https://github.com/edx/edx-notifications.git@ac87984ebfa0c80cb2e215f684165310bbd8bc62#egg=edx-notifications
-e git+https://github.com/open-craft/problem-builder.git@5e00f92d78da0b28ae7f39c3f03596f49bec7119#egg=problem-builder
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