示例#1
0
    def get(self, request):
        """List out all the enrollments for the current user

        Returns a JSON response with all the course enrollments for the current user.

        Args:
            request (Request): The GET request for course enrollment listings.
            user (str): Get all enrollments for the specified user's username.

        Returns:
            A JSON serialized representation of the user's course enrollments.

        """
        user = request.GET.get('user', request.user.username)
        if request.user.username != user:
            # Return a 404 instead of a 403 (Unauthorized). If one user is looking up
            # other users, do not let them deduce the existence of an enrollment.
            return Response(status=status.HTTP_404_NOT_FOUND)
        try:
            return Response(api.get_enrollments(user))
        except CourseEnrollmentError:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    "message":
                    (u"An error occurred while retrieving enrollments for user '{user}'"
                     ).format(user=user)
                })
示例#2
0
    def get(self, request):
        """List out all the enrollments for the current user

        Returns a JSON response with all the course enrollments for the current user.

        Args:
            request (Request): The GET request for course enrollment listings.
            user (str): Get all enrollments for the specified user's username.

        Returns:
            A JSON serialized representation of the user's course enrollments.

        """
        user = request.GET.get('user', request.user.username)
        if request.user.username != user:
            # Return a 404 instead of a 403 (Unauthorized). If one user is looking up
            # other users, do not let them deduce the existence of an enrollment.
            return Response(status=status.HTTP_404_NOT_FOUND)
        try:
            return Response(api.get_enrollments(user))
        except CourseEnrollmentError:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    "message": (
                        u"An error occurred while retrieving enrollments for user '{user}'"
                    ).format(user=user)
                }
            )
示例#3
0
    def post(self, request):
        """
        Unenrolls the specified user from all courses.
        """
        username = None
        user_model = get_user_model()

        try:
            # Get the username from the request and check that it exists
            username = request.data['username']
            user_model.objects.get(username=username)

            enrollments = api.get_enrollments(username)
            active_enrollments = [
                enrollment for enrollment in enrollments
                if enrollment['is_active']
            ]
            if len(active_enrollments) < 1:
                return Response(status=status.HTTP_200_OK)
            return Response(api.unenroll_user_from_all_courses(username))
        except KeyError:
            return Response(u'Username not specified.',
                            status=status.HTTP_404_NOT_FOUND)
        except user_model.DoesNotExist:
            return Response(u'The user "{}" does not exist.'.format(username),
                            status=status.HTTP_404_NOT_FOUND)
        except Exception as exc:  # pylint: disable=broad-except
            return Response(text_type(exc),
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
示例#4
0
 def post(self, request):
     """
     Unenrolls the specified user from all courses.
     """
     try:
         # Get the username from the request.
         username = request.data['username']
         # Ensure that a retirement request status row exists for this username.
         UserRetirementStatus.get_retirement_for_retirement_action(username)
         enrollments = api.get_enrollments(username)
         active_enrollments = [
             enrollment for enrollment in enrollments
             if enrollment['is_active']
         ]
         if len(active_enrollments) < 1:
             return Response(status=status.HTTP_204_NO_CONTENT)
         return Response(api.unenroll_user_from_all_courses(username))
     except KeyError:
         return Response(u'Username not specified.',
                         status=status.HTTP_404_NOT_FOUND)
     except UserRetirementStatus.DoesNotExist:
         return Response(u'No retirement request status for username.',
                         status=status.HTTP_404_NOT_FOUND)
     except Exception as exc:  # pylint: disable=broad-except
         return Response(text_type(exc),
                         status=status.HTTP_500_INTERNAL_SERVER_ERROR)
示例#5
0
    def get(self, request):
        """Gets a list of all course enrollments for a user.

        Returns a list for the currently logged in user, or for the user named by the 'user' GET
        parameter.  If the username does not match the currently logged in user, only courses the
        requesting user has staff permissions for are listed.

        Only staff or instructor permissions on individual courses are taken into account when
        deciding whether the requesting user is permitted to see a particular enrollment, i.e.
        organizational staff access doesn't grant permission to see the enrollments in all courses
        of the organization.  This may change in the future.

        However, users with global staff access can see all enrollments of all students.
        """
        username = request.GET.get('user', request.user.username)
        try:
            enrollment_data = api.get_enrollments(username)
        except CourseEnrollmentError:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    "message":
                    (u"An error occurred while retrieving enrollments for user '{username}'"
                     ).format(username=username)
                })
        if username == request.user.username or GlobalStaff().has_user(request.user) or \
                self.has_api_key_permissions(request):
            return Response(enrollment_data)
        filtered_data = []
        for enrollment in enrollment_data:
            course_key = CourseKey.from_string(
                enrollment["course_details"]["course_id"])
            if user_has_role(request.user, CourseStaffRole(course_key)):
                filtered_data.append(enrollment)
        return Response(filtered_data)
 def post(self, request):
     """
     Unenrolls the specified user from all courses.
     """
     # Get the User from the request.
     username = request.data.get('user', None)
     if not username:
         return Response(status=status.HTTP_404_NOT_FOUND,
                         data={'message': u'The user was not specified.'})
     try:
         # make sure the specified user exists
         User.objects.get(username=username)
     except ObjectDoesNotExist:
         return Response(
             status=status.HTTP_404_NOT_FOUND,
             data={
                 'message':
                 u'The user "{}" does not exist.'.format(username)
             })
     try:
         enrollments = api.get_enrollments(username)
         active_enrollments = [
             enrollment for enrollment in enrollments
             if enrollment['is_active']
         ]
         if len(active_enrollments) < 1:
             return Response(status=status.HTTP_200_OK)
         return Response(api.unenroll_user_from_all_courses(username))
     except Exception as exc:  # pylint: disable=broad-except
         return Response(text_type(exc),
                         status=status.HTTP_500_INTERNAL_SERVER_ERROR)
