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