d75707a5f387588020731bc951b3c4fa22870309
[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 contact.models import Contact
11 from wtem.models import Submission
12
13
14 def get_submissions():
15     return sorted(Submission.objects.exclude(answers=None).all(), key=lambda s: -s.final_result)
16
17 minimum = 34
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     )
42
43     def __init__(self):
44         super(Command, self).__init__()
45         self.sent = self.failed = None
46
47     def handle(self, *args, **options):
48         translation.activate('pl')
49         for target in ['to_teachers', 'to_students']:
50             if options[target]:
51                 self.sent = 0
52                 self.failed = 0
53                 getattr(self, 'handle_' + target)(*args, **options)
54
55     def handle_to_students(self, *args, **options):
56         self.stdout.write('>>> Sending results to students')
57         subject = 'Wyniki I etapu Olimpiady Cyfrowej'
58
59         for submission in get_submissions():
60             if options['only_to'] and submission.email != options['only_to']:
61                 continue
62             final_result = submission.final_result
63             if final_result < minimum:
64                 template = 'results_student_failed.txt'
65             else:
66                 template = 'results_student_passed.txt'
67             message = render_to_string('wtem/' + template, dict(final_result=submission.final_result))
68             self.send_message(message, subject, submission.email)
69
70         self.sum_up()
71
72     def handle_to_teachers(self, *args, **options):
73         self.stdout.write('>>> Sending results to teachers')
74         subject = 'Wyniki I etapu Olimpiady Cyfrowej'
75
76         submissions_by_contact = dict()
77
78         for submission in get_submissions():
79             if options['only_to'] and submission.contact.contact != options['only_to']:
80                 continue
81             submissions_by_contact.setdefault(submission.contact.id, []).append(submission)
82
83         for contact_id, submissions in submissions_by_contact.items():
84             contact = Contact.objects.get(id=contact_id)
85             message = render_to_string('wtem/results_teacher.txt', dict(submissions=submissions))
86             self.send_message(message, subject, contact.contact)
87
88         self.sum_up()
89
90     def sum_up(self):        
91         self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed))
92
93     def send_message(self, message, subject, email):
94         self.stdout.write('>>> sending results to %s' % email)
95         try:
96             send_mail(subject=subject, body=message, to=[email])
97         except BaseException, e:
98             self.failed += 1
99             self.stdout.write('failed sending to: ' + email + ': ' + str(e))
100         else:
101             self.sent += 1
102             self.stdout.write('message sent to: ' + email)