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 wtem.management.commands import send_mail
10 class Command(BaseCommand):
11 def handle(self, *args, **options):
15 query = Contact.objects.filter(form_tag='wtem').order_by('contact').distinct('contact')
16 template_name = args[0]
17 message = render_to_string('wtem/' + template_name + '.txt')
18 subject = render_to_string('wtem/' + template_name + '_subject.txt')
21 'Send the following to %d teachers with subject "%s"\n\n %s\n\n?' %
22 (query.count(), subject.encode('utf8'), message.encode('utf8')))
27 self.send_message(message, subject, contact.contact)
28 except Exception as e:
30 self.stdout.write('failed sending to: ' + contact.contact + ' - ' + str(e))
33 self.stdout.write('message sent to: ' + contact.contact)
35 self.stdout.write('sent: %s, failed: %s' % (sent, failed))
37 def send_message(self, message, subject, email):
38 self.stdout.write('>>> sending to %s' % email)
39 send_mail(subject=subject, body=message, to=[email])