def users(self, request, pk): """ Add a User to a Workgroup """ if request.method == 'GET': users = User.objects.filter(workgroups=pk) response_data = [] if users: for user in users: serializer = UserSerializer(user) response_data.append(serializer.data) # pylint: disable=E1101 return Response(response_data, status=status.HTTP_200_OK) elif request.method == 'POST': user_id = request.DATA.get('id') try: user = User.objects.get(id=user_id) except ObjectDoesNotExist: message = 'User {} does not exist'.format(user_id) return Response({"detail": message}, status.HTTP_400_BAD_REQUEST) workgroup = self.get_object() workgroup.users.add(user) workgroup.save() # add user to the workgroup cohort, create it if it doesn't exist (for cases where there is a legacy # workgroup) course_descriptor, course_key, course_content = _get_course(self.request, user, workgroup.project.course_id) # pylint: disable=W0612 try: cohort = get_cohort_by_name(course_key, workgroup.cohort_name, CourseUserGroup.WORKGROUP) add_user_to_cohort(cohort, user.username) except ObjectDoesNotExist: # This use case handles cases where a workgroup might have been created before # the notion of a cohorted discussion. So we need to backfill in the data cohort = add_cohort(course_key, workgroup.cohort_name, CourseUserGroup.WORKGROUP) for workgroup_user in workgroup.users.all(): add_user_to_cohort(cohort, workgroup_user.username) return Response({}, status=status.HTTP_201_CREATED) else: user_id = request.DATA.get('id') try: user = User.objects.get(id=user_id) except ObjectDoesNotExist: message = 'User {} does not exist'.format(user_id) return Response({"detail": message}, status.HTTP_400_BAD_REQUEST) workgroup = self.get_object() course_descriptor, course_key, course_content = _get_course(self.request, user, workgroup.project.course_id) # pylint: disable=W0612 cohort = get_cohort_by_name(course_key, workgroup.cohort_name, CourseUserGroup.WORKGROUP) workgroup.users.remove(user) remove_user_from_cohort(cohort, user.username, CourseUserGroup.WORKGROUP) return Response({}, status=status.HTTP_204_NO_CONTENT)
def test_workgroups_users_post_with_cohort_backfill(self): """ This test asserts a case where a workgroup was created before the existence of a cohorted discussion """ data = { 'name': self.test_workgroup_name, 'project': self.test_project.id } response = self.do_post(self.test_workgroups_uri, data) self.assertEqual(response.status_code, 201) test_uri = '{}{}/'.format(self.test_workgroups_uri, str(response.data['id'])) users_uri = '{}users/'.format(test_uri) data = {"id": self.test_user.id} response = self.do_post(users_uri, data) self.assertEqual(response.status_code, 201) response = self.do_get(test_uri) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['users'][0]['id'], self.test_user.id) cohort_name = Workgroup.cohort_name_for_workgroup( self.test_project.id, response.data['id'], self.test_workgroup_name ) # now let's remove existing cohort users cohort = get_cohort_by_name(self.test_course.id, cohort_name, CourseUserGroup.WORKGROUP) self.assertTrue(is_user_in_cohort(cohort, self.test_user.id, CourseUserGroup.WORKGROUP)) remove_user_from_cohort(cohort, self.test_user.username, CourseUserGroup.WORKGROUP) self.assertFalse(is_user_in_cohort(cohort, self.test_user.id, CourseUserGroup.WORKGROUP)) # delete cohort delete_empty_cohort(self.test_course.id, cohort_name, CourseUserGroup.WORKGROUP) self.assertEqual(0, len(get_course_cohort_names(self.test_course.id, CourseUserGroup.WORKGROUP))) # add a 2nd user and make sure a discussion cohort was created and users were backfilled test_uri = '{}{}/'.format(self.test_workgroups_uri, str(response.data['id'])) users_uri = '{}users/'.format(test_uri) data = {"id": self.test_user2.id} response = self.do_post(users_uri, data) self.assertEqual(response.status_code, 201) # now inspect cohort and assert that things are as we anticipate (i.e. both users are in there) cohort = get_cohort_by_name(self.test_course.id, cohort_name, CourseUserGroup.WORKGROUP) self.assertIsNotNone(cohort) self.assertTrue(is_user_in_cohort(cohort, self.test_user.id, CourseUserGroup.WORKGROUP)) self.assertTrue(is_user_in_cohort(cohort, self.test_user2.id, CourseUserGroup.WORKGROUP))