From: Aleksander Ɓukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Date: Mon, 4 Nov 2013 15:20:51 +0000 (+0100)
Subject: wtem: generating submissions from contact forms
X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/b154d06ff9d6b7fa711607bc02d03fb9db5c6d33

wtem: generating submissions from contact forms
---

diff --git a/wtem/management/__init__.py b/wtem/management/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/wtem/management/commands/__init__.py b/wtem/management/commands/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/wtem/management/commands/wtem_generate_keys.py b/wtem/management/commands/wtem_generate_keys.py
new file mode 100644
index 0000000..239fe6b
--- /dev/null
+++ b/wtem/management/commands/wtem_generate_keys.py
@@ -0,0 +1,26 @@
+from django.core.management.base import BaseCommand, CommandError
+
+from contact.models import Contact
+from wtem.models import Submission
+
+
+class Command(BaseCommand):
+    help = 'Sends personalized links to WTEM contestants'
+
+    def handle(self, *args, **options):
+        new = 0
+        skipped = 0
+
+        for wtem_contact in Contact.objects.filter(form_tag = 'wtem'):
+            for student in wtem_contact.body['student']:
+                if not Submission.objects.filter(email = student['email']).exists():
+                    args = dict()
+                    for attr in ['first_name', 'last_name', 'email']:
+                        args[attr] = student[attr]
+                    args['contact'] = wtem_contact
+                    Submission.create(**args)
+                    new += 1
+                else:
+                    skipped += 1
+
+        self.stdout.write('New: ' + str(new) + ', skipped: ' + str(skipped))
diff --git a/wtem/models.py b/wtem/models.py
index b6858cc..4a42c30 100644
--- a/wtem/models.py
+++ b/wtem/models.py
@@ -22,28 +22,17 @@ class Submission(models.Model):
         return key
 
     @classmethod
-    def create(cls, first_name, last_name, email, contact = None, key = None):
-        submissions = []
-
-        if contact:
-            students = contact['students']
-        else:
-            students = [dict(first_name = first_name, last_name = last_name, email = email)]
-        
-        if key is None:
-            key = Submission.generate_key()
-
-        for student in students:
-            submission = cls(
-                contact = contact,
-                key = key,
-                first_name = student['first_name'],
-                last_name = student['last_name'],
-                email = student['email']
-            )
-            submission.save()
-            submissions.append(submission)
-        return submissions
+    def create(cls, first_name, last_name, email, key = None, contact = None):
+        submission = cls(
+            contact = contact,
+            key = key if key else Submission.generate_key(),
+            first_name = first_name,
+            last_name = last_name,
+            email = email
+        )
+
+        submission.save()
+        return submission
 
 
 class Attachment(models.Model):
diff --git a/wtem/views.py b/wtem/views.py
index 127710a..d329f12 100644
--- a/wtem/views.py
+++ b/wtem/views.py
@@ -17,7 +17,7 @@ def form(request, key):
         submission = Submission.objects.get(key = key)
     except Submission.DoesNotExist:
         if settings.DEBUG and key == '12345':
-            submission = Submission.create(first_name = 'Debug', last_name = 'Debug', email = 'debug@debug.com', key = '12345')[0]
+            submission = Submission.create(first_name = 'Debug', last_name = 'Debug', email = 'debug@debug.com', key = '12345')
         else:
             raise Http404