1 # -*- coding: utf-8 -*-
4 from optparse import make_option
6 from django.core.management.base import BaseCommand, CommandError
7 from django.conf import settings
8 from wtem.management.commands import send_mail
9 from django.template.loader import render_to_string
11 from contact.models import Contact
14 class Command(BaseCommand):
15 def handle(self, *args, **options):
19 query = Contact.objects.filter(form_tag = 'wtem').order_by('contact').distinct('contact')
20 template_name = args[0]
21 message = render_to_string('wtem/' + template_name + '.txt')
22 subject = render_to_string('wtem/' + template_name + '_subject.txt')
24 answer = raw_input('Send the following to %d teachers with subject "%s"\n\n %s\n\n?' % \
25 (query.count(), subject.encode('utf8'), message.encode('utf8')))
30 self.send_message(message, subject, contact.contact)
31 except Exception as e:
33 self.stdout.write('failed sending to: ' + contact.contact + ' - ' + str(e))
36 self.stdout.write('message sent to: ' + contact.contact)
38 self.stdout.write('sent: %s, failed: %s' % (sent, failed))
40 def send_message(self, message, subject, email):
41 self.stdout.write('>>> sending to %s' % email)