new question
[edumed.git] / wtem / management / commands / wtem_generate_keys.py
1 from django.core.management.base import BaseCommand, CommandError
2
3 from contact.models import Contact
4 from wtem.models import Submission
5
6
7 class Command(BaseCommand):
8     help = 'Sends personalized links to WTEM contestants'
9
10     def handle(self, *args, **options):
11         new = 0
12         skipped = 0
13
14         for wtem_contact in Contact.objects.filter(form_tag = 'wtem'):
15             for student in wtem_contact.body['student']:
16                 if not Submission.objects.filter(email = student['email']).exists():
17                     args = dict()
18                     for attr in ['first_name', 'last_name', 'email']:
19                         args[attr] = student[attr]
20                     args['contact'] = wtem_contact
21                     Submission.create(**args)
22                     new += 1
23                 else:
24                     self.stdout.write('skipping ' + student['email'] + ': already exists.')
25                     skipped += 1
26
27         self.stdout.write('New: ' + str(new) + ', skipped: ' + str(skipped))