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 ("\n Showimg %s messages between %s and %s:\n \n " % (options ['state' ], options ['start' ], options ['stop' ]))
59+ else :
60+ self .stdout .write ("\n Showimg %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