def change_user_type(): admin = validate_admin() stuid = request.form['user'] user_type = request.form['type'] session.query(User).filter_by(stuid=stuid).update({"type": user_type}) session.commit() return "Successfully changed user %s to %s." % (stuid, user_type)
def change_user_type(): admin = validate_admin() stuid = request.form['user'] user_type = request.form['type'] session.query(User).filter_by(stuid=stuid).update({ "type": user_type }) session.commit() return "Successfully changed user %s to %s." % (stuid, user_type)
def validate_admin(): stuid, name = auth() user = get_user(stuid) if not user.type == "admin": if user.stuid in options.admins: session.query(User).filter_by(stuid=user.stuid).update( {"type": "admin"}) session.commit() else: raise Exception("You are not authorized to view this page.") return user
def validate_admin(): stuid, name = auth() user = get_user(stuid) if not user.type == "admin": if user.stuid in options.admins: session.query(User).filter_by(stuid=user.stuid).update({ "type": "admin" }) session.commit() else: raise Exception("You are not authorized to view this page.") return user
def admin(): admin = validate_admin() # change back to admin view admin.proxy = admin.stuid session.commit() homeworks = get_homework() categories = session.query(Category).all() users = get_users() guests = [] admins = [] for user in users: if user.type == "guest": guests.append(user) elif user.type == "admin": admins.append(user) gradebook, max_scores = get_gradebook() return render_template("admin/index.html", homeworks=homeworks, guests=guests, admins=admins, categories=categories, gradebook=gradebook, max_scores=max_scores, options=options)
def download_peer_reviews(): admin = validate_admin() tasks = session.query(GradingTask).all() csv = "grader,student,question,score,comments,rating\n" for t in tasks: row = [] for item in [ t.grader, t.student, t.question_id, t.score, t.comments, t.rating ]: if item is not None: row.append('"' + str(item).replace('"', '""') + '"') else: row.append('""') csv += ",".join(row) + "\n" response = make_response(csv) course = options.title.replace(" ", "") filename = "%sPeerAssessments.csv" % course response.headers[ "Content-Disposition"] = "attachment; filename=%s" % filename return response
def download_grades(): admin = validate_admin() homeworks = get_homework() categories = session.query(Category).all() gradebook, max_scores = get_gradebook() csv = '"SUNet","Student","Overall",' csv += ','.join(('"' + c.name.replace('"', '""') + ' Total"') for c in categories) + "," csv += ','.join(('"' + hw.name.replace('"', '""') + '"') for hw in homeworks) + "\n" csv += ',"MAXIMUM",,' + ",".join("" for c in categories) + "," csv += ",".join(str(max_scores[hw.id]) for hw in homeworks) + "\n" for student, grades in gradebook: row = [student.stuid, student.name, str(grades["overall"])] for category in categories: row.append(str(grades[category.name])) for hw in homeworks: if hw.id not in grades: row.append("") else: row.append("E" if grades[hw.id].excused else str(grades[hw.id].score)) csv += ",".join(row) + "\n" response = make_response(csv) course = options.title.replace(" ", "") date = datetime.now().strftime("%m-%d-%Y") filename = "%sGrades%s.csv" % (course, date) response.headers["Content-Disposition"] = "attachment; filename=%s" % filename return response
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 change_user(): admin = validate_admin() student = request.args['user'] if not session.query(User).get(student): raise Exception("No user exists with the given ID.") admin.proxy = student session.commit() return redirect(url_for('hw_list'))
def hw_list(): user = validate_user() homeworks = get_homework() categories = session.query(Category).all() to_do = update_hw_grades(user, homeworks) session.commit() return render_template("list.html", homeworks=homeworks, categories=categories, user=user, options=options, current_time=pdt_now(), to_do=to_do)
def validate_user(): stuid, name = auth() try: user = get_user(stuid) except: type = "admin" if stuid in options.admins else "student" user = User(stuid=stuid, name=name, type=type) session.add(user) session.commit() if user.type == "admin" and user.proxy: user = session.query(User).get(user.proxy) return user
def update_category(): admin = validate_admin() name = request.form['name'] weight = float(request.form['weight']) drops = int(request.form['drops']) try: category_id = int(request.form['id']) category = session.query(Category).get(category_id) category.name = name category.weight = weight category.drops = drops except: session.add(Category(name=name, weight=weight, drops=drops)) session.commit() return "Category %s successfully added/updated." % name
def download_peer_reviews(): admin = validate_admin() tasks = session.query(GradingTask).all() csv = "grader,student,question,score,comments,rating\n" for t in tasks: row = [] for item in [t.grader, t.student, t.question_id, t.score, t.comments, t.rating]: if item is not None: row.append('"' + str(item).replace('"', '""') + '"') else: row.append('""') csv += ",".join(row) + "\n" response = make_response(csv) course = options.title.replace(" ", "") filename = "%sPeerAssessments.csv" % course response.headers["Content-Disposition"] = "attachment; filename=%s" % filename return response
def download_grades(): admin = validate_admin() homeworks = get_homework() categories = session.query(Category).all() gradebook, max_scores = get_gradebook() csv = '"SUNet","Student","Overall",' csv += ','.join(('"' + c.name.replace('"', '""') + ' Total"') for c in categories) + "," csv += ','.join( ('"' + hw.name.replace('"', '""') + '"') for hw in homeworks) + "\n" csv += ',"MAXIMUM",,' + ",".join("" for c in categories) + "," csv += ",".join(str(max_scores[hw.id]) for hw in homeworks) + "\n" for student, grades in gradebook: row = [student.stuid, student.name, str(grades["overall"])] for category in categories: row.append(str(grades[category.name])) for hw in homeworks: if hw.id not in grades: row.append("") else: row.append( "E" if grades[hw.id].excused else str(grades[hw.id].score)) csv += ",".join(row) + "\n" response = make_response(csv) course = options.title.replace(" ", "") date = datetime.now().strftime("%m-%d-%Y") filename = "%sGrades%s.csv" % (course, date) response.headers[ "Content-Disposition"] = "attachment; filename=%s" % filename return response
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