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').exclude(contact=None).order_by('contact')\
17 template_name = args[0]
20 contacts = contacts.filter(contact__in=emails)
21 message = render_to_string('wtem/' + template_name + '.txt')
22 subject = render_to_string('wtem/' + template_name + '_subject.txt')
25 'Send the following to %d teachers with subject "%s"\n\n%s\n\n?' %
26 (contacts.count(), subject.encode('utf8'), message.encode('utf8')))
29 for contact in contacts:
31 self.send_message(message, subject, contact.contact)
32 except Exception as e:
34 self.stdout.write('failed sending to: ' + contact.contact + ' - ' + str(e))
37 self.stdout.write('message sent to: ' + contact.contact)
39 self.stdout.write('sent: %s, failed: %s' % (sent, failed))
41 def send_message(self, message, subject, email):
42 self.stdout.write('>>> sending to %s' % email)
43 send_mail(subject=subject, body=message, to=[email])