X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/a0e5579f9eba72add85eb75c6267bdf3c1313432..abb9819943355e192b04fc9aedcbd6fbc44bc5f9:/wtem/management/commands/wtem_send_results.py?ds=inline diff --git a/wtem/management/commands/wtem_send_results.py b/wtem/management/commands/wtem_send_results.py index ef593e7..ddc7aa4 100644 --- a/wtem/management/commands/wtem_send_results.py +++ b/wtem/management/commands/wtem_send_results.py @@ -2,19 +2,20 @@ from optparse import make_option +from collections import defaultdict from django.core.management.base import BaseCommand from wtem.management.commands import send_mail from django.utils import translation from django.template.loader import render_to_string -from contact.models import Contact from wtem.models import Submission def get_submissions(): - return sorted(Submission.objects.exclude(answers=None).all(), key=lambda s: -s.final_result) + return sorted(Submission.objects.exclude(answers=None), key=lambda s: -s.final_result) -minimum = 52 + +minimum = 67.33 class Command(BaseCommand): @@ -37,11 +38,23 @@ class Command(BaseCommand): action='store', dest='only_to', default=None, - help='Send emails only to listed addresses'), + help='Send email only to one address'), + make_option( + '--dummy', + action='store_true', + dest='dummy', + default=False, + help='Print emails instead of sending them'), ) + def __init__(self): + super(Command, self).__init__() + self.sent = self.failed = None + self.dummy = None + def handle(self, *args, **options): translation.activate('pl') + self.dummy = options['dummy'] for target in ['to_teachers', 'to_students']: if options[target]: self.sent = 0 @@ -69,25 +82,28 @@ class Command(BaseCommand): self.stdout.write('>>> Sending results to teachers') subject = 'Wyniki I etapu Olimpiady Cyfrowej' - submissions_by_contact = dict() + submissions_by_contact = defaultdict(list) for submission in get_submissions(): if options['only_to'] and submission.contact.contact != options['only_to']: continue - submissions_by_contact.setdefault(submission.contact.id, []).append(submission) + submissions_by_contact[submission.contact.contact].append(submission) - for contact_id, submissions in submissions_by_contact.items(): - contact = Contact.objects.get(id=contact_id) + for contact_email, submissions in submissions_by_contact.items(): message = render_to_string('wtem/results_teacher.txt', dict(submissions=submissions)) - self.send_message(message, subject, contact.contact) + self.send_message(message, subject, contact_email) self.sum_up() - def sum_up(self): + def sum_up(self): self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed)) def send_message(self, message, subject, email): self.stdout.write('>>> sending results to %s' % email) + if self.dummy: + self.stdout.write(message) + self.sent += 1 + return try: send_mail(subject=subject, body=message, to=[email]) except BaseException, e: