94c2653f97d2abf1d61645bb4340d24dc9c56eed
[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 Submission.objects.exclude(answers = None).all()
17
18 minimum = 47.5
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     )
34
35     def handle(self, *args, **options):
36         translation.activate('pl')
37         for target in ['to_teachers', 'to_students']:
38             if options[target]:
39                 self.sent = 0
40                 self.failed = 0
41                 getattr(self, 'handle_' + target)(*args, **options)
42
43     def handle_to_students(self, *args, **options):
44         self.stdout.write('>>> Sending results to students')
45         subject = 'Twój wynik w I etapie Wielkiego Turnieju Edukacji Medialnej'
46
47         for submission in get_submissions():
48             final_result = submission.final_result
49             if final_result < minimum:
50                 template = 'results_student_failed.txt'
51             else:
52                 template = 'results_student_passed.txt'
53             message = render_to_string('wtem/' + template, dict(final_result = submission.final_result))
54             self.send_message(message, subject, submission.email)
55
56         self.sum_up()
57
58     def handle_to_teachers(self, *args, **kwargs):
59         self.stdout.write('>>> Sending results to teachers')
60         subject = 'Wyniki Twoich uczniów w I etapie Wielkiego Turnieju Edukacji Medialnej'
61         failed = sent = 0
62
63         submissions_by_contact = dict()
64
65         for submission in get_submissions():
66             submissions_by_contact.setdefault(submission.contact.id, []).append(submission)
67
68         for contact_id, submissions in submissions_by_contact.items():
69             contact = Contact.objects.get(id=contact_id)
70             message = render_to_string('wtem/results_teacher.txt', dict(submissions = submissions))
71             self.send_message(message, subject, contact.contact)
72
73         self.sum_up()
74
75     def sum_up(self):        
76         self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed))
77
78     def send_message(self, message, subject, email):
79         self.stdout.write('>>> sending results to %s' % email)
80         try:
81             send_mail(
82                 subject = subject,
83                 body = message,
84                 to = [email]
85             )
86         except:
87             self.failed += 1
88             self.stdout.write('failed sending to: ' + email + ': ' + str(e))
89         else:
90             self.sent += 1
91             self.stdout.write('message sent to: ' + email)
92
93