diff --git a/user/schema/mutation.py b/user/schema/mutation.py index f9447aafbf48847c2a8bf41a6f33606bb4aa9352..5dc4de497272c6cd4b02884bf9f12bf38a187ebe 100644 --- a/user/schema/mutation.py +++ b/user/schema/mutation.py @@ -44,19 +44,28 @@ class AssignUsersToTeamMutation(graphene.Mutation): def mutate( cls, root, info, user_ids: List[str], team_id: str ) -> graphene.Mutation: - # TODO: allow assigning instructors and when assigning an instructor, - # create entry in InstructorOfExercise instead of UserInTeam team = get_model(Team, id=team_id) exercise_access(info.context, team.exercise_id) users = validate_team_assigning(user_ids, team) - UserInTeam.objects.bulk_create( - UserInTeam(user=user, team=team) for user in users - ) + trainees: List[UserInTeam] = [] + instructors: List[InstructorOfExercise] = [] + for user in users: + if user.group >= User.AuthGroup.INSTRUCTOR: + instructors.append( + InstructorOfExercise( + user=user, exercise_id=team.exercise_id + ) + ) + else: + trainees.append(UserInTeam(user=user, team=team)) + + UserInTeam.objects.bulk_create(trainees) + InstructorOfExercise.objects.bulk_create(instructors) logger.info( log_user_msg(info.context, info.context.user) - + f"assigned trainees: {users} to team id: {team_id}" + + f"assigned trainees: {users} to team id: ({team_id})" ) return AssignUsersToTeamMutation(operation_done=True) diff --git a/user/schema/validators.py b/user/schema/validators.py index b66d92adbf6495ecc1ebf912f8bdeb463000b452..6dda130e8661a1f8c8547c641f74a4c7c1c47641 100644 --- a/user/schema/validators.py +++ b/user/schema/validators.py @@ -1,13 +1,13 @@ from typing import List, Type, TypeVar, Union -from django.db.models import Model from django.conf import settings -from rest_framework.exceptions import PermissionDenied +from django.db.models import Model from django.db.models import QuerySet +from rest_framework.exceptions import PermissionDenied -from user.models import UserInTeam, User, InstructorOfExercise, DefinitionAccess from exercise.models import Team, Exercise, Definition from user.graphql_inputs import ChangeUserInput +from user.models import UserInTeam, User, InstructorOfExercise, DefinitionAccess ModelType = TypeVar("ModelType", bound=Model) @@ -116,6 +116,9 @@ def validate_team_assigning(user_ids: List[str], team: Team) -> List[User]: assigned_to_team = UserInTeam.objects.filter( user__in=users, team__in=teams_of_exercise ) + assigned_to_exercise = InstructorOfExercise.objects.filter( + user__in=users, exercise_id=team.exercise_id + ) if assigned_to_team.exists(): assigned_users = list( assigned_to_team.values_list("user__username", flat=True) @@ -125,7 +128,15 @@ def validate_team_assigning(user_ids: List[str], team: Team) -> List[User]: f"to a team of the same exercise" ) - _check_group(users, [User.AuthGroup.TRAINEE]) + if assigned_to_exercise.exists(): + assigned_users = list( + assigned_to_exercise.values_list("user__username", flat=True) + ) + raise ValueError( + f"Users {assigned_users} already assigned " + f"to the exercise as instructors" + ) + return users