1 # -*- coding: utf-8 -*-
3 from optparse import make_option
5 from django.core.management.base import BaseCommand
6 from django.conf import settings
7 from wtem.management.commands import send_mail
8 from django.template.loader import render_to_string
10 from wtem.models import Submission, DEBUG_KEY
13 class Command(BaseCommand):
14 help = 'Sends personalized links to WTEM contestants'
15 args = '<email_address1>, <email_address2>, ...'
17 option_list = BaseCommand.option_list + (
23 help='Use all available submissions'),
29 help='Force sending key even if one was already sent')
32 def handle(self, *args, **options):
33 if len(args) or options['all']:
34 return self.send_keys(*args, **options)
35 self.stdout.write('No submissions selected')
37 def send_keys(self, *args, **options):
41 submissions = Submission.objects.all()
42 if not options['force']:
43 submissions = submissions.filter(key_sent=False)
45 submissions = submissions.filter(email__in=args)
47 for submission in submissions:
48 assert len(submission.key) == 30 or (settings.DEBUG and submission.key == DEBUG_KEY)
51 self.send_key(submission)
52 except Exception as e:
54 self.stdout.write('failed sending to: ' + submission.email + ' - ' + repr(e))
56 submission.key_sent = True
59 self.stdout.write('key sent to: ' + submission.email)
61 self.stdout.write('sent: ' + str(sent))
63 def send_key(self, submission):
64 self.stdout.write('>>> sending to ' + submission.email)
66 subject="Olimpiada Cyfrowa - Twój link do zadań I etapu",
67 body=render_to_string('wtem/email_key.txt', dict(submission=submission)),
68 to=[submission.email])