update emails for stage2
[edumed.git] / wtem / management / commands / wtem_send_results.py
1 # -*- coding: utf-8 -*-
2
3 from optparse import make_option
4
5 from collections import defaultdict
6 from django.core.management.base import BaseCommand
7 from wtem.management.commands import send_mail
8 from django.utils import translation
9 from django.template.loader import render_to_string
10
11 from wtem.models import Submission
12
13
14 def get_submissions():
15     return sorted(Submission.objects.exclude(answers=None), key=lambda s: -s.final_result)
16
17 minimum = 63.06
18
19
20 class Command(BaseCommand):
21
22     option_list = BaseCommand.option_list + (
23         make_option(
24             '--to-teachers',
25             action='store_true',
26             dest='to_teachers',
27             default=False,
28             help='Send emails to teachers'),
29         make_option(
30             '--to-students',
31             action='store_true',
32             dest='to_students',
33             default=False,
34             help='Send emails to students'),
35         make_option(
36             '--only-to',
37             action='store',
38             dest='only_to',
39             default=None,
40             help='Send email only to one address'),
41         make_option(
42             '--dummy',
43             action='store_true',
44             dest='dummy',
45             default=False,
46             help='Print emails instead of sending them'),
47     )
48
49     def __init__(self):
50         super(Command, self).__init__()
51         self.sent = self.failed = None
52         self.dummy = None
53
54     def handle(self, *args, **options):
55         translation.activate('pl')
56         self.dummy = options['dummy']
57         for target in ['to_teachers', 'to_students']:
58             if options[target]:
59                 self.sent = 0
60                 self.failed = 0
61                 getattr(self, 'handle_' + target)(*args, **options)
62
63     def handle_to_students(self, *args, **options):
64         self.stdout.write('>>> Sending results to students')
65         subject = 'Wyniki I etapu Olimpiady Cyfrowej'
66
67         for submission in get_submissions():
68             if options['only_to'] and submission.email != options['only_to']:
69                 continue
70             final_result = submission.final_result
71             if final_result < minimum:
72                 template = 'results_student_failed.txt'
73             else:
74                 template = 'results_student_passed.txt'
75             message = render_to_string('wtem/' + template, dict(final_result=submission.final_result))
76             self.send_message(message, subject, submission.email)
77
78         self.sum_up()
79
80     def handle_to_teachers(self, *args, **options):
81         self.stdout.write('>>> Sending results to teachers')
82         subject = 'Wyniki I etapu Olimpiady Cyfrowej'
83
84         submissions_by_contact = defaultdict(list)
85
86         for submission in get_submissions():
87             if options['only_to'] and submission.contact.contact != options['only_to']:
88                 continue
89             submissions_by_contact[submission.contact.contact].append(submission)
90
91         for contact_email, submissions in submissions_by_contact.items():
92             message = render_to_string('wtem/results_teacher.txt', dict(submissions=submissions))
93             self.send_message(message, subject, contact_email)
94
95         self.sum_up()
96
97     def sum_up(self):
98         self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed))
99
100     def send_message(self, message, subject, email):
101         self.stdout.write('>>> sending results to %s' % email)
102         if self.dummy:
103             self.stdout.write(message)
104             self.sent += 1
105             return
106         try:
107             send_mail(subject=subject, body=message, to=[email])
108         except BaseException, e:
109             self.failed += 1
110             self.stdout.write('failed sending to: ' + email + ': ' + str(e))
111         else:
112             self.sent += 1
113             self.stdout.write('message sent to: ' + email)