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 sorted(Submission.objects.exclude(answers = None).all(), key=lambda s: -s.final_result)
 
  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'),
 
  33         make_option('--only-to',
 
  37             help='Send emails to students'),
 
  40     def handle(self, *args, **options):
 
  41         translation.activate('pl')
 
  42         for target in ['to_teachers', 'to_students']:
 
  46                 getattr(self, 'handle_' + target)(*args, **options)
 
  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'
 
  52         for submission in get_submissions():
 
  53             if options['only_to'] and submission.email != options['only_to']:
 
  55             final_result = submission.final_result
 
  56             if final_result < minimum:
 
  57                 template = 'results_student_failed.txt'
 
  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)
 
  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'
 
  70         submissions_by_contact = dict()
 
  72         for submission in get_submissions():
 
  73             if options['only_to'] and submission.contact.contact != options['only_to']:
 
  75             submissions_by_contact.setdefault(submission.contact.id, []).append(submission)
 
  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)
 
  85         self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed))
 
  87     def send_message(self, message, subject, email):
 
  88         self.stdout.write('>>> sending results to %s' % email)
 
  95         except BaseException, e:
 
  97             self.stdout.write('failed sending to: ' + email + ': ' + str(e))
 
 100             self.stdout.write('message sent to: ' + email)