Skip to content

Commit 3562af0

Browse files
committed
Added management commands to show saved messages (unsent and otherwise) and to send or re-send saved messages as needed.
- Legacy-Id: 17351
1 parent 46ab91e commit 3562af0

3 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright The IETF Trust 2020, All Rights Reserved
2+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright The IETF Trust 2020, All Rights Reserved
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import absolute_import, print_function, unicode_literals
5+
6+
7+
import email
8+
import smtplib
9+
10+
from django.core.management.base import BaseCommand
11+
12+
import debug # pyflakes:ignore
13+
14+
from ietf.message.models import Message
15+
from ietf.utils.mail import send_mail_message
16+
17+
class Command(BaseCommand):
18+
help = 'Show messages, by default unsent messages'
19+
20+
def add_arguments(self, parser):
21+
parser.add_argument(
22+
'--pks', dest='primary_keys',
23+
help="Send the messages with the given primary keys. Accepts a comma-separated list of keys.",
24+
)
25+
parser.add_argument(
26+
'--resend', action="store_true", default=False,
27+
help="Re-send messages (ignoring that they are marked as already sent)."
28+
)
29+
parser.add_argument(
30+
'-t', '--start', '--from', type=str, default=None,
31+
help='Limit the list to messages saved after the given time (default %(default)s).',
32+
)
33+
parser.add_argument(
34+
'--stop', '--to', type=str, default=None,
35+
help='Limit the list to messages saved after the given time.',
36+
)
37+
parser.add_argument(
38+
'--unsent', action="store_true", default=False,
39+
help="Send only the unsent messages from the PKs or date range given",
40+
)
41+
42+
def handle(self, *args, **options):
43+
start = options['start']
44+
stop = options['stop']
45+
pks = options['primary_keys']
46+
resend= options['resend']
47+
unsent= options['unsent']
48+
49+
if pks:
50+
primary_keys = [pk.strip() for pk in pks.split(',')]
51+
else:
52+
primary_keys = []
53+
54+
messages = Message.objects.all()
55+
if primary_keys:
56+
messages = messages.filter(pk__in=primary_keys)
57+
if start:
58+
messages = messages.filter(time__gte=start)
59+
if stop:
60+
messages = messages.filter(sent__lte=stop)
61+
sent = messages.filter(sent__isnull=False)
62+
if sent.exists() and not resend and not unsent:
63+
self.stderr.write("Error: Asked to send one or more already sent messages, and --resend not given")
64+
for m in sent:
65+
to = ','.join( a[1] for a in email.utils.getaddresses([m.to]) )
66+
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()))
67+
else:
68+
if unsent:
69+
messages = messages.filter(sent__isnull=True)
70+
for m in messages:
71+
to = ','.join( a[1] for a in email.utils.getaddresses([m.to]) )
72+
try:
73+
send_mail_message(None, m)
74+
self.stdout.write('%s %s -> %s "%s"' % (m.pk, m.frm, to, m.subject.strip()))
75+
except smtplib.SMTPException as e:
76+
self.stdout.write('Failure %s: %s %s -> %s "%s"' % (e, m.pk, m.frm, to, m.subject.strip()))
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright The IETF Trust 2020, All Rights Reserved
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import absolute_import, print_function, unicode_literals
5+
6+
import email
7+
import datetime
8+
9+
from django.core.management.base import BaseCommand
10+
11+
import debug # pyflakes:ignore
12+
13+
from ietf.message.models import Message
14+
15+
class Command(BaseCommand):
16+
help = 'Show messages, by default unsent messages'
17+
18+
def add_arguments(self, parser):
19+
default_start = datetime.datetime.now() - datetime.timedelta(days=14)
20+
parser.add_argument(
21+
'-t', '--start', '--from', type=str, default=default_start.strftime('%Y-%m-%d %H:%M'),
22+
help='Limit the list to messages saved after the given time (default %(default)s).',
23+
)
24+
parser.add_argument(
25+
'--stop', '--to', type=str, default=None,
26+
help='Limit the list to messages saved after the given time.',
27+
)
28+
parser.add_argument(
29+
'-p', '--pk', action="store_true", default=False,
30+
help='output only a list of primary keys.',
31+
)
32+
selection = parser.add_mutually_exclusive_group()
33+
selection.add_argument(
34+
'-a', '--all', action='store_const', dest='state', const='all',
35+
help='Shows a list of all messages.',
36+
)
37+
selection.add_argument(
38+
'-u', '--unsent', action='store_const', dest='state', const='unsent',
39+
help='Shows a list of unsent messages',
40+
)
41+
selection.add_argument(
42+
'-s', '--sent', action='store_const', dest='state', const='sent',
43+
help='Shows a list of sent messages.',
44+
)
45+
46+
47+
def handle(self, *args, **options):
48+
messages = Message.objects.all()
49+
if options['state'] == 'sent':
50+
messages = messages.filter(sent__isnull=False)
51+
elif options['state'] == 'unsent':
52+
messages = messages.filter(sent__isnull=True)
53+
else:
54+
options['state'] = 'all'
55+
messages = messages.filter(time__gte=options['start'])
56+
if options['stop']:
57+
messages = messages.filter(sent__lte=options['stop'])
58+
self.stdout.write("\nShowimg %s messages between %s and %s:\n\n" % (options['state'], options['start'], options['stop']))
59+
else:
60+
self.stdout.write("\nShowimg %s messages since %s:\n\n" % (options['state'], options['start']))
61+
62+
if options['pk']:
63+
self.stdout.write(','.join([ str(pk) for pk in messages.values_list('pk', flat=True)] ))
64+
else:
65+
for m in messages:
66+
to = ','.join( a[1] for a in email.utils.getaddresses([m.to]) )
67+
self.stdout.write('%s %16s %16s %72s %s -> %s "%s"\n' %
68+
(m.pk, m.time.strftime('%Y-%m-%d %H:%M'), m.sent and m.sent.strftime('%Y-%m-%d %H:%M') or '',
69+
m.msgid.strip('<>'), m.frm, to, m.subject.strip()))
70+

0 commit comments

Comments
 (0)