Exemple #1
0
    def aggregate_batches(cls, queryset) -> dict:
        queryset = Manager_Batches.annotate_assignments(queryset)

        return queryset.annotate(
            costs_approved=ExpressionWrapper(F('count_assignments_approved') *
                                             F('settings_batch__reward'),
                                             output_field=IntegerField()),
            costs_rejected=ExpressionWrapper(F('count_assignments_rejected') *
                                             F('settings_batch__reward'),
                                             output_field=IntegerField()),
            costs_submitted=ExpressionWrapper(
                F('count_assignments_submitted') * F('settings_batch__reward'),
                output_field=IntegerField()),
            costs_dead=ExpressionWrapper(F('count_assignments_dead') *
                                         F('settings_batch__reward'),
                                         output_field=IntegerField()),
            costs_pending=ExpressionWrapper(F('count_assignments_pending') *
                                            F('settings_batch__reward'),
                                            output_field=IntegerField()),
        ).aggregate(
            sum_costs_approved=Coalesce(Sum('costs_approved'), 0),
            sum_costs_rejected=Coalesce(Sum('costs_rejected'), 0),
            sum_costs_submitted=Coalesce(Sum('costs_submitted'), 0),
            sum_costs_dead=Coalesce(Sum('costs_dead'), 0),
            sum_costs_pending=Coalesce(Sum('costs_pending'), 0),
        )
Exemple #2
0
    def test_annotate_assignments(self):
        queryset = Batch.objects.all()
        queryset = Manager_Batches.annotate_assignments(queryset)
        batch = queryset.filter(name='batch1').get()

        self.assertEqual(batch.count_hits, 10)

        self.assertEqual(batch.count_assignments_total, 100)
        self.assertEqual(batch.count_assignments_approved, 18)
        self.assertEqual(batch.count_assignments_rejected, 14)
        self.assertEqual(batch.count_assignments_submitted, 13)
        self.assertEqual(batch.count_assignments_dead, 45)

        self.assertEqual(batch.count_assignments_living_total, 50)
        self.assertEqual(batch.count_assignments_living_available, 10)

        self.assertEqual(batch.count_assignments_pending, 40)
    def handle(self, *args, **options):
        queryset = Batch.objects.filter(use_sandbox=False)
        queryset = Manager_Batches.annotate_assignments(queryset)
        queryset = queryset.filter(count_assignments_living_total__gt=0)

        count_batches = queryset.count()
        projects = queryset.values('project__name').annotate(
            Sum('count_assignments_living_total'))

        if count_batches > 0:
            self.stdout.write(
                self.style.SUCCESS(
                    '{} living batches in the projects {}'.format(
                        queryset.count(), ', '.join([
                            project['project__name'] for project in projects
                        ]))))
        else:
            self.stdout.write(self.style.SUCCESS('No living batches'))