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 view_responses(): admin = validate_admin() q_id = request.args.get('q') responses = get_all_responses_to_question(q_id) return render_template("admin/view_responses.html", responses=responses, 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 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 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 update_max_score(): admin = validate_admin() hw_id = request.form['hw_id'] max_score = request.form['max_score'].strip() homework = get_homework(hw_id) homework.max_score = None if max_score == "" else float(max_score) session.commit() return "The maximum score for %s has been successfully updated!" % homework.name
def update_hw_name(): admin = validate_admin() hw_id = request.form['hw_id'] hw_name = request.form['hw_name'] homework = get_homework(hw_id) homework.name = hw_name session.commit() return '''The homework name was successfully updated to "%s"!''' % homework.name
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 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 update_response(): admin = validate_admin() response_id = request.form["response_id"] response = get_question_response(response_id) score = request.form["score"] response.score = float(score) if score else None response.comments = request.form["comments"] calculate_grade(response.user, response.question.homework) session.commit() return "Updated score for student %s to %f." % (response.stuid, response.score)
def update_question(): admin = validate_admin() q_id = request.form['q_id'] xml_new = request.form['xml'] node = ET.fromstring(xml_new) node.attrib['id'] = q_id question = Question.from_xml(node) return json.dumps({ "xml": question.xml, "html": question.to_html(), })
def update_due_date(): admin = validate_admin() hw_id = request.form['hw_id'] start_date = datetime.strptime(request.form['start_date'], "%m/%d/%Y %H:%M:%S") due_date = datetime.strptime(request.form['due_date'], "%m/%d/%Y %H:%M:%S") homework = get_homework(hw_id) homework.start_date = start_date homework.due_date = due_date session.commit() return "Due date for %s updated successfully!" % homework.name
def move_question(): admin = validate_admin() q_id = int(request.form['q_id']) hw_id = int(request.form['hw_id']) question = get_question(q_id) question.hw_id = hw_id if hw_id else None session.commit() if hw_id: return "Question ID %d moved to <a href=hw?id=%d>%s</a>." % (q_id, question.homework.id, question.homework.name) else: return "Question ID %d has been deleted!" % q_id
def move_question(): admin = validate_admin() q_id = int(request.form['q_id']) hw_id = int(request.form['hw_id']) question = get_question(q_id) question.hw_id = hw_id if hw_id else None session.commit() if hw_id: return "Question ID %d moved to <a href=hw?id=%d>%s</a>." % ( q_id, question.homework.id, question.homework.name) else: return "Question ID %d has been deleted!" % q_id
def add_question(): admin = validate_admin() xml = request.form['xml'] node = ET.fromstring(xml) # remove any ID tags for e in node.iter(): if "id" in e.attrib: e.attrib.pop("id") question = Question.from_xml(node) question.homework = get_homework(request.form['hw_id']) session.commit() return "Question added successfully!"
def import_homework(): admin = validate_admin() hw_id = request.form['hw_id'] hw = get_homework(hw_id) xml = request.files['xml'].read() if not xml: return "Empty file uploaded." try: hw.update_from_xml(xml) except: return "Invalid XML uploaded" return "Homework updated successfully from XML."
def export_homework(): admin = validate_admin() hw_id = request.args['id'] hw = get_homework(hw_id) # get XML for questions, wrap in <homework> tags xml = "\n\n".join(q.xml for q in hw.questions) xml = "<homework>\n" + xml + "\n</homework>" # get tree, remove any ID tags, convert back to string node = ET.fromstring(xml) for e in node.iter(): if "id" in e.attrib: e.attrib.pop("id") xml = ET.tostring(node) return Response(xml, content_type='text')
def add_homework(): admin = validate_admin() name = request.form['name'] start_date = datetime.strptime(request.form['start_date'], "%m/%d/%Y %H:%M:%S") due_date = datetime.strptime(request.form['due_date'], "%m/%d/%Y %H:%M:%S") category_id = request.form['category_id'] homework = Homework(name=name, start_date=start_date, due_date=due_date, category_id=category_id) session.add(homework) session.commit() return "%s added successfully!" % name
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 update_grade(): admin = validate_admin() stuid = request.form['stuid'] hw_id = request.form['hw_id'] score = request.form['score'].strip() excused = 1 if request.form['excused'] == "true" else 0 # check that score is valid try: float(score) except: assert(score in ["", "E"]) # fill in grades grade = get_grade(stuid, hw_id) if not grade: add_grade(get_user(stuid), get_homework(hw_id), score, excused) else: grade.score = score grade.excused = excused session.commit() return "Grade update successful!"
def update_grade(): admin = validate_admin() stuid = request.form['stuid'] hw_id = request.form['hw_id'] score = request.form['score'].strip() excused = 1 if request.form['excused'] == "true" else 0 # check that score is valid try: float(score) except: assert (score in ["", "E"]) # fill in grades grade = get_grade(stuid, hw_id) if not grade: add_grade(get_user(stuid), get_homework(hw_id), score, excused) else: grade.score = score grade.excused = excused session.commit() return "Grade update successful!"
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