def test_update_all_cached_grade_data(self, mock_refr, mock_enr, mock_cert, mock_grade): """Test for update_all_cached_grade_data""" for mock_func in (mock_refr, mock_enr, mock_cert, mock_grade, ): assert mock_func.called is False CachedEdxDataApi.update_all_cached_grade_data(self.user) assert mock_enr.called is False mock_refr.assert_called_once_with(self.user.social_auth.get(provider=EdxOrgOAuth2.name)) for mock_func in (mock_cert, mock_grade, ): mock_func.assert_called_once_with(self.user, ANY)
def test_update_all_cached_grade_data(self, mock_refr, mock_enr, mock_cert, mock_grade): """Test for update_all_cached_grade_data""" for mock_func in ( mock_refr, mock_enr, mock_cert, mock_grade, ): assert mock_func.called is False CachedEdxDataApi.update_all_cached_grade_data(self.user) assert mock_enr.called is False mock_refr.assert_called_once_with( self.user.social_auth.get(provider=EdxOrgOAuth2.name)) for mock_func in ( mock_cert, mock_grade, ): mock_func.assert_called_once_with(self.user, ANY)
def freeze_user_final_grade(user, course_run, raise_on_exception=False): """ Public function to freeze final grades for the a user in a course run. Args: user (User): a django User course_run (CourseRun): a course run model object raise_on_exception (bool): If true, raise an exception on error. Else create a warning and exit Returns: FinalGrade: The final grade created, or None if there was an exception but raise_on_exception was False """ # no need to do anything if the course run is not ready if not course_run.can_freeze_grades: if not raise_on_exception: log.info( 'The grade for user "%s" course "%s" cannot be frozen yet', user.username, course_run.edx_course_key ) return None else: raise FreezeGradeFailedException( 'The grade for user "{0}" course "{1}" cannot be frozen yet'.format( user.username, course_run.edx_course_key, ) ) # update one last time the user's certificates and current grades try: CachedEdxDataApi.update_all_cached_grade_data(user) except Exception as ex: # pylint: disable=broad-except con = get_redis_connection("redis") con.lpush(CACHE_KEY_FAILED_USERS_BASE_STR.format(course_run.edx_course_key), user.id) if not raise_on_exception: log.exception( 'Impossible to refresh the edX cache for user "%s" in course %s', user.username, course_run.edx_course_key ) return None else: raise FreezeGradeFailedException( 'Impossible to refresh the edX cache for user "{0}" in course {1}'.format( user.username, course_run.edx_course_key ) ) from ex # get the final grade for the user in the program try: final_grade = get_final_grade(user, course_run) except Exception as ex: # pylint: disable=broad-except # If user doesn't have a grade no need to freeze con = get_redis_connection("redis") con.lpush(CACHE_KEY_FAILED_USERS_BASE_STR.format(course_run.edx_course_key), user.id) if not raise_on_exception: log.exception( 'Impossible to get final grade for user "%s" in course %s', user.username, course_run.edx_course_key) return None else: raise FreezeGradeFailedException( 'Impossible to get final grade for user "{0}" in course {1}'.format( user.username, course_run.edx_course_key ) ) from ex # the final grade at this point should not exists, but putting a `get_or_create` # should solve the problem when the function is called synchronously from the dashboard REST API multiple times final_grade_obj, _ = FinalGrade.objects.get_or_create( user=user, course_run=course_run, grade=final_grade.grade, passed=final_grade.passed, status=FinalGradeStatus.COMPLETE, course_run_paid_on_edx=final_grade.payed_on_edx ) return final_grade_obj
def freeze_user_final_grade(user, course_run, raise_on_exception=False): """ Public function to freeze final grades for the a user in a course run. Args: user (User): a django User course_run (CourseRun): a course run model object raise_on_exception (bool): If true, raise an exception on error. Else create a warning and exit Returns: FinalGrade: The final grade created, or None if there was an exception but raise_on_exception was False """ # no need to do anything if the course run is not ready if not course_run.can_freeze_grades: if not raise_on_exception: log.info( 'The grade for user "%s" course "%s" cannot be frozen yet', user.username, course_run.edx_course_key ) return None else: raise FreezeGradeFailedException( 'The grade for user "{0}" course "{1}" cannot be frozen yet'.format( user.username, course_run.edx_course_key, ) ) # update one last time the user's certificates and current grades try: CachedEdxDataApi.update_all_cached_grade_data(user) except Exception as ex: # pylint: disable=broad-except con = get_redis_connection("redis") con.lpush(CACHE_KEY_FAILED_USERS_BASE_STR.format(course_run.edx_course_key), user.id) if not raise_on_exception: log.exception( 'Impossible to refresh the edX cache for user "%s" in course %s', user.username, course_run.edx_course_key ) return None else: raise FreezeGradeFailedException( 'Impossible to refresh the edX cache for user "{0}" in course {1}'.format( user.username, course_run.edx_course_key ) ) from ex # get the final grade for the user in the program try: final_grade = get_final_grade(user, course_run) except Exception as ex: # pylint: disable=broad-except # If user doesn't have a grade no need to freeze con = get_redis_connection("redis") con.lpush(CACHE_KEY_FAILED_USERS_BASE_STR.format(course_run.edx_course_key), user.id) if not raise_on_exception: log.exception( 'Impossible to get final grade for user "%s" in course %s', user.username, course_run.edx_course_key) return None else: raise FreezeGradeFailedException( 'Impossible to get final grade for user "{0}" in course {1}'.format( user.username, course_run.edx_course_key ) ) from ex # the final grade at this point should not exists, but putting a `get_or_create` # should solve the problem when the function is called synchronously from the dashboard REST API multiple times final_grade_obj, _ = FinalGrade.objects.get_or_create( user=user, course_run=course_run, grade=final_grade.grade, passed=final_grade.passed, status=FinalGradeStatus.COMPLETE, course_run_paid_on_edx=final_grade.payed_on_edx ) return final_grade_obj