hide banner
[edumed.git] / wtem / management / commands / wtem_generate_keys.py
1 # -*- coding: utf-8 -*-
2 from django.core.management.base import BaseCommand
3
4 from contact.models import Contact
5 from wtem.models import Submission
6
7
8 class Command(BaseCommand):
9
10     def handle(self, *ids, **options):
11         new = 0
12         skipped = 0
13
14         query = Contact.objects.filter(form_tag='wtem').order_by('-created_at')
15         if ids:
16             query = query.filter(pk__in=ids)
17
18         for wtem_contact in query:
19             for student in wtem_contact.body['student']:
20                 if not Submission.objects.filter(email=student['email']).exists():
21                     args = dict()
22                     for attr in ['first_name', 'last_name', 'email']:
23                         args[attr] = student[attr]
24                     args['contact'] = wtem_contact
25                     Submission.create(**args)
26                     new += 1
27                 else:
28                     self.stdout.write('skipping ' + student['email'] + ': already exists.')
29                     skipped += 1
30
31         self.stdout.write('New: ' + str(new) + ', skipped: ' + str(skipped))