Split promobox into 2 + carousel height fix
[edumed.git] / wtem / management / commands / wtem_send_results_csv.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     args = 'csv_filename'
22
23     option_list = BaseCommand.option_list + (
24         make_option('--to-teachers',
25             action='store_true',
26             dest='to_teachers',
27             default=False,
28             help='Send emails to teachers'),
29         make_option('--to-students',
30             action='store_true',
31             dest='to_students',
32             default=False,
33             help='Send emails to students'),
34         make_option('--only-to',
35             action='store',
36             dest='only_to',
37             default=None,
38             help='Send emails to students'),
39     )
40
41     def handle(self, csv_filename, *args, **options):
42         translation.activate('pl')
43         self.results = [line.decode('utf-8').strip('\n').split(',') for line in open(csv_filename)]
44         for target in ['to_teachers', 'to_students']:
45             if options[target]:
46                 self.sent = 0
47                 self.failed = 0
48                 getattr(self, 'handle_' + target)(*args, **options)
49
50     def handle_to_students(self, *args, **options):
51         self.stdout.write('>>> Sending results to students')
52         subject = 'Wyniki I etapu Wielkiego Turnieju Edukacji Medialnej'
53
54         for result in self.results:
55             if options['only_to'] and result[1] != options['only_to']:
56                 continue
57             final_result = result[4]
58             if result[5] != 'TAK':
59                 template = 'results_student_failed.txt'
60             else:
61                 template = 'results_student_passed.txt'
62             message = render_to_string('wtem/' + template, dict(final_result=final_result))
63             self.send_message(message, subject, result[1])
64
65         self.sum_up()
66
67     def handle_to_teachers(self, *args, **options):
68         self.stdout.write('>>> Sending results to teachers')
69         subject = 'Wyniki I etapu Wielkiego Turnieju Edukacji Medialnej'
70         failed = sent = 0
71
72         submissions_by_contact = dict()
73
74         from decimal import Decimal, InvalidOperation
75         def dec_or_0(s):
76             try:
77                 return Decimal(s)
78             except InvalidOperation:
79                 return Decimal(0)
80
81         for result in sorted(self.results, key=lambda r: dec_or_0(r[4]), reverse=True):
82             if options['only_to'] and result[3] != options['only_to']:
83                 continue
84             submissions_by_contact.setdefault(result[3], []).append({
85                 'first_name': result[0].split()[0],
86                 'last_name': result[0].split()[1],
87                 'final_result': result[4],
88             })
89
90         for email, submissions in submissions_by_contact.items():
91             # contact = Contact.objects.get(id=contact_id)
92             message = render_to_string('wtem/results_teacher.txt', dict(submissions = submissions))
93             self.send_message(message, subject, email)
94
95         self.sum_up()
96
97     def sum_up(self):        
98         self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed))
99
100     def send_message(self, message, subject, email):
101         self.stdout.write('>>> sending results to %s' % email)
102         try:
103             send_mail(
104                 subject = subject,
105                 body = message,
106                 to = [email]
107             )
108         except BaseException, e:
109             self.failed += 1
110             self.stdout.write('failed sending to: ' + email + ': ' + str(e))
111         else:
112             self.sent += 1
113             self.stdout.write('message sent to: ' + email)
114
115