Sending keys wip: up to point of actually sending an email
[edumed.git] / wtem / management / commands / wtem_send_keys.py
1 from optparse import make_option
2
3 from django.core.management.base import BaseCommand, CommandError
4 from django.conf import settings
5
6 from wtem.models import Submission, DEBUG_KEY
7
8
9 class Command(BaseCommand):
10     help = 'Sends personalized links to WTEM contestants'
11     args = '<email_address1>, <email_address2>, ...'
12
13     option_list = BaseCommand.option_list + (
14         make_option('--all',
15             action='store_true',
16             dest='all',
17             default=False,
18             help='Use all available submissions'),
19         make_option('--force',
20             action='store_true',
21             dest='force',
22             default=False,
23             help='Force sending key even if one was already sent')
24         )
25
26     def handle(self, *args, **options):
27         if len(args) or options['all']:
28             return self.send_keys(*args, **options)
29         self.stdout.write('No submissions selected')
30
31     def send_keys(self, *args, **options):
32         sent = 0
33         skipped = 0
34         failed = 0
35
36         query = Submission.objects.all()
37         if not options['force']:
38             query = query.filter(key_sent = False)
39         if len(args):
40             query = query.filter(email__in = args)
41
42         for submission in query.all():
43             assert len(submission.key) == 30 or (settings.DEBUG and submission.key == DEBUG_KEY)
44
45             try:
46                 self.send_key(submission)
47             except:
48                 failed += 1
49                 self.stdout.write('failed sending to: ' + submission.email)
50             else:
51                 submission.key_sent = True
52                 submission.save()
53                 sent += 1
54                 self.stdout.write('key sent to: ' + submission.email)
55
56         self.stdout.write('sent: ' + str(sent))
57
58     def send_key(self, submission):
59         self.stdout.write('>>> sending to ' + submission.email)