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