All-or-nothing answer checking mode for questions of type "wybor"
[edumed.git] / wtem / management / commands / wtem_assign_submissions.py
1 from django.core.management.base import BaseCommand, CommandError
2 from django.db.models import Count
3 from django.contrib.auth.models import User
4
5 from contact.models import Contact
6 from wtem.models import Submission
7
8
9 class Command(BaseCommand):
10
11     def handle(self, *args, **options):
12         how_many = int(args[0])
13         examiner_names = args[1:]
14
15         users = User.objects.filter(username__in = examiner_names)
16         submissions_query = Submission.objects.annotate(examiners_count = Count('examiners'))
17
18         submissions = submissions_query \
19             .filter(examiners_count__lt=2)[0:how_many]
20
21         for submission in submissions:
22             submission.examiners.add(*users)
23             submission.save()
24             self.stdout.write('added to %s:%s' % (submission.id, submission.email))
25
26         count_by_examiners = dict()
27         for submission in submissions_query.all():
28             count_by_examiners[submission.examiners_count] = \
29                 count_by_examiners.get(submission.examiners_count, 0) + 1
30         self.stdout.write('%s' % count_by_examiners)