1 # -*- coding: utf-8 -*-
 
   3 from optparse import make_option
 
   5 from django.core.management.base import BaseCommand
 
   6 from wtem.management.commands import send_mail
 
   7 from django.utils import translation
 
   8 from django.template.loader import render_to_string
 
  10 from contact.models import Contact
 
  11 from wtem.models import Submission
 
  14 def get_submissions():
 
  15     return sorted(Submission.objects.exclude(answers=None).all(), key=lambda s: -s.final_result)
 
  20 class Command(BaseCommand):
 
  22     option_list = BaseCommand.option_list + (
 
  28             help='Send emails to teachers'),
 
  34             help='Send emails to students'),
 
  40             help='Send email only to one address'),
 
  44         super(Command, self).__init__()
 
  45         self.sent = self.failed = None
 
  47     def handle(self, *args, **options):
 
  48         translation.activate('pl')
 
  49         for target in ['to_teachers', 'to_students']:
 
  53                 getattr(self, 'handle_' + target)(*args, **options)
 
  55     def handle_to_students(self, *args, **options):
 
  56         self.stdout.write('>>> Sending results to students')
 
  57         subject = 'Wyniki I etapu Olimpiady Cyfrowej'
 
  59         for submission in get_submissions():
 
  60             if options['only_to'] and submission.email != options['only_to']:
 
  62             final_result = submission.final_result
 
  63             if final_result < minimum:
 
  64                 template = 'results_student_failed.txt'
 
  66                 template = 'results_student_passed.txt'
 
  67             message = render_to_string('wtem/' + template, dict(final_result=submission.final_result))
 
  68             self.send_message(message, subject, submission.email)
 
  72     def handle_to_teachers(self, *args, **options):
 
  73         self.stdout.write('>>> Sending results to teachers')
 
  74         subject = 'Wyniki I etapu Olimpiady Cyfrowej'
 
  76         submissions_by_contact = dict()
 
  78         for submission in get_submissions():
 
  79             if options['only_to'] and submission.contact.contact != options['only_to']:
 
  81             submissions_by_contact.setdefault(submission.contact.id, []).append(submission)
 
  83         for contact_id, submissions in submissions_by_contact.items():
 
  84             contact = Contact.objects.get(id=contact_id)
 
  85             message = render_to_string('wtem/results_teacher.txt', dict(submissions=submissions))
 
  86             self.send_message(message, subject, contact.contact)
 
  91         self.stdout.write('sent: %s, failed: %s' % (self.sent, self.failed))
 
  93     def send_message(self, message, subject, email):
 
  94         self.stdout.write('>>> sending results to %s' % email)
 
  96             send_mail(subject=subject, body=message, to=[email])
 
  97         except BaseException, e:
 
  99             self.stdout.write('failed sending to: ' + email + ': ' + str(e))
 
 102             self.stdout.write('message sent to: ' + email)