示例#7
0
    def get(self, request):
        """Gets a list of all course enrollments for a user.

        Returns a list for the currently logged in user, or for the user named by the 'user' GET
        parameter.  If the username does not match the currently logged in user, only courses the
        requesting user has staff permissions for are listed.

        Only staff or instructor permissions on individual courses are taken into account when
        deciding whether the requesting user is permitted to see a particular enrollment, i.e.
        organizational staff access doesn't grant permission to see the enrollments in all courses
        of the organization.  This may change in the future.

        However, users with global staff access can see all enrollments of all students.
        """
        username = request.GET.get('user', request.user.username)
        try:
            enrollment_data = api.get_enrollments(username)
        except CourseEnrollmentError:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    "message": (
                        u"An error occurred while retrieving enrollments for user '{username}'"
                    ).format(username=username)
                }
            )
        if username == request.user.username or GlobalStaff().has_user(request.user) or \
                self.has_api_key_permissions(request):
            return Response(enrollment_data)
        filtered_data = []
        for enrollment in enrollment_data:
            course_key = CourseKey.from_string(enrollment["course_details"]["course_id"])
            if user_has_role(request.user, CourseStaffRole(course_key)):
                filtered_data.append(enrollment)
        return Response(filtered_data)
示例#8
0
 def test_get_all_enrollments(self, enrollments):
     for enrollment in enrollments:
         fake_data_api.add_course(enrollment['course_id'], course_modes=enrollment['course_modes'])
         api.add_enrollment(self.USERNAME, enrollment['course_id'], enrollment['mode'])
     result = api.get_enrollments(self.USERNAME)
     self.assertEqual(len(enrollments), len(result))
     for result_enrollment in result:
         self.assertIn(
             result_enrollment['course']['course_id'],
             [enrollment['course_id'] for enrollment in enrollments]
         )
示例#9
0
def list_student_enrollments(request):
    """List out all the enrollments for the current student

    Returns a JSON response with all the course enrollments for the current student.

    Args:
        request (Request): The GET request for course enrollment listings.

    Returns:
        A JSON serialized representation of the student's course enrollments.

    """
    return Response(api.get_enrollments(request.user.username))
示例#10
0
文件: views.py 项目: taozeze/ANALYSE
def list_student_enrollments(request):
    """List out all the enrollments for the current student

    Returns a JSON response with all the course enrollments for the current student.

    Args:
        request (Request): The GET request for course enrollment listings.

    Returns:
        A JSON serialized representation of the student's course enrollments.

    """
    return Response(api.get_enrollments(request.user.username))
示例#11
0
 def get(self, request, username):
     """
     Returns a list of enrollments for the given user, along with
     information about previous manual enrollment changes.
     """
     enrollments = get_enrollments(username)
     for enrollment in enrollments:
         # Folds the course_details field up into the main JSON object.
         enrollment.update(**enrollment.pop('course_details'))
         course_key = CourseKey.from_string(enrollment['course_id'])
         # Add the price of the course's verified mode.
         self.include_verified_mode_info(enrollment, course_key)
         # Add manual enrollment history, if it exists
         enrollment['manual_enrollment'] = self.manual_enrollment_data(enrollment, course_key)
     return JsonResponse(enrollments)
