'Merge capa state and grades, if possible.'

def handle_capa_merge(self, correct_course_row, newline_course_row, dry_run):

    log.info('->    Merging    capa    grade    and    state    information    for    CSM    rows    %s    (newline)    and    %s    (correct    course_id)', newline_course_row.id, correct_course_row.id)
    (state, grade, max_grade) = self.capa_state_and_grade(correct_course_row, newline_course_row)
    nl_grade_tuple = (newline_course_row.grade, newline_course_row.max_grade)
    cc_grade_tuple = (correct_course_row.grade, correct_course_row.max_grade)
    safe_to_change_grade = ((grade, max_grade) in [nl_grade_tuple, cc_grade_tuple])
    if (not safe_to_change_grade):
        log.error('->    Derived    grade    %s    does    not    match    grades    from    either    %s    %s    or    %s    %s    --    Archiving    newline    entry    %s    and    leaving    %s    alone', (grade, max_grade), newline_course_row.id, nl_grade_tuple, correct_course_row.id, cc_grade_tuple, newline_course_row.id, correct_course_row.id)
    safe_to_change_result = FixResult(record_trimmed=0, data_copied=0, record_archived=1, capa_merge=1, error=0)
    not_safe_to_change_result = FixResult(record_trimmed=0, data_copied=0, record_archived=1, capa_merge=0, error=1)
    if dry_run:
        return (safe_to_change_result if safe_to_change_grade else not_safe_to_change_result)
    try:
        with transaction.atomic():
            if safe_to_change_grade:
                correct_course_row.state = state
                correct_course_row.grade = grade
                correct_course_row.max_grade = max_grade
                correct_course_row.save()
            self.cleanup_row(newline_course_row)
        return (safe_to_change_result if safe_to_change_grade else not_safe_to_change_result)
    except DatabaseError:
        log.exception('Failed    while    trying    to    merge    capa    CSM    row    %s    (newline)    and    %s    (correct    course_id)')
        return FixResult(record_trimmed=0, data_copied=0, record_archived=0, capa_merge=0, error=1)
