1 # -*- coding: utf-8 -*-
3 from django.core.management.base import BaseCommand
4 from django.template.loader import render_to_string
6 from contact.models import Contact
7 from stage2.models import Participant
8 from wtem.management.commands import send_mail
11 class Command(BaseCommand):
12 def handle(self, *args, **options):
16 query = Participant.objects.order_by('contact__contact').distinct('contact__contact')\
17 .values_list('contact__contact', flat=True)
18 template_name = args[0]
19 message = render_to_string('wtem/' + template_name + '.txt')
20 subject = render_to_string('wtem/' + template_name + '_subject.txt')
23 'Send the following to %d teachers with subject "%s"\n\n %s\n\n?' %
24 (query.count(), subject.encode('utf8'), message.encode('utf8')))
29 self.send_message(message, subject, contact)
30 except Exception as e:
32 self.stdout.write('failed sending to: ' + contact + ' - ' + str(e))
35 self.stdout.write('message sent to: ' + contact)
37 self.stdout.write('sent: %s, failed: %s' % (sent, failed))
39 def send_message(self, message, subject, email):
40 self.stdout.write('>>> sending to %s' % email)
41 send_mail(subject=subject, body=message, to=[email])