def grades(): user = validate_user() categories = session.query(Category).all() homeworks = get_homeworks_before() update_hw_grades(user, homeworks) session.commit() gradebook, max_scores = get_gradebook() grades = [entry for entry in gradebook if entry[0] == user] if grades: grades = grades[0][1] else: grades = {hw.id: get_grade(user.stuid, hw.id) for hw in homeworks} return render_template("grades.html", homeworks=homeworks, grades=grades, max_scores=max_scores, options=options, user=user, categories=categories)
def get_gradebook(): """ helper function that gets the gradebook """ user = validate_user() if user.type == "admin": homeworks = get_homework() else: homeworks = get_homeworks_before() # get all the grades, put them into a gradebook gradebook, max_scores = {}, {} for user in get_users(): if user.type == "student": gradebook[user] = {} for homework in homeworks: grades = session.query(Grade).filter_by(hw_id=homework.id).all() scores = [] for g in grades: if g.student not in gradebook: continue gradebook[g.student][homework.id] = g try: scores.append(float(g.score)) except: pass if homework.max_score is not None: max_scores[homework.id] = homework.max_score elif scores: max_scores[homework.id] = max(scores) else: max_scores[homework.id] = None gradebook = gradebook.items() gradebook.sort(key=lambda entry: convert_to_last_name(entry[0].name)) categories = session.query(Category).all() # calculate total scores by category, taking into account excused assignments for student, grades in gradebook: earned = {c: [] for c in categories} possible = {c: [] for c in categories} for hw in homeworks: if max_scores[hw.id] is None or max_scores[hw.id] == 0: continue possible[hw.category].append(max_scores[hw.id]) if hw.id in grades: if grades[ hw. id].excused: # if student was excused from assignment possible[hw.category].remove( max_scores[hw.id]) # don't count it against else: try: earned[hw.category].append(float(grades[hw.id].score)) except: earned[hw.category].append(0) else: earned[hw.category].append(0) # add grades to gradebook grades["overall"] = 0. for category, poss in possible.iteritems(): if len(poss) == 0: grades[category.name] = "0 / 0" continue # sort scores by benefit to grade if dropped e, p = sum(earned[category]), sum(poss) if len(poss) > category.drops + 1: grades_sorted = sorted(zip(earned[category], poss), key=lambda x: -(e - x[0]) / (p - x[1])) grades_sorted = grades_sorted[category.drops:] out = zip(*grades_sorted) else: out = earned[category], poss grades[category.name] = "%0.1f / %0.1f" % (sum(out[0]), sum( out[1])) if sum(out[1]) > 0: grades["overall"] += category.weight * sum(out[0]) / sum( out[1]) return gradebook, max_scores
def get_gradebook(): """ helper function that gets the gradebook """ user = validate_user() if user.type == "admin": homeworks = get_homework() else: homeworks = get_homeworks_before() # get all the grades, put them into a gradebook gradebook, max_scores = {}, {} for user in get_users(): if user.type == "student": gradebook[user] = {} for homework in homeworks: grades = session.query(Grade).filter_by(hw_id = homework.id).all() scores = [] for g in grades: if g.student not in gradebook: continue gradebook[g.student][homework.id] = g try: scores.append(float(g.score)) except: pass if homework.max_score is not None: max_scores[homework.id] = homework.max_score elif scores: max_scores[homework.id] = max(scores) else: max_scores[homework.id] = None gradebook = gradebook.items() gradebook.sort(key=lambda entry: convert_to_last_name(entry[0].name)) categories = session.query(Category).all() # calculate total scores by category, taking into account excused assignments for student, grades in gradebook: earned = {c: [] for c in categories} possible = {c: [] for c in categories} for hw in homeworks: if max_scores[hw.id] is None or max_scores[hw.id] == 0: continue possible[hw.category].append(max_scores[hw.id]) if hw.id in grades: if grades[hw.id].excused: # if student was excused from assignment possible[hw.category].remove(max_scores[hw.id]) # don't count it against else: try: earned[hw.category].append(float(grades[hw.id].score)) except: earned[hw.category].append(0) else: earned[hw.category].append(0) # add grades to gradebook grades["overall"] = 0. for category, poss in possible.iteritems(): if len(poss) == 0: grades[category.name] = "0 / 0" continue # sort scores by benefit to grade if dropped e, p = sum(earned[category]), sum(poss) if len(poss) > category.drops + 1: grades_sorted = sorted(zip(earned[category], poss), key=lambda x: -(e-x[0])/(p-x[1])) grades_sorted = grades_sorted[category.drops:] out = zip(*grades_sorted) else: out = earned[category], poss grades[category.name] = "%0.1f / %0.1f" % (sum(out[0]), sum(out[1])) if sum(out[1]) > 0: grades["overall"] += category.weight * sum(out[0]) / sum(out[1]) return gradebook, max_scores