def handle(self, *args, **options): this_folder = os.path.dirname(os.path.realpath(__file__)) output_file_path = this_folder + "/grades.txt" lines = [] with open(output_file_path, "r") as f: for line in f: line = line.strip('\n') lines.append(line) with transaction.atomic(): new_grades = [] for line in lines: grade_data = line.split(',') course_section_id = int(grade_data[0]) if grade_data[1] == "None": marking_period_id = None else: marking_period_id = int(grade_data[1]) student_id = int(grade_data[2]) if grade_data[3] == "None": grade_decimal = None else: grade_decimal = Decimal(grade_data[3]) if grade_data[4] == "False": override_final = False else: override_final = True # the filter below should only return 1 response since # the combo of section, student, and period is guaranteed # to be unique in our db. We could just use a get_or_create # but the get() and the save() each time takes forver num_updated = Grade.objects.filter( course_section_id=course_section_id, marking_period_id=marking_period_id, student_id=student_id).update( override_final=override_final, grade=grade_decimal) if num_updated == 0: # the grade didn't exist, so the update above did nothing, # let's just create a new grade and save it at the end # using the bulk_create method which is much faster # than calling save() each time new_grade = Grade( course_section_id=course_section_id, marking_period_id=marking_period_id, student_id=student_id, grade=grade_decimal, override_final=override_final, ) new_grades.append(new_grade) Grade.objects.bulk_create(new_grades)
def test_current_vs_older(self): self.student = Student(first_name='Billy', last_name='Bob', username='******', id=12345) self.student.save() self.year = SchoolYear(name='2011-2012', start_date='2011-09-10', end_date='2012-06-15', grad_date='2012-06-17', id=2011) self.year.save() self.mp = MarkingPeriod(name="tri1 2011", start_date='2011-09-10', end_date='2012-06-15', school_year=self.year, monday=True, friday=True) self.mp.save() self.mp2 = MarkingPeriod(name="tri2 2012", start_date='2011-09-10', end_date='2012-06-15', school_year=self.year, monday=True, friday=True) self.mp2.save() self.mp3 = MarkingPeriod(name="tri3 2012", start_date='2011-09-10', end_date='2012-06-15', school_year=self.year, monday=True, friday=True) self.mp3.save() courses = [ Course(fullname='Algebra', shortname='alg', id=12, credits=4), Course(fullname='English', shortname='eng', id=13, credits=4), Course(fullname='History', shortname='hist', id=14, credits=4) ] course_sections = [] for course in courses: course.save() course_section = CourseSection(course=course, name=course.fullname) course_section.save() course_sections.append(course_section) course_sections[0].marking_period.add(self.mp) course_sections[1].marking_period.add(self.mp2) course_sections[2].marking_period.add(self.mp3) grades = [ Grade(student=self.student, course_section=course_sections[0], grade=86.78, marking_period=self.mp), Grade(student=self.student, course_section=course_sections[1], grade=94.73, marking_period=self.mp2), Grade(student=self.student, course_section=course_sections[2], grade=77.55, marking_period=self.mp3) ] for grade in grades: grade.save() course_enrollments = [ CourseEnrollment(user=self.student, course_section=course_sections[0], grade=grades[0]), CourseEnrollment(user=self.student, course_section=course_sections[1], grade=grades[1]), CourseEnrollment(user=self.student, course_section=course_sections[2], grade=grades[2]) ] for course_enrollment in course_enrollments: course_enrollment.save() StudentYearGrade.objects.create(student=self.student, year=self.year) syg = self.student.studentyeargrade_set.get(year=self.year) current = syg.calculate_grade_and_credits( date_report=datetime.date.today()) older = syg.calculate_grade_and_credits() self.assertEqual(current, older)