1 # -*- coding: utf-8 -*-
3 from optparse import make_option
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
11 from contact.models import Contact
12 from wtem.models import Submission
15 def get_submissions():
16 return Submission.objects.exclude(answers = None).all()
20 class Command(BaseCommand):
22 option_list = BaseCommand.option_list + (
23 make_option('--to-teachers',
27 help='Send emails to teachers'),
28 make_option('--to-students',
32 help='Send emails to students'),
35 def handle(self, *args, **options):
36 translation.activate('pl')
37 for target in ['to_teachers', 'to_students']:
41 getattr(self, 'handle_' + target)(*args, **options)
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'
47 for submission in get_submissions():
48 final_result = submission.final_result
49 if final_result < minimum:
50 template = 'results_student_failed.txt'
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)
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'
63 submissions_by_contact = dict()
65 for submission in get_submissions():
66 submissions_by_contact.setdefault(submission.contact.id, []).append(submission)
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)
76 self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed))
78 def send_message(self, message, subject, email):
79 self.stdout.write('>>> sending results to %s' % email)
88 self.stdout.write('failed sending to: ' + email + ': ' + str(e))
91 self.stdout.write('message sent to: ' + email)