示例#12
0
 def get(self, request, username):
     """
     Returns a list of enrollments for the given user, along with
     information about previous manual enrollment changes.
     """
     enrollments = get_enrollments(username)
     for enrollment in enrollments:
         # Folds the course_details field up into the main JSON object.
         enrollment.update(**enrollment.pop('course_details'))
         course_key = CourseKey.from_string(enrollment['course_id'])
         # Add the price of the course's verified mode.
         self.include_verified_mode_info(enrollment, course_key)
         # Add manual enrollment history, if it exists
         enrollment['manual_enrollment'] = self.manual_enrollment_data(
             enrollment, course_key)
     return JsonResponse(enrollments)
示例#13
0
 def get(self, request):
     """Gets a list of all course enrollments for the currently logged in user."""
     username = request.GET.get('user', request.user.username)
     if request.user.username != username and not self.has_api_key_permissions(
             request):
         # Return a 404 instead of a 403 (Unauthorized). If one user is looking up
         # other users, do not let them deduce the existence of an enrollment.
         return Response(status=status.HTTP_404_NOT_FOUND)
     try:
         return Response(api.get_enrollments(username))
     except CourseEnrollmentError:
         return Response(
             status=status.HTTP_400_BAD_REQUEST,
             data={
                 "message":
                 (u"An error occurred while retrieving enrollments for user '{username}'"
                  ).format(username=username)
             })
示例#14
0
 def get(self, request):
     """Gets a list of all course enrollments for the currently logged in user."""
     username = request.GET.get('user', request.user.username)
     if request.user.username != username and not self.has_api_key_permissions(request):
         # Return a 404 instead of a 403 (Unauthorized). If one user is looking up
         # other users, do not let them deduce the existence of an enrollment.
         return Response(status=status.HTTP_404_NOT_FOUND)
     try:
         return Response(api.get_enrollments(username))
     except CourseEnrollmentError:
         return Response(
             status=status.HTTP_400_BAD_REQUEST,
             data={
                 "message": (
                     u"An error occurred while retrieving enrollments for user '{username}'"
                 ).format(username=username)
             }
         )
示例#15
0
 def post(self, request):
     """
     Unenrolls the specified user from all courses.
     """
     try:
         # Get the username from the request.
         username = request.data['username']
         # Ensure that a retirement request status row exists for this username.
         UserRetirementStatus.get_retirement_for_retirement_action(username)
         enrollments = api.get_enrollments(username)
         active_enrollments = [enrollment for enrollment in enrollments if enrollment['is_active']]
         if len(active_enrollments) < 1:
             return Response(status=status.HTTP_204_NO_CONTENT)
         return Response(api.unenroll_user_from_all_courses(username))
     except KeyError:
         return Response(u'Username not specified.', status=status.HTTP_404_NOT_FOUND)
     except UserRetirementStatus.DoesNotExist:
         return Response(u'No retirement request status for username.', status=status.HTTP_404_NOT_FOUND)
     except Exception as exc:  # pylint: disable=broad-except
         return Response(text_type(exc), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
示例#16
0
    def get(self, request):
        """Gets a list of all course enrollments for a user.

        Returns a list for the currently logged in user, or for the user named by the 'user' GET
        parameter. If the username does not match that of the currently logged in user, only
        courses for which the currently logged in user has the Staff or Admin role are listed.
        As a result, a course team member can find out which of his or her own courses a particular
        learner is enrolled in.

        Only the Staff or Admin role (granted on the Django administrative console as the staff
        or instructor permission) in individual courses gives the requesting user access to
        enrollment data. Permissions granted at the organizational level do not give a user
        access to enrollment data for all of that organization's courses.

        Users who have the global staff permission can access all enrollment data for all
        courses.
        """
        username = request.GET.get('user', request.user.username)
        try:
            enrollment_data = api.get_enrollments(username)
            print enrollment_data
        except CourseEnrollmentError:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    "message":
                    (u"An error occurred while retrieving enrollments for user '{username}'"
                     ).format(username=username)
                })
        if username == request.user.username or GlobalStaff().has_user(request.user) or \
                self.has_api_key_permissions(request):
            return Response(enrollment_data)
        filtered_data = []
        for enrollment in enrollment_data:
            course_key = CourseKey.from_string(
                enrollment["course_details"]["course_id"])
            if user_has_role(request.user, CourseStaffRole(course_key)):
                filtered_data.append(enrollment)
        return Response(filtered_data)
