Newer
Older
import datetime
from django.conf import settings
from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand, CommandError
from alerts.models import Contact, Trigger
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Command(BaseCommand):
help="Delete Contacts and Notifications for inactive users"
def add_arguments(self, parser):
parser.add_argument('-q', '--quiet', action='store_true',
default=False, help='Suppress output')
def handle(self, *args, **options):
verbose = not options['quiet']
if verbose:
self.stdout.write(('Checking inactive users\' triggers and '
'contacts at {0}').format(datetime.datetime.utcnow()))
# Get contacts and triggers whose user is no longer in the LVC
lvc = Group.objects.get(name=settings.LVC_GROUP)
triggers = Trigger.objects.exclude(user__groups=lvc)
contacts = Contact.objects.exclude(user__groups=lvc)
# Generate log message
if verbose:
if triggers.exists():
t_log_msg = "Deleting {0} triggers: ".format(triggers.count())\
+ " | ".join([t.__str__() for t in triggers])
self.stdout.write(t_log_msg)
if contacts.exists():
c_log_msg = "Deleting {0} contacts: ".format(contacts.count())\
+ " | ".join([c.__str__() for c in contacts])
self.stdout.write(c_log_msg)
# Delete
triggers.delete()
contacts.delete()