refactor test form
[edumed.git] / wtem / management / commands / wtem_send_keys.py
index 82e7469..96511fb 100644 (file)
@@ -1,7 +1,11 @@
+# -*- coding: utf-8 -*-
+
 from optparse import make_option
 
 from optparse import make_option
 
-from django.core.management.base import BaseCommand, CommandError
+from django.core.management.base import BaseCommand
 from django.conf import settings
 from django.conf import settings
+from wtem.management.commands 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
 
@@ -11,12 +15,14 @@ class Command(BaseCommand):
     args = '<email_address1>, <email_address2>, ...'
 
     option_list = BaseCommand.option_list + (
     args = '<email_address1>, <email_address2>, ...'
 
     option_list = BaseCommand.option_list + (
-        make_option('--all',
+        make_option(
+            '--all',
             action='store_true',
             dest='all',
             default=False,
             help='Use all available submissions'),
             action='store_true',
             dest='all',
             default=False,
             help='Use all available submissions'),
-        make_option('--force',
+        make_option(
+            '--force',
             action='store_true',
             dest='force',
             default=False,
             action='store_true',
             dest='force',
             default=False,
@@ -33,20 +39,20 @@ class Command(BaseCommand):
         skipped = 0
         failed = 0
 
         skipped = 0
         failed = 0
 
-        query = Submission.objects.all()
+        submissions = Submission.objects.all()
         if not options['force']:
         if not options['force']:
-            query = query.filter(key_sent = False)
+            submissions = submissions.filter(key_sent=False)
         if len(args):
         if len(args):
-            query = query.filter(email__in = args)
+            submissions = submissions.filter(email__in=args)
 
 
-        for submission in query.all():
+        for submission in submissions:
             assert len(submission.key) == 30 or (settings.DEBUG and submission.key == DEBUG_KEY)
 
             try:
                 self.send_key(submission)
             assert len(submission.key) == 30 or (settings.DEBUG and submission.key == DEBUG_KEY)
 
             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 +62,8 @@ 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(
+            subject="WLEM - Twój link do egzaminu",
+            body=render_to_string('wtem/email_key.txt', dict(submission=submission)),
+            to=[submission.email])