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 contacts = Contact.objects.filter(form_tag='olimpiada').order_by('contact').distinct('contact')
16 template_name = args[0]
19 contacts = contacts.filter(contact__in=emails)
20 message = render_to_string('wtem/' + template_name + '.txt')
21 subject = render_to_string('wtem/' + template_name + '_subject.txt')
24 'Send the following to %d teachers with subject "%s"\n\n%s\n\n?' %
25 (contacts.count(), subject.encode('utf8'), message.encode('utf8')))
28 for contact in contacts:
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)
42 send_mail(subject=subject, body=message, to=[email])