def create(self, request): """ Create a new workgroup and its cohort. """ response = super(WorkgroupsViewSet, self).create(request) if response.status_code == status.HTTP_201_CREATED: # create the workgroup cohort workgroup = self.object course_descriptor, course_key, course_content = _get_course(self.request, self.request.user, workgroup.project.course_id) # pylint: disable=W0612 add_cohort(course_key, workgroup.cohort_name, CourseUserGroup.WORKGROUP) return response
def test_add_cohort(self): """ Make sure cohorts.add_cohort() properly adds a cohort to a course and handles errors. """ course = modulestore().get_course(self.toy_course_key) added_cohort = cohorts.add_cohort(course.id, "My Cohort") self.assertEqual(added_cohort.name, "My Cohort") self.assertRaises(ValueError, lambda: cohorts.add_cohort(course.id, "My Cohort")) self.assertRaises( ValueError, lambda: cohorts.add_cohort( SlashSeparatedCourseKey("course", "does_not", "exist"), "My Cohort"))
def test_add_cohort(self): """ Make sure cohorts.add_cohort() properly adds a cohort to a course and handles errors. """ course = modulestore().get_course(self.toy_course_key) added_cohort = cohorts.add_cohort(course.id, "My Cohort") self.assertEqual(added_cohort.name, "My Cohort") self.assertRaises( ValueError, lambda: cohorts.add_cohort(course.id, "My Cohort") ) self.assertRaises( ValueError, lambda: cohorts.add_cohort(SlashSeparatedCourseKey("course", "does_not", "exist"), "My Cohort") )
def test_add_cohort(self, mock_tracker): """ Make sure cohorts.add_cohort() properly adds a cohort to a course and handles errors. """ course = modulestore().get_course(self.toy_course_key) added_cohort = cohorts.add_cohort(course.id, "My Cohort") mock_tracker.emit.assert_any_call("edx.cohort.creation_requested", { "cohort_name": added_cohort.name, "cohort_id": added_cohort.id }) self.assertEqual(added_cohort.name, "My Cohort") self.assertRaises(ValueError, lambda: cohorts.add_cohort(course.id, "My Cohort")) self.assertRaises( ValueError, lambda: cohorts.add_cohort( SlashSeparatedCourseKey("course", "does_not", "exist"), "My Cohort"))
def test_add_cohort(self, mock_tracker): """ Make sure cohorts.add_cohort() properly adds a cohort to a course and handles errors. """ course = modulestore().get_course(self.toy_course_key) added_cohort = cohorts.add_cohort(course.id, "My Cohort") mock_tracker.emit.assert_any_call( "edx.cohort.creation_requested", {"cohort_name": added_cohort.name, "cohort_id": added_cohort.id} ) self.assertEqual(added_cohort.name, "My Cohort") self.assertRaises( ValueError, lambda: cohorts.add_cohort(course.id, "My Cohort") ) self.assertRaises( ValueError, lambda: cohorts.add_cohort(SlashSeparatedCourseKey("course", "does_not", "exist"), "My Cohort") )
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)