update emails for stage2
[edumed.git] / wtem / management / commands / wtem_send_results.py
index 94c2653..5a02001 100644 (file)
@@ -2,38 +2,58 @@
 
 from optparse import make_option
 
 
 from optparse import make_option
 
+from collections import defaultdict
 from django.core.management.base import BaseCommand
 from django.core.management.base import BaseCommand
-from django.conf import settings
 from wtem.management.commands import send_mail
 from django.utils import translation
 from django.template.loader import render_to_string
 
 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():
 from wtem.models import Submission
 
 
 def get_submissions():
-    return Submission.objects.exclude(answers = None).all()
+    return sorted(Submission.objects.exclude(answers=None), key=lambda s: -s.final_result)
+
+minimum = 63.06
 
 
-minimum = 47.5
 
 class Command(BaseCommand):
 
     option_list = BaseCommand.option_list + (
 
 class Command(BaseCommand):
 
     option_list = BaseCommand.option_list + (
-        make_option('--to-teachers',
+        make_option(
+            '--to-teachers',
             action='store_true',
             dest='to_teachers',
             default=False,
             help='Send emails to teachers'),
             action='store_true',
             dest='to_teachers',
             default=False,
             help='Send emails to teachers'),
-        make_option('--to-students',
+        make_option(
+            '--to-students',
             action='store_true',
             dest='to_students',
             default=False,
             help='Send emails to students'),
             action='store_true',
             dest='to_students',
             default=False,
             help='Send emails to students'),
+        make_option(
+            '--only-to',
+            action='store',
+            dest='only_to',
+            default=None,
+            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')
     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
         for target in ['to_teachers', 'to_students']:
             if options[target]:
                 self.sent = 0
@@ -42,52 +62,52 @@ class Command(BaseCommand):
 
     def handle_to_students(self, *args, **options):
         self.stdout.write('>>> Sending results to students')
 
     def handle_to_students(self, *args, **options):
         self.stdout.write('>>> Sending results to students')
-        subject = 'Twój wynik w I etapie Wielkiego Turnieju Edukacji Medialnej'
+        subject = 'Wyniki I etapu Olimpiady Cyfrowej'
 
         for submission in get_submissions():
 
         for submission in get_submissions():
+            if options['only_to'] and submission.email != options['only_to']:
+                continue
             final_result = submission.final_result
             if final_result < minimum:
                 template = 'results_student_failed.txt'
             else:
                 template = 'results_student_passed.txt'
             final_result = submission.final_result
             if final_result < minimum:
                 template = 'results_student_failed.txt'
             else:
                 template = 'results_student_passed.txt'
-            message = render_to_string('wtem/' + template, dict(final_result = submission.final_result))
+            message = render_to_string('wtem/' + template, dict(final_result=submission.final_result))
             self.send_message(message, subject, submission.email)
 
         self.sum_up()
 
             self.send_message(message, subject, submission.email)
 
         self.sum_up()
 
-    def handle_to_teachers(self, *args, **kwargs):
+    def handle_to_teachers(self, *args, **options):
         self.stdout.write('>>> Sending results to teachers')
         self.stdout.write('>>> Sending results to teachers')
-        subject = 'Wyniki Twoich uczniów w I etapie Wielkiego Turnieju Edukacji Medialnej'
-        failed = sent = 0
+        subject = 'Wyniki I etapu Olimpiady Cyfrowej'
 
 
-        submissions_by_contact = dict()
+        submissions_by_contact = defaultdict(list)
 
         for submission in get_submissions():
 
         for submission in get_submissions():
-            submissions_by_contact.setdefault(submission.contact.id, []).append(submission)
+            if options['only_to'] and submission.contact.contact != options['only_to']:
+                continue
+            submissions_by_contact[submission.contact.contact].append(submission)
 
 
-        for contact_id, submissions in submissions_by_contact.items():
-            contact = Contact.objects.get(id=contact_id)
-            message = render_to_string('wtem/results_teacher.txt', dict(submissions = submissions))
-            self.send_message(message, subject, contact.contact)
+        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_email)
 
         self.sum_up()
 
 
         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)
         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:
         try:
-            send_mail(
-                subject = subject,
-                body = message,
-                to = [email]
-            )
-        except:
+            send_mail(subject=subject, body=message, to=[email])
+        except BaseException, e:
             self.failed += 1
             self.stdout.write('failed sending to: ' + email + ': ' + str(e))
         else:
             self.sent += 1
             self.stdout.write('message sent to: ' + email)
             self.failed += 1
             self.stdout.write('failed sending to: ' + email + ': ' + str(e))
         else:
             self.sent += 1
             self.stdout.write('message sent to: ' + email)
-
-