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.

31 lines
1.2 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. unassigned_examples = Example.objects.filter(project=project, assignments__isnull=True)
  14. dataset_size = unassigned_examples.count()
  15. strategy = create_assignment_strategy(strategy_name, dataset_size, weights)
  16. assignments = strategy.assign()
  17. assignments = [
  18. Assignment(
  19. project=project,
  20. example=unassigned_examples[assignment.example],
  21. assignee=members[assignment.user].user,
  22. )
  23. for assignment in assignments
  24. ]
  25. Assignment.objects.bulk_create(assignments)