Sending emails to teachers
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 12 Nov 2013 11:12:14 +0000 (12:12 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 15 Jan 2014 10:18:58 +0000 (11:18 +0100)
wtem/management/commands/wtem_email_teachers.py [new file with mode: 0644]
wtem/templates/wtem/email_teacher_before_subject.txt [new file with mode: 0644]

diff --git a/wtem/management/commands/wtem_email_teachers.py b/wtem/management/commands/wtem_email_teachers.py
new file mode 100644 (file)
index 0000000..3aba80d
--- /dev/null
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+
+import sys
+from optparse import make_option
+
+from django.core.management.base import BaseCommand, CommandError
+from django.conf import settings
+from django.core.mail import send_mail
+from django.template.loader import render_to_string
+
+from contact.models import Contact
+
+
+class Command(BaseCommand):
+    def handle(self, *args, **options):
+        sent = 0
+        failed = 0
+
+        query = Contact.objects.filter(form_tag = 'wtem').order_by('contact').distinct('contact')
+        template_name = args[0]
+        message = render_to_string('wtem/' + template_name + '.txt')
+        subject = render_to_string('wtem/' + template_name + '_subject.txt')
+        
+        answer = raw_input('Send the following to %d teachers with subject "%s"\n\n %s\n\n?' % \
+            (query.count(), subject.encode('utf8'), message.encode('utf8')))
+
+        if answer == 'yes':
+            for contact in query:
+                try:
+                    self.send_message(message, subject, contact.contact)
+                except Exception as e:
+                    failed += 1
+                    self.stdout.write('failed sending to: ' + contact.contact + ' - ' + str(e))
+                else:
+                    sent += 1
+                    self.stdout.write('message sent to: ' + contact.contact)
+
+        self.stdout.write('sent: %s, failed: %s' % (sent, failed))
+
+    def send_message(self, message, subject, email):
+        self.stdout.write('>>> sending to %s' % email)
+        send_mail(
+            subject,
+            message,
+            getattr(settings, 'WTEM_CONTACT_EMAIL', 'no-reply@edukacjamedialna.edu.pl'),
+            [email],
+            fail_silently=False
+        )
+
diff --git a/wtem/templates/wtem/email_teacher_before_subject.txt b/wtem/templates/wtem/email_teacher_before_subject.txt
new file mode 100644 (file)
index 0000000..5649e56
--- /dev/null
@@ -0,0 +1 @@
+Pierwszy etap Wielkiego Turnieju Edukacji Medialnej już wkrótce
\ No newline at end of file