def test_first_matching_item(self): """ Tests that first_matching_item returns a matching item in an iterable, or None """ iterable = [1, 2, 3, 4, 5] first_matching = first_matching_item(iterable, lambda item: item == 1) second_matching = first_matching_item(iterable, lambda item: item == 5) third_matching = first_matching_item(iterable, lambda item: item == 10) assert first_matching == 1 assert second_matching == 5 assert third_matching is None
def first_unexpired_run(self): """ Gets the first unexpired CourseRun associated with this Course Returns: CourseRun or None: An unexpired course run """ return first_matching_item(self.courserun_set.all(), lambda course_run: course_run.is_unexpired)
def get_start_date(self, program): """Get the starting date of the first course in the program""" course = program.course_set.order_by("position_in_program").first() if not course: return None first_unexpired = first_matching_item( course.courserun_set.all().order_by("start_date"), lambda run: run.is_unexpired) return first_unexpired.start_date if first_unexpired else None
def get_end_date(self, program): """Get the ending date of the last course of the program""" course = program.course_set.order_by("position_in_program").last() if not course: return None last_unexpired = first_matching_item( course.courserun_set.all().order_by("-start_date"), lambda run: run.is_unexpired) return last_unexpired.end_date if last_unexpired else None
def first_unexpired_run(self): """ Gets the first unexpired CourseRun associated with this Course Returns: CourseRun or None: An unexpired course run """ return first_matching_item( self.courserun_set.all(), lambda course_run: course_run.is_unexpired )
def deserialize_grade_data(user, course_runs, grade_data_list): """ Deserializes grade data for a user """ edx_course_key = None for grade_data in grade_data_list: edx_course_key = grade_data['edx_course_key'] course_run = first_matching_item( course_runs, lambda cr: cr.edx_course_key == edx_course_key) grade = Decimal(grade_data['grade']) FinalGrade.objects.update_or_create(user=user, course_run=course_run, grade=grade, passed=grade >= PASSING_GRADE, status=FinalGradeStatus.COMPLETE, course_run_paid_on_edx=True)
def deserialize_grade_data(user, course_runs, grade_data_list): """ Deserializes grade data for a user """ edx_course_key = None for grade_data in grade_data_list: edx_course_key = grade_data['edx_course_key'] course_run = first_matching_item( course_runs, lambda cr: cr.edx_course_key == edx_course_key ) grade = Decimal(grade_data['grade']) FinalGrade.objects.update_or_create( user=user, course_run=course_run, grade=grade, passed=grade >= PASSING_GRADE, status=FinalGradeStatus.COMPLETE, course_run_paid_on_edx=True )
def deserialize_enrollment_data(user, social_username, course_runs, enrollment_data_list): """ Deserializes enrollment data for a user """ enrollment_handler = CachedEnrollmentHandler(user, social_username=social_username) enrolled_programs = set() edx_course_key = None for enrollment_data in enrollment_data_list: edx_course_key = enrollment_data['edx_course_key'] course_run = first_matching_item( course_runs, lambda cr: cr.edx_course_key == edx_course_key ) enrollment_handler.set_or_create(course_run) enrolled_programs.add(course_run.course.program) if course_run.course.program.financial_aid_availability: add_paid_order_for_course(user, course_run) # Add ProgramEnrollments for any Program that has an associated CachedEnrollment for enrolled_program in enrolled_programs: ProgramEnrollment.objects.get_or_create(user=user, program=enrolled_program)
def deserialize_enrollment_data(user, social_username, course_runs, enrollment_data_list): """ Deserializes enrollment data for a user """ enrollment_handler = CachedEnrollmentHandler( user, social_username=social_username) enrolled_programs = set() edx_course_key = None for enrollment_data in enrollment_data_list: edx_course_key = enrollment_data['edx_course_key'] course_run = first_matching_item( course_runs, lambda cr: cr.edx_course_key == edx_course_key) enrollment_handler.set_or_create(course_run) enrolled_programs.add(course_run.course.program) if course_run.course.program.financial_aid_availability: add_paid_order_for_course(user, course_run) # Add ProgramEnrollments for any Program that has an associated CachedEnrollment for enrolled_program in enrolled_programs: ProgramEnrollment.objects.get_or_create(user=user, program=enrolled_program)