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.

33 lines
1.3 KiB

  1. from typing import List
  2. from django.shortcuts import get_object_or_404
  3. from examples.assignment.strategies import StrategyName, create_assignment_strategy
  4. from examples.models import Assignment, Example
  5. from projects.models import Member, Project
  6. def bulk_assign(project_id: int, strategy_name: StrategyName, member_ids: List[int], weights: List[int]) -> None:
  7. project = get_object_or_404(Project, pk=project_id)
  8. members = Member.objects.filter(project=project, pk__in=member_ids)
  9. if len(members) != len(member_ids):
  10. raise ValueError("Invalid member ids")
  11. # Sort members by member_ids
  12. members = sorted(members, key=lambda m: member_ids.index(m.id))
  13. index_to_user = {i: member.user for i, member in enumerate(members)}
  14. unassigned_examples = Example.objects.filter(project=project, assignments__isnull=True)
  15. index_to_example = {i: example for i, example in enumerate(unassigned_examples)}
  16. dataset_size = unassigned_examples.count()
  17. strategy = create_assignment_strategy(strategy_name, dataset_size, weights)
  18. assignments = strategy.assign()
  19. assignments = [
  20. Assignment(
  21. project=project,
  22. example=index_to_example[assignment.example],
  23. assignee=index_to_user[assignment.user],
  24. )
  25. for assignment in assignments
  26. ]
  27. Assignment.objects.bulk_create(assignments)