You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.5 KiB

  1. from django.db.models import Count, Manager
  2. class ExampleManager(Manager):
  3. def bulk_create(self, objs, batch_size=None, ignore_conflicts=False):
  4. super().bulk_create(objs, batch_size=batch_size, ignore_conflicts=ignore_conflicts)
  5. uuids = [data.uuid for data in objs]
  6. examples = self.in_bulk(uuids, field_name='uuid')
  7. return [examples[uid] for uid in uuids]
  8. class ExampleStateManager(Manager):
  9. def count_done(self, examples, user=None):
  10. if user:
  11. queryset = self.filter(example_id__in=examples, confirmed_by=user)
  12. else:
  13. queryset = self.filter(example_id__in=examples)
  14. return queryset.distinct().values('example').count()
  15. def measure_member_progress(self, examples, members):
  16. done_count = self.filter(example_id__in=examples)\
  17. .values('confirmed_by__username')\
  18. .annotate(total=Count('confirmed_by'))
  19. response = {
  20. 'total': examples.count(),
  21. 'progress': [
  22. {
  23. 'user': obj['confirmed_by__username'],
  24. 'done': obj['total']
  25. } for obj in done_count
  26. ]
  27. }
  28. members_with_progress = {o['confirmed_by__username'] for o in done_count}
  29. for member in members:
  30. if member.username not in members_with_progress:
  31. response['progress'].append({
  32. 'user': member.username,
  33. 'done': 0
  34. })
  35. return response