示例#17
0
    def get(self, request):
        """Gets a list of all course enrollments for a user.

        Returns a list for the currently logged in user, or for the user named by the 'user' GET
        parameter. If the username does not match that of the currently logged in user, only
        courses for which the currently logged in user has the Staff or Admin role are listed.
        As a result, a course team member can find out which of his or her own courses a particular
        learner is enrolled in.

        Only the Staff or Admin role (granted on the Django administrative console as the staff
        or instructor permission) in individual courses gives the requesting user access to
        enrollment data. Permissions granted at the organizational level do not give a user
        access to enrollment data for all of that organization's courses.

        Users who have the global staff permission can access all enrollment data for all
        courses.
        """
        username = request.GET.get('user', request.user.username)
        try:
            # Edraak: pass request to api.get_enrollments
            enrollment_data = api.get_enrollments(username, request=request)
        except CourseEnrollmentError:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    "message": (
                        u"An error occurred while retrieving enrollments for user '{username}'"
                    ).format(username=username)
                }
            )
        if username == request.user.username or GlobalStaff().has_user(request.user) or \
                self.has_api_key_permissions(request):
            return Response(enrollment_data)
        filtered_data = []
        for enrollment in enrollment_data:
            course_key = CourseKey.from_string(enrollment["course_details"]["course_id"])
            if user_has_role(request.user, CourseStaffRole(course_key)):
                filtered_data.append(enrollment)
        return Response(filtered_data)
示例#18
0
    def get(self, request, username_or_email):
        """
        Returns a list of enrollments for the given user, along with
        information about previous manual enrollment changes.
        """
        try:
            user = User.objects.get(Q(username=username_or_email) | Q(email=username_or_email))
        except User.DoesNotExist:
            return JsonResponse([])

        enrollments = get_enrollments(user.username, include_inactive=True)
        for enrollment in enrollments:
            # Folds the course_details field up into the main JSON object.
            enrollment.update(**enrollment.pop('course_details'))
            course_key = CourseKey.from_string(enrollment['course_id'])
            # get the all courses modes and replace with existing modes.
            enrollment['course_modes'] = self.get_course_modes(course_key)
            # Add the price of the course's verified mode.
            self.include_verified_mode_info(enrollment, course_key)
            # Add manual enrollment history, if it exists
            enrollment['manual_enrollment'] = self.manual_enrollment_data(enrollment, course_key)
        return JsonResponse(enrollments)
示例#19
0
    def get(self, request, username_or_email):
        """
        Returns a list of enrollments for the given user, along with
        information about previous manual enrollment changes.
        """
        try:
            user = User.objects.get(Q(username=username_or_email) | Q(email=username_or_email))
        except User.DoesNotExist:
            return JsonResponse([])

        enrollments = get_enrollments(user.username, include_inactive=True)
        for enrollment in enrollments:
            # Folds the course_details field up into the main JSON object.
            enrollment.update(**enrollment.pop('course_details'))
            course_key = CourseKey.from_string(enrollment['course_id'])
            # get the all courses modes and replace with existing modes.
            enrollment['course_modes'] = self.get_course_modes(course_key)
            # Add the price of the course's verified mode.
            self.include_verified_mode_info(enrollment, course_key)
            # Add manual enrollment history, if it exists
            enrollment['manual_enrollment'] = self.manual_enrollment_data(enrollment, course_key)
        return JsonResponse(enrollments)
示例#20
0
    def post(self, request):
        """
        Unenrolls the specified user from all courses.
        """
        username = None
        user_model = get_user_model()

        try:
            # Get the username from the request and check that it exists
            username = request.data['username']
            user_model.objects.get(username=username)

            enrollments = api.get_enrollments(username)
            active_enrollments = [enrollment for enrollment in enrollments if enrollment['is_active']]
            if len(active_enrollments) < 1:
                return Response(status=status.HTTP_200_OK)
            return Response(api.unenroll_user_from_all_courses(username))
        except KeyError:
            return Response(u'Username not specified.', status=status.HTTP_404_NOT_FOUND)
        except user_model.DoesNotExist:
            return Response(u'The user "{}" does not exist.'.format(username), status=status.HTTP_404_NOT_FOUND)
        except Exception as exc:  # pylint: disable=broad-except
            return Response(text_type(exc), status=status.HTTP_500_INTERNAL_SERVER_ERROR)