workshop banner
[edumed.git] / wtem / management / commands / wlem_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='wlem').order_by('-created_at')
15         if ids:
16             query = query.filter(pk__in=ids)
17
18         for wlem_contact in query:
19             if not Submission.objects.filter(email=wlem_contact.contact).exists():
20                 first_name, last_name = wlem_contact.body['nazwisko'].split()
21                 args = {
22                     'email': wlem_contact.contact,
23                     'first_name': first_name,
24                     'last_name': last_name,
25                 }
26                 Submission.create(**args)
27                 new += 1
28             else:
29                 self.stdout.write('skipping ' + wlem_contact.contact + ': already exists.')
30                 skipped += 1
31
32         self.stdout.write('New: ' + str(new) + ', skipped: ' + str(skipped))