Example #1
0
def refresh_one_course(cursor, course):
    course_row = cursor.execute("""SELECT * 
        FROM pom.Courses 
        WHERE CourseCode = ?""", course.cx_code.encode('utf8')).fetchone()
    
    logger.info("Populating information for [{0}] {1}".format(
        course.cx_code, course_row.Name))
    
    course.name = _sanitize(course_row.Name)
    course.grading_style = _sanitize(course_row.GradingStyle)
    course.description = _sanitize(course_row.Description)
    course.note = BR_TAGS.sub('\n', _sanitize(course_row.Note)).strip()
    course.credit = float(course_row.Credits)
    course.number = int(course_row.Number)
    course.spots = int(course_row.SeatsTotal)
    course.filled = int(course_row.SeatsFilled)
    course.primary_association = CAMPUSES_LOOKUP.get(course_row.PrimaryAssociation, -1)
    
    course.save()
    
    # Get the instructors for the course
    instructors = cursor.execute("""SELECT DISTINCT(pi.Name), pc.CourseCode, pi.InstructorID
        FROM pom.Instructors AS pi 
        JOIN pom.Courses AS pc 
        ON (pc.CourseCode = pi.CourseCode) 
        WHERE pi.CourseCode = ?
        ORDER BY pi.InstructorID;""", course.cx_code.encode('utf8')).fetchall()
    
    inames = []
    for instructor in instructors:
        inames.append(instructor.Name)
    
    course.instructor = "; ".join(inames)
    
    # TODO: Normalize instructors into their own table
    
    # Check for fees or prerequisites
    match = FEE_REGEX.findall(unicode(course.description))
    if match:
        course.fee = True
    
    # TODO: add a prerequisites booleanfield
    # if course_row.Requisites == "Y":
    #     course.prerequisites = True
    
    # Populate meeting times and locations
    
    refresh_meetings(cursor, course)
    
    # Populate departments and requirement areas
    
    try:
        course.primary_department = Department.objects.get(code=course_row.Department)
    except Department.DoesNotExist:
        logger.warning("Tried to create a course record for {0} in the {1} "
            "department, but {1} did not exist. Skipping.".format(
                course.cx_code, course_row.Department))
        course.delete()
        return
    
    # Clear secondary department/RA associations in case they've changed
    course.departments.clear()
    course.requirement_areas.clear()
    
    dept_rows = cursor.execute("""SELECT CallingDepartment 
        FROM pom.Courses
        WHERE CourseCode = ?;""", course.cx_code.encode('utf8')).fetchall()
    
    for dept in dept_rows:
        if _is_requirement_area(dept.CallingDepartment):
            course.requirement_areas.add(
                RequirementArea.objects.get(code=dept.CallingDepartment)
            )
        else:
            course.departments.add(
                Department.objects.get(code=dept.CallingDepartment)
            )
    
    course.save()