add submodule librarian
[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 django.conf import settings
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 contact.models import Contact
12 from wtem.models import Submission
13
14
15 def get_submissions():
16     return sorted(Submission.objects.exclude(answers = None).all(), key=lambda s: -s.final_result)
17
18 minimum = 55
19
20 class Command(BaseCommand):
21
22     option_list = BaseCommand.option_list + (
23         make_option('--to-teachers',
24             action='store_true',
25             dest='to_teachers',
26             default=False,
27             help='Send emails to teachers'),
28         make_option('--to-students',
29             action='store_true',
30             dest='to_students',
31             default=False,
32             help='Send emails to students'),
33         make_option('--only-to',
34             action='store',
35             dest='only_to',
36             default=None,
37             help='Send emails to students'),
38     )
39
40     def handle(self, *args, **options):
41         translation.activate('pl')
42         for target in ['to_teachers', 'to_students']:
43             if options[target]:
44                 self.sent = 0
45                 self.failed = 0
46                 getattr(self, 'handle_' + target)(*args, **options)
47
48     def handle_to_students(self, *args, **options):
49         self.stdout.write('>>> Sending results to students')
50         subject = 'Wyniki I etapu Wielkiego Turnieju Edukacji Medialnej'
51
52         for submission in get_submissions():
53             if options['only_to'] and submission.email != options['only_to']:
54                 continue
55             final_result = submission.final_result
56             if final_result < minimum:
57                 template = 'results_student_failed.txt'
58             else:
59                 template = 'results_student_passed.txt'
60             message = render_to_string('wtem/' + template, dict(final_result = submission.final_result))
61             self.send_message(message, subject, submission.email)
62
63         self.sum_up()
64
65     def handle_to_teachers(self, *args, **options):
66         self.stdout.write('>>> Sending results to teachers')
67         subject = 'Wyniki I etapu Wielkiego Turnieju Edukacji Medialnej'
68         failed = sent = 0
69
70         submissions_by_contact = dict()
71
72         for submission in get_submissions():
73             if options['only_to'] and submission.contact.contact != options['only_to']:
74                 continue
75             submissions_by_contact.setdefault(submission.contact.id, []).append(submission)
76
77         for contact_id, submissions in submissions_by_contact.items():
78             contact = Contact.objects.get(id=contact_id)
79             message = render_to_string('wtem/results_teacher.txt', dict(submissions = submissions))
80             self.send_message(message, subject, contact.contact)
81
82         self.sum_up()
83
84     def sum_up(self):        
85         self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed))
86
87     def send_message(self, message, subject, email):
88         self.stdout.write('>>> sending results to %s' % email)
89         try:
90             send_mail(
91                 subject = subject,
92                 body = message,
93                 to = [email]
94             )
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)
101
102