Beispiel #1
0
    def test_post_response_valid_xml(self):
        """
        Test post grade with valid XML response
        """
        uri = 'https://localhost:8000/dev_stack'

        def request_callback(request, cburi, headers):
            # pylint: disable=unused-argument,
            """
            Mock expected response.
            """
            return 200, headers, self.expected_response

        httpretty.register_uri(httpretty.POST, uri, body=request_callback)
        consumers = {
            "__consumer_key__": {
                "secret": "__lti_secret__",
                "cert": TEST_CLIENT_CERT,
            },
        }
        body = generate_request_xml('message_identifier_id', 'operation',
                                    'lis_result_sourcedid', '1.0')
        ret = post_message(consumers, "__consumer_key__", uri, body)
        self.assertTrue(ret)

        ret = post_message2(consumers, "__consumer_key__", uri, body)
        self.assertTrue(ret)
Beispiel #2
0
    def test_post_response_valid_xml(self):
        """
        Test post grade with valid XML response
        """
        uri = 'https://localhost:8000/dev_stack'

        def request_callback(request, cburi, headers):
            # pylint: disable=unused-argument,
            """
            Mock expected response.
            """
            return 200, headers, self.expected_response

        httpretty.register_uri(httpretty.POST, uri, body=request_callback)
        consumers = {
            "__consumer_key__": {
                "secret": "__lti_secret__",
                "cert": TEST_CLIENT_CERT,
            },
        }
        body = generate_request_xml('message_identifier_id', 'operation',
                                    'lis_result_sourcedid', '1.0')
        ret = post_message(consumers, "__consumer_key__", uri, body)
        self.assertTrue(ret)

        ret = post_message2(consumers, "__consumer_key__", uri, body)
        self.assertTrue(ret)
Beispiel #3
0
    def post_score(self, submission):
        """
        Post grade to LTI consumer using XML

        :param: score: 0 <= score <= 1. (Score MUST be between 0 and 1)
        :return: True if post successful and score valid
        :exception: LTIPostMessageException if call failed
        """
        score = submission.user_score()
        launch_url = self.get_launch_url(submission)
        print(launch_url)

        xml = self.lti.generate_request_xml(
            self.message_identifier(), 'replaceResult',
            self.lti.lis_result_sourcedid(self.request), score, launch_url)

        if post_message(self.lti.consumers(),
                        self.lti.oauth_consumer_key(self.request),
                        self.lti.lis_outcome_service_url(self.request), xml):
            return True
        else:
            msg = ('An error occurred while saving your score. '
                   'Please try again.')
            messages.add_message(self.request, messages.ERROR, msg)

            # Something went wrong, display an error.
            # Is 500 the right thing to do here?
            raise LTIPostMessageException('Post grade failed')
Beispiel #4
0
    def post(self, request):
        """
        Post grade to LTI consumer using XML
        :param: score: 0 <= score <= 1. (Score MUST be between 0 and 1)
        :return: True if post successful and score valid
        :exception: LTIPostMessageException if call failed
        """
        try:
            lti_session = ltiSession.objects.get(
                id=request.data["ltisession"]["id"])
        except ltiSession.DoesNotExist:
            return Response(data={
                "error": "No LTI session exists for this ID"
            }, status=status.HTTP_400_BAD_REQUEST)
        consumer = lticonsumer.objects.get(id=lti_session.lti_consumer.id)
        schematic = StateSave.objects.get(save_id=request.data["schematic"])
        schematic.shared = True
        schematic.save()
        submission_data = {
            "project": consumer,
            "student": schematic.owner,
            "score": consumer.score,
            "ltisession": lti_session,
            "schematic": schematic
        }
        submission = Submission.objects.create(**submission_data)
        xml = generate_request_xml(
            message_identifier(), 'replaceResult',
            lti_session.lis_result_sourcedid, submission.score)
        msg = ""
        try:
            post = post_message(
                consumers(), lti_session.oauth_consumer_key,
                lti_session.lis_outcome_service_url, xml)
            if not post:
                msg = 'An error occurred while saving your score.\
                     Please try again.'
                raise LTIPostMessageException('Post grade failed')
            else:
                submission.lms_success = True
                submission.save()
                msg = 'Your score was submitted. Great job!'
                return Response(data={"message": msg},
                                status=status.HTTP_200_OK)

        except LTIException:
            submission.lms_success = False
            submission.save()
            return Response(data={"message": msg},
                            status=status.HTTP_400_BAD_REQUEST)
Beispiel #5
0
    def test_post_response_invalid_xml(self):
        """
        Test post message with invalid XML response
        """
        uri = (u'https://edge.edx.org/courses/MITx/ODL_ENG/2014_T1/xblock/'
               u'i4x:;_;_MITx;_ODL_ENG;_lti;_94173d3e79d145fd8ec2e83f15836ac8/'
               u'handler_noauth/grade_handler')

        def request_callback(request, cburi, headers):
            return 200, headers, "success"

        httpretty.register_uri(httpretty.POST, uri, body=request_callback)
        consumers = {"__consumer_key__": {"secret": "__lti_secret__"}}
        body = '<xml></xml>'
        ret = post_message(consumers, "__consumer_key__", uri, body)
        self.assertFalse(ret)
    def test_post_response_invalid_xml(self):
        """
        Test post message with invalid XML response
        """
        uri = (u'https://edge.edx.org/courses/MITx/ODL_ENG/2014_T1/xblock/'
               u'i4x:;_;_MITx;_ODL_ENG;_lti;_94173d3e79d145fd8ec2e83f15836ac8/'
               u'handler_noauth/grade_handler')

        def request_callback(request, cburi, headers):
            return 200, headers, "success"

        httpretty.register_uri(httpretty.POST, uri, body=request_callback)
        consumers = {
            "__consumer_key__": {"secret": "__lti_secret__"}
        }
        body = '<xml></xml>'
        ret = post_message(consumers, "__consumer_key__", uri, body)
        self.assertFalse(ret)
Beispiel #7
0
def send_grade_to_lms(request, attempt):
    """
    Sends the grade to lms using xml.
    :param request: django request object
    :param attempt: the Response object whose data is to be sent
    :return: None if quiz is not graded.
    """
    quiz = db.get_quiz(request)
    quiz_settings = db.get_quiz_settings(quiz)
    student = db.get_user(request)
    if not quiz_settings.graded:
        return None
    outcome_service_url = lti.get_outcome_service_url(request)
    result_sourcedid = lti.get_result_sourced_id(request)
    # if outcome_service_url or result_sourcedid is not available in this request, search the database
    # It is very unlikely (or maybe impossible) that outcome_service_url will not be in the request itself,
    # but there is no harm in searching database if we can't find it in the current request ( or is there? )
    if not (outcome_service_url or result_sourcedid):
        try:
            outcome_service_data = OutcomeServiceData.objects.get(user=student,
                                                                  quiz=quiz)
            outcome_service_url = outcome_service_data.lis_outcome_service_url
            result_sourcedid = outcome_service_data.lis_result_sourcedid
        except OutcomeServiceData.DoesNotExists:
            return  # cannot send the grade as outcome_service_url is not available

    consumer_key = lti.get_oauth_consumer_key(request)
    message_identifier_id = "iquiz_grade"  # TODO: Is this correct?
    operation = "replaceResult"
    score = get_grade(attempt, quiz)
    xml = generate_request_xml(message_identifier_id=message_identifier_id,
                               operation=operation,
                               lis_result_sourcedid=result_sourcedid,
                               score=score)

    if not post_message(CONSUMERS, consumer_key, outcome_service_url, xml):
        raise Exception("Some error occurred while sending grade to the lms.")

    # add the last successfully sent response id and time to the outcome_service_data
    outcome_service_data = OutcomeServiceData.objects.get_or_create(
        user=student, quiz=quiz)
    outcome_service_data.response = attempt
    outcome_service_data.outcome_send_time = datetime.datetime.utcnow()
Beispiel #8
0
    def get(self, request, submissionid, grade):
        user = request.user
        submitted_assignment = SubmittedAssignments.objects.get(
            id=submissionid)
        lis_result_sourcedid = submitted_assignment.lis_result_sourcedid
        if lis_result_sourcedid:
            xml = generate_request_xml('{:.0f}'.format(time.time()),
                                       'replaceResult', lis_result_sourcedid,
                                       grade)
            config = getattr(settings, 'PYLTI_CONFIG', dict())
            consumers = config.get('consumers', dict())
            print(consumers)
            print(submitted_assignment.consumer_key)
            if not post_message(consumers, submitted_assignment.consumer_key,
                                submitted_assignment.lis_outcome_service_url,
                                xml):

                # Something went wrong, display an error.
                # Is 500 the right thing to do here?
                print("Scoring not successful")
            else:
                print('Your score was submitted. Great job!')

        submitted_assignment_serializer = SubmittedAssignmentsSerializer(
            submitted_assignment,
            data={
                'grade': grade,
                'graded': True
            },
            partial=True)
        if submitted_assignment_serializer.is_valid():
            submitted_assignment_serializer.save()
        else:
            return Response({'error': submitted_assignment_serializer.errors},
                            status=500)
        return Response(
            {'submitted_assignment': submitted_assignment_serializer.data},
            status=200)
Beispiel #9
0
    def post(self, request, *args, **kwargs):
        """
        Post grade to LTI consumer using XML

        :param: score: 0 <= score <= 1. (Score MUST be between 0 and 1)
        :return: True if post successful and score valid
        :exception: LTIPostMessageException if call failed
        """
        try:
            score = float(request.POST.get('score'))
        except ValueError:
            score = 0

        redirect_url = request.POST.get('next', '/')
        launch_url = request.POST.get('launch_url', None)

        xml = self.lti.generate_request_xml(
            self.message_identifier(), 'replaceResult',
            self.lti.lis_result_sourcedid(request), score, launch_url)

        if not post_message(self.lti.consumers(),
                            self.lti.oauth_consumer_key(request),
                            self.lti.lis_outcome_service_url(request), xml):

            msg = ('An error occurred while saving your score. '
                   'Please try again.')
            messages.add_message(request, messages.ERROR, msg)

            # Something went wrong, display an error.
            # Is 500 the right thing to do here?
            raise LTIPostMessageException('Post grade failed')
        else:
            msg = ('Your score was submitted. Great job!')
            messages.add_message(request, messages.INFO, msg)

            return HttpResponseRedirect(redirect_url)
Beispiel #10
0
    def post(self, request, *args, **kwargs):
        """
        Post grade to LTI consumer using XML

        :param: score: 0 <= score <= 1. (Score MUST be between 0 and 1)
        :return: True if post successful and score valid
        :exception: LTIPostMessageException if call failed
        """
        try:
            score = float(request.POST.get('score'))
        except ValueError:
            score = 0

        redirect_url = request.POST.get('next', '/')
        launch_url = request.POST.get('launch_url', None)

        xml = self.lti.generate_request_xml(
            self.message_identifier(), 'replaceResult',
            self.lti.lis_result_sourcedid(request), score, launch_url)

        if not post_message(
            self.lti.consumers(), self.lti.oauth_consumer_key(request),
                self.lti.lis_outcome_service_url(request), xml):

            msg = ('An error occurred while saving your score. '
                   'Please try again.')
            messages.add_message(request, messages.ERROR, msg)

            # Something went wrong, display an error.
            # Is 500 the right thing to do here?
            raise LTIPostMessageException('Post grade failed')
        else:
            msg = ('Your score was submitted. Great job!')
            messages.add_message(request, messages.INFO, msg)

            return HttpResponseRedirect(redirect_url)