Exemplo n.º 1
0
    def update(self,
               subsection,
               only_if_higher=None,
               score_deleted=False,
               force_update_subsections=False,
               persist_grade=True):
        """
        Updates the SubsectionGrade object for the student and subsection.
        """
        self._log_event(log.debug,
                        u"update, subsection: {}".format(subsection.location),
                        subsection)

        calculated_grade = CreateSubsectionGrade(
            subsection,
            self.course_data.structure,
            self._submissions_scores,
            self._csm_scores,
        )

        if persist_grade and should_persist_grades(
                self.course_data.course_key):
            if only_if_higher:
                try:
                    grade_model = PersistentSubsectionGrade.read_grade(
                        self.student.id, subsection.location)
                except PersistentSubsectionGrade.DoesNotExist:
                    pass
                else:
                    orig_subsection_grade = ReadSubsectionGrade(
                        subsection, grade_model, self)
                    if not is_score_higher_or_equal(
                            orig_subsection_grade.graded_total.earned,
                            orig_subsection_grade.graded_total.possible,
                            calculated_grade.graded_total.earned,
                            calculated_grade.graded_total.possible,
                            treat_undefined_as_zero=True,
                    ):
                        return orig_subsection_grade

            grade_model = calculated_grade.update_or_create_model(
                self.student, score_deleted, force_update_subsections)
            self._update_saved_subsection_grade(subsection.location,
                                                grade_model)

            if settings.FEATURES.get(
                    'ENABLE_COURSE_ASSESSMENT_GRADE_CHANGE_SIGNAL'):
                COURSE_ASSESSMENT_GRADE_CHANGED.send_robust(
                    sender=self,
                    user=self.student,
                    subsection_id=calculated_grade.location,
                    subsection_grade=calculated_grade.graded_total.earned)

        return calculated_grade
    def test_handle_enterprise_learner_subsection(self):
        """
        Test to assert transmit_subsection_learner_data is called when COURSE_ASSESSMENT_GRADE_CHANGED signal is fired.
        """
        with patch(
                'integrated_channels.integrated_channel.tasks.transmit_single_subsection_learner_data.apply_async',
                return_value=None) as mock_task_apply:
            course_key = CourseKey.from_string(self.course_id)
            COURSE_ASSESSMENT_GRADE_CHANGED.disconnect()
            COURSE_ASSESSMENT_GRADE_CHANGED.send(sender=None,
                                                 user=self.user,
                                                 course_id=course_key,
                                                 subsection_id='subsection_id',
                                                 subsection_grade=1.0)
            assert not mock_task_apply.called

            self._create_enterprise_enrollment(self.user.id, self.course_id)
            task_kwargs = {
                'username': self.user.username,
                'course_run_id': self.course_id,
                'subsection_id': 'subsection_id',
                'grade': '1.0'
            }
            COURSE_ASSESSMENT_GRADE_CHANGED.send(sender=None,
                                                 user=self.user,
                                                 course_id=course_key,
                                                 subsection_id='subsection_id',
                                                 subsection_grade=1.0)
            mock_task_apply.assert_called_once_with(kwargs=task_kwargs)
            COURSE_ASSESSMENT_GRADE_CHANGED.connect(listen_for_passing_grade)