Sending emails with contest keys
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 5 Nov 2013 13:23:53 +0000 (14:23 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 15 Jan 2014 10:18:54 +0000 (11:18 +0100)
wtem/management/commands/wtem_send_keys.py
wtem/templates/wtem/email_key.txt [new file with mode: 0644]

index 82e7469..cff7bf3 100644 (file)
@@ -1,7 +1,12 @@
+# -*- coding: utf-8 -*-
+
+import sys
 from optparse import make_option
 
 from django.core.management.base import BaseCommand, CommandError
 from django.conf import settings
 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 wtem.models import Submission, DEBUG_KEY
 
 
 from wtem.models import Submission, DEBUG_KEY
 
@@ -44,9 +49,9 @@ class Command(BaseCommand):
 
             try:
                 self.send_key(submission)
 
             try:
                 self.send_key(submission)
-            except:
+            except Exception as e:
                 failed += 1
                 failed += 1
-                self.stdout.write('failed sending to: ' + submission.email)
+                self.stdout.write('failed sending to: ' + submission.email + ' - ' + str(e))
             else:
                 submission.key_sent = True
                 submission.save()
             else:
                 submission.key_sent = True
                 submission.save()
@@ -56,4 +61,11 @@ class Command(BaseCommand):
         self.stdout.write('sent: ' + str(sent))
 
     def send_key(self, submission):
         self.stdout.write('sent: ' + str(sent))
 
     def send_key(self, submission):
-        self.stdout.write('>>> sending to ' + submission.email)
\ No newline at end of file
+        self.stdout.write('>>> sending to ' + submission.email)
+        send_mail(
+            "WTEM - Twój link do zadań",
+            render_to_string('wtem/email_key.txt', dict(submission = submission)),
+            getattr(settings, 'WTEM_CONTACT_EMAIL', 'no-reply@edukacjamedialna.edu.pl'),
+            [submission.email],
+            fail_silently=False
+            )
\ No newline at end of file
diff --git a/wtem/templates/wtem/email_key.txt b/wtem/templates/wtem/email_key.txt
new file mode 100644 (file)
index 0000000..865b0be
--- /dev/null
@@ -0,0 +1,3 @@
+Poniżej znajduje się wygenerowany specjalnie dla Ciebie link, pod którym będziesz mógł/mogła rozwiązać zadania pierwszego etapu Wielkiego Turnieju Edukacji Medialnej:
+
+http://edukacjamedialna.edu.pl{% url 'wtem_form' key=submission.key %}
\ No newline at end of file