diff --git a/courses/migrations/0036_update_letter_grades_for_dedp.py b/courses/migrations/0036_update_letter_grades_for_dedp.py new file mode 100644 index 0000000000..33c8bc5161 --- /dev/null +++ b/courses/migrations/0036_update_letter_grades_for_dedp.py @@ -0,0 +1,60 @@ +# Generated by Django 3.2.18 on 2023-06-30 20:33 + +import datetime + +import pytz +from django.db import migrations + +from courses.models import Program + + +def convert_to_letter(grade): + """Convert a decimal number to letter grade""" + if grade >= 0.825: + return "A" + elif grade >= 0.65: + return "B" + elif grade >= 0.55: + return "C" + elif grade >= 0.50: + return "D" + else: + return "F" + + +def populate_letter_grade(apps, schema_editor): + """Fix the letter grade for the DEDP courses prior to 3T2022 + that was set by 0033_populate_letter_grades""" + CourseRunGrade = apps.get_model("courses", "CourseRunGrade") + Course = apps.get_model("courses", "Course") + + dedp_program = Program.objects.filter(readable_id="program-v1:MITx+DEDP").first() + + if dedp_program is not None: + dedp_course_ids = [course[0].id for course in dedp_program.courses] + # Good Economics for Hard Times need to be added + dedp_course_ids.append( + Course.objects.filter(readable_id="course-v1:MITxT+14.009x").first() + ) + else: + dedp_course_ids = [] + + grades = CourseRunGrade.objects.filter( + passed=True, + set_by_admin=True, + course_run__course_id__in=dedp_course_ids, + course_run__start_date__lt=datetime.datetime(2022, 9, 1, tzinfo=pytz.UTC), + ) + for grade in grades: + grade.letter_grade = convert_to_letter(grade.grade) + grade.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("courses", "0035_merge_related_name_and_backfill_enrollments"), + ] + + operations = [ + migrations.RunPython(populate_letter_grade, migrations.RunPython.noop), + ]