1 # -*- coding: utf-8 -*-
2 from optparse import make_option
4 from django.core.management.base import BaseCommand
5 from django.db.models import Count
6 from django.contrib.auth.models import User
8 from wtem.models import Submission, Attachment
11 class Command(BaseCommand):
13 option_list = BaseCommand.option_list + (
15 '--with-attachments-only',
17 dest='attachments_only',
19 help='Take into account only submissions with attachments'),
21 '--without-attachments-only',
23 dest='no_attachments_only',
25 help='Take into account only submissions without attachments'),
28 def handle(self, *args, **options):
30 limit_from = int(args[0])
31 limit_to = int(args[1])
32 examiner_names = args[2:]
34 users = User.objects.filter(username__in=examiner_names)
35 all_submissions = Submission.objects.annotate(examiners_count=Count('examiners'))
37 submissions = all_submissions.exclude(answers=None)
39 with_attachment_ids = Attachment.objects.values_list('submission_id', flat=True).all()
40 if options['attachments_only']:
41 submissions = submissions.filter(id__in=with_attachment_ids)
42 if options['no_attachments_only']:
43 submissions = submissions.exclude(id__in=with_attachment_ids)
45 for submission in submissions.order_by('id')[limit_from:limit_to]:
46 submission.examiners.add(*users)
48 self.stdout.write('added to %s:%s' % (submission.id, submission.email))
50 count_by_examiners = dict()
51 for submission in all_submissions.all():
52 count_by_examiners[submission.examiners_count] = \
53 count_by_examiners.get(submission.examiners_count, 0) + 1
54 self.stdout.write('%s' % count_by_examiners)