コード例 #1
0
    def get(self, request, subsection_id):
        """
        Returns subection grade data, override grade data and a history of changes made to
        a specific users specific subsection grade.

        Args:
            subsection_id: String representation of a usage_key, which is an opaque key of
            a persistant subection grade.
            user_id: An integer represenation of a user

        """
        try:
            usage_key = UsageKey.from_string(subsection_id)
        except InvalidKeyError:
            raise self.api_error(status_code=status.HTTP_404_NOT_FOUND,
                                 developer_message='Invalid UsageKey',
                                 error_code='invalid_usage_key')

        if not has_course_author_access(request.user, usage_key.course_key):
            raise DeveloperErrorViewMixin.api_error(
                status_code=status.HTTP_403_FORBIDDEN,
                developer_message=
                'The requesting user does not have course author permissions.',
                error_code='user_permissions',
            )

        try:
            user_id = int(request.GET.get('user_id'))
        except ValueError:
            raise self.api_error(status_code=status.HTTP_404_NOT_FOUND,
                                 developer_message='Invalid UserID',
                                 error_code='invalid_user_id')
        success = True
        err_msg = ""
        override = None
        history = []
        history_record_limit = request.GET.get('history_record_limit')
        if history_record_limit is not None:
            try:
                history_record_limit = int(history_record_limit)
            except ValueError:
                history_record_limit = 0

        try:
            original_grade = PersistentSubsectionGrade.read_grade(
                user_id, usage_key)
            if original_grade is not None and hasattr(original_grade,
                                                      'override'):
                override = original_grade.override
                # pylint: disable=no-member
                history = list(
                    PersistentSubsectionGradeOverride.history.filter(
                        grade_id=original_grade.id).order_by('history_date')
                    [:history_record_limit])
            grade_data = {
                'earned_all': original_grade.earned_all,
                'possible_all': original_grade.possible_all,
                'earned_graded': original_grade.earned_graded,
                'possible_graded': original_grade.possible_graded,
            }
        except PersistentSubsectionGrade.DoesNotExist:
            try:
                grade_data = self._get_grade_data_for_not_attempted_assignment(
                    user_id, usage_key)
            except SubsectionUnavailableToUserException as exc:
                success = False
                err_msg = str(exc)
                grade_data = {
                    'earned_all': 0,
                    'possible_all': 0,
                    'earned_graded': 0,
                    'possible_graded': 0,
                }

        response_data = {
            'success': success,
            'original_grade': grade_data,
            'override': override,
            'history': history,
            'subsection_id': usage_key,
            'user_id': user_id,
            'course_id': usage_key.course_key,
        }
        if not success:
            response_data['error_message'] = err_msg
        results = SubsectionGradeResponseSerializer(response_data)
        return Response(results.data)
コード例 #2
0
    def get(self, request, subsection_id):
        """
        Returns subection grade data, override grade data and a history of changes made to
        a specific users specific subsection grade.

        Args:
            subsection_id: String representation of a usage_key, which is an opaque key of
            a persistant subection grade.
            user_id: An integer represenation of a user

        """
        try:
            usage_key = UsageKey.from_string(subsection_id)
        except InvalidKeyError:
            raise self.api_error(status_code=status.HTTP_404_NOT_FOUND,
                                 developer_message='Invalid UsageKey',
                                 error_code='invalid_usage_key')

        if not has_course_author_access(request.user, usage_key.course_key):
            raise DeveloperErrorViewMixin.api_error(
                status_code=status.HTTP_403_FORBIDDEN,
                developer_message=
                'The requesting user does not have course author permissions.',
                error_code='user_permissions',
            )

        try:
            user_id = int(request.GET.get('user_id'))
        except ValueError:
            raise self.api_error(status_code=status.HTTP_404_NOT_FOUND,
                                 developer_message='Invalid UserID',
                                 error_code='invalid_user_id')

        try:
            original_grade = PersistentSubsectionGrade.read_grade(
                user_id, usage_key)
        except PersistentSubsectionGrade.DoesNotExist:
            results = SubsectionGradeResponseSerializer({
                'original_grade': None,
                'override': None,
                'history': [],
                'subsection_id': usage_key,
                'user_id': user_id,
                'course_id': None,
            })

            return Response(results.data)

        limit_history_request_value = request.GET.get('history_record_limit')
        if limit_history_request_value is not None:
            try:
                history_record_limit = int(limit_history_request_value)
            except ValueError:
                history_record_limit = 0

        try:
            override = original_grade.override
            if limit_history_request_value is not None:
                history = reversed(
                    list(override.history.all().order_by('-history_date')
                         [:history_record_limit]))
            else:
                history = override.history.all().order_by('history_date')
        except PersistentSubsectionGradeOverride.DoesNotExist:
            override = None
            history = []

        results = SubsectionGradeResponseSerializer({
            'original_grade':
            original_grade,
            'override':
            override,
            'history':
            history,
            'subsection_id':
            original_grade.usage_key,
            'user_id':
            original_grade.user_id,
            'course_id':
            original_grade.course_id,
        })
        return Response(results.data)