1 # -*- coding: utf-8 -*-
3 from optparse import make_option
5 from django.core.management.base import BaseCommand
6 from wtem.management.commands import send_mail
7 from django.template.loader import render_to_string
9 from stage2.models import Participant
12 class Command(BaseCommand):
13 help = 'Sends personalized links to WTEM contestants'
14 args = '<email_address1>, <email_address2>, ...'
16 option_list = BaseCommand.option_list + (
22 help='Use all available participants'),
28 help='Force sending key even if one was already sent')
31 def handle(self, *args, **options):
32 if len(args) or options['all']:
33 return self.send_keys(*args, **options)
34 self.stdout.write('No participants selected')
36 def send_keys(self, *args, **options):
40 participants = Participant.objects.all()
41 if not options['force']:
42 participants = participants.filter(key_sent=False)
44 participants = participants.filter(email__in=args)
46 for participant in participants:
47 assert len(participant.key) == 30
50 self.send_key(participant)
51 except Exception as e:
53 self.stdout.write('failed sending to: ' + participant.email + ' - ' + str(e))
55 participant.key_sent = True
58 self.stdout.write('key sent to: ' + participant.email)
60 self.stdout.write('sent: ' + str(sent))
62 def send_key(self, participant):
63 self.stdout.write('>>> sending to ' + participant.email)
65 subject="Olimpiada Cyfrowa - Twój link do drugiego etapu",
66 body=render_to_string('stage2/email_key.txt', {'participant': participant}),
67 to=[participant.email])