wtem: generating submissions from contact forms
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 4 Nov 2013 15:20:51 +0000 (16:20 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 15 Jan 2014 10:18:54 +0000 (11:18 +0100)
wtem/management/__init__.py [new file with mode: 0644]
wtem/management/commands/__init__.py [new file with mode: 0644]
wtem/management/commands/wtem_generate_keys.py [new file with mode: 0644]
wtem/models.py
wtem/views.py

diff --git a/wtem/management/__init__.py b/wtem/management/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/wtem/management/commands/__init__.py b/wtem/management/commands/__init__.py
new file mode 100644 (file)
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 (file)
index 0000000..239fe6b
--- /dev/null
@@ -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))
index b6858cc..4a42c30 100644 (file)
@@ -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):
index 127710a..d329f12 100644 (file)
@@ -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