fields on answer list
[edumed.git] / wtem / management / commands / wtem_generate_keys.py
index 19b9582..48cd8b2 100644 (file)
@@ -1,30 +1,21 @@
-from django.core.management.base import BaseCommand, CommandError
+# -*- coding: utf-8 -*-
+from django.core.management.base import BaseCommand
 
-from contact.models import Contact
-from wtem.models import Submission
+from wtem.models import Submission, Confirmation
 
 
 class Command(BaseCommand):
-    
-    def handle(self, *ids, **options):
+
+    def handle(self, **options):
         new = 0
         skipped = 0
 
-        query = Contact.objects.filter(form_tag = 'wtem').order_by('-created_at')
-        if ids:
-            query = query.filter(pk__in=ids)
-
-        for wtem_contact in query:
-            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:
-                    self.stdout.write('skipping ' + student['email'] + ': already exists.')
-                    skipped += 1
+        emails = list(Submission.objects.values_list('email', flat=True))
+        for confirmation in Confirmation.objects.exclude(email__in=emails):
+            args = {}
+            for attr in ['first_name', 'last_name', 'email', 'contact']:
+                args[attr] = getattr(confirmation, attr)
+            Submission.create(**args)
+            new += 1
 
         self.stdout.write('New: ' + str(new) + ', skipped: ' + str(skipped))