Added management commands to show saved messages (unsent and otherwise) and to send or re-send saved messages as needed.

- Legacy-Id: 17351
This commit is contained in:
Henrik Levkowetz 2020-02-26 18:14:29 +00:00
parent 46ab91ec1d
commit 3562af0b9b
3 changed files with 148 additions and 0 deletions
ietf/message/management/commands

View file

@ -0,0 +1,2 @@
# Copyright The IETF Trust 2020, All Rights Reserved

View file

@ -0,0 +1,76 @@
# Copyright The IETF Trust 2020, All Rights Reserved
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import email
import smtplib
from django.core.management.base import BaseCommand
import debug # pyflakes:ignore
from ietf.message.models import Message
from ietf.utils.mail import send_mail_message
class Command(BaseCommand):
help = 'Show messages, by default unsent messages'
def add_arguments(self, parser):
parser.add_argument(
'--pks', dest='primary_keys',
help="Send the messages with the given primary keys. Accepts a comma-separated list of keys.",
)
parser.add_argument(
'--resend', action="store_true", default=False,
help="Re-send messages (ignoring that they are marked as already sent)."
)
parser.add_argument(
'-t', '--start', '--from', type=str, default=None,
help='Limit the list to messages saved after the given time (default %(default)s).',
)
parser.add_argument(
'--stop', '--to', type=str, default=None,
help='Limit the list to messages saved after the given time.',
)
parser.add_argument(
'--unsent', action="store_true", default=False,
help="Send only the unsent messages from the PKs or date range given",
)
def handle(self, *args, **options):
start = options['start']
stop = options['stop']
pks = options['primary_keys']
resend= options['resend']
unsent= options['unsent']
if pks:
primary_keys = [pk.strip() for pk in pks.split(',')]
else:
primary_keys = []
messages = Message.objects.all()
if primary_keys:
messages = messages.filter(pk__in=primary_keys)
if start:
messages = messages.filter(time__gte=start)
if stop:
messages = messages.filter(sent__lte=stop)
sent = messages.filter(sent__isnull=False)
if sent.exists() and not resend and not unsent:
self.stderr.write("Error: Asked to send one or more already sent messages, and --resend not given")
for m in sent:
to = ','.join( a[1] for a in email.utils.getaddresses([m.to]) )
self.stderr.write(' sent %s: %s %s -> %s "%s"' % (m.sent.strftime('%Y-%m-%d %H:%M'), m.pk, m.frm, to, m.subject.strip()))
else:
if unsent:
messages = messages.filter(sent__isnull=True)
for m in messages:
to = ','.join( a[1] for a in email.utils.getaddresses([m.to]) )
try:
send_mail_message(None, m)
self.stdout.write('%s %s -> %s "%s"' % (m.pk, m.frm, to, m.subject.strip()))
except smtplib.SMTPException as e:
self.stdout.write('Failure %s: %s %s -> %s "%s"' % (e, m.pk, m.frm, to, m.subject.strip()))

View file

@ -0,0 +1,70 @@
# Copyright The IETF Trust 2020, All Rights Reserved
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import email
import datetime
from django.core.management.base import BaseCommand
import debug # pyflakes:ignore
from ietf.message.models import Message
class Command(BaseCommand):
help = 'Show messages, by default unsent messages'
def add_arguments(self, parser):
default_start = datetime.datetime.now() - datetime.timedelta(days=14)
parser.add_argument(
'-t', '--start', '--from', type=str, default=default_start.strftime('%Y-%m-%d %H:%M'),
help='Limit the list to messages saved after the given time (default %(default)s).',
)
parser.add_argument(
'--stop', '--to', type=str, default=None,
help='Limit the list to messages saved after the given time.',
)
parser.add_argument(
'-p', '--pk', action="store_true", default=False,
help='output only a list of primary keys.',
)
selection = parser.add_mutually_exclusive_group()
selection.add_argument(
'-a', '--all', action='store_const', dest='state', const='all',
help='Shows a list of all messages.',
)
selection.add_argument(
'-u', '--unsent', action='store_const', dest='state', const='unsent',
help='Shows a list of unsent messages',
)
selection.add_argument(
'-s', '--sent', action='store_const', dest='state', const='sent',
help='Shows a list of sent messages.',
)
def handle(self, *args, **options):
messages = Message.objects.all()
if options['state'] == 'sent':
messages = messages.filter(sent__isnull=False)
elif options['state'] == 'unsent':
messages = messages.filter(sent__isnull=True)
else:
options['state'] = 'all'
messages = messages.filter(time__gte=options['start'])
if options['stop']:
messages = messages.filter(sent__lte=options['stop'])
self.stdout.write("\nShowimg %s messages between %s and %s:\n\n" % (options['state'], options['start'], options['stop']))
else:
self.stdout.write("\nShowimg %s messages since %s:\n\n" % (options['state'], options['start']))
if options['pk']:
self.stdout.write(','.join([ str(pk) for pk in messages.values_list('pk', flat=True)] ))
else:
for m in messages:
to = ','.join( a[1] for a in email.utils.getaddresses([m.to]) )
self.stdout.write('%s %16s %16s %72s %s -> %s "%s"\n' %
(m.pk, m.time.strftime('%Y-%m-%d %H:%M'), m.sent and m.sent.strftime('%Y-%m-%d %H:%M') or '',
m.msgid.strip('<>'), m.frm, to, m.subject.strip()))