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.

32 lines
1.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  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 = (
  17. self.filter(example_id__in=examples).values("confirmed_by__username").annotate(total=Count("confirmed_by"))
  18. )
  19. response = {
  20. "total": examples.count(),
  21. "progress": [{"user": obj["confirmed_by__username"], "done": obj["total"]} for obj in done_count],
  22. }
  23. members_with_progress = {o["confirmed_by__username"] for o in done_count}
  24. for member in members:
  25. if member.username not in members_with_progress:
  26. response["progress"].append({"user": member.username, "done": 0})
  27. return response