def assignment_delete(assignment_pk): assignment = Assignment.get(pk=assignment_pk) if request.method == 'GET': return render_template('assignment_delete.html', assignment=assignment) if request.method == 'POST': assignment.delete() return redirect(url_for('assignments'))
def assignment_view(assignment_pk): assignment = Assignment.get(pk=assignment_pk) students = Student.all() grades = assignment.get_grades() g_by_student_pk = dict([(g.student_pk, g) for g in grades]) for s in students: s.grade = g_by_student_pk.get(s.pk) return render_template( 'assignment_view.html', assignment=assignment, students=students )
def assignment_view(assignment_pk): assignment = Assignment.get(pk=assignment_pk) students = Student.all() grades = assignment.get_grades() g_by_student_pk = dict([(g.student_pk, g) for g in grades]) for s in students: s.grade = g_by_student_pk.get(s.pk) return render_template('assignment_view.html', assignment=assignment, students=students)
def assignment_update(assignment_pk): assignment = Assignment.get(pk=assignment_pk) if request.method == 'GET': return render_template('assignment_update.html', assignment=assignment) elif request.method == 'POST': assignment.name = request.form['name'] assignment.description = request.form['description'] assignment.comment = request.form['comment'] assignment.due_date = request.form['due_date'] assignment.points = request.form['points'] assignment.is_public = request.form.get('is_public', False, bool) assignment.save() return redirect(url_for('assignment_view', assignment_pk=assignment.pk))
def assignment_grades_update(assignment_pk): assignment = Assignment.get(pk=assignment_pk) students = Student.all() grades = assignment.get_grades() # We decorate the student's with their grades. # Ideally, this would be done with a select_related type thing in the # model. At the SQL level. TODO g_by_student_pk = dict([(grade.student_pk, grade) for grade in grades]) for s in students: s.grade = g_by_student_pk.get(s.pk) if request.method == 'GET': return render_template( "assignment_grades_update.html", assignment=assignment, students=students ) # TODO: This POSt method seems cumbersome. Can it be fixed? if request.method == 'POST': for student in students: # These keys are first generated in the template as input tag # name attributes. points_key = "student_{0}_points".format(student.pk) comment_key = "student_{0}_comment".format(student.pk) try: points = request.form[points_key].strip() comment = request.form[comment_key].strip() except KeyError: # This will prevent a 400 status code from being returned if we # try to get data from the form about a student that didn't # exist when the form was created. continue try: points = int(points.strip()) except ValueError: points = None comment = comment.strip() if student.grade is None: student.grade = Grade( student_pk=student.pk, assignment_pk=assignment.pk, points=points, comment=comment ) else: student.grade.points = points student.grade.comment = comment student.grade.save() return redirect(url_for('assignment_view', assignment_pk=assignment_pk))
def assignment_grades_update(assignment_pk): assignment = Assignment.get(pk=assignment_pk) students = Student.all() grades = assignment.get_grades() # We decorate the student's with their grades. # Ideally, this would be done with a select_related type thing in the # model. At the SQL level. TODO g_by_student_pk = dict([(grade.student_pk, grade) for grade in grades]) for s in students: s.grade = g_by_student_pk.get(s.pk) if request.method == 'GET': return render_template("assignment_grades_update.html", assignment=assignment, students=students) # TODO: This POSt method seems cumbersome. Can it be fixed? if request.method == 'POST': for student in students: # These keys are first generated in the template as input tag # name attributes. points_key = "student_{0}_points".format(student.pk) comment_key = "student_{0}_comment".format(student.pk) try: points = request.form[points_key].strip() comment = request.form[comment_key].strip() except KeyError: # This will prevent a 400 status code from being returned if we # try to get data from the form about a student that didn't # exist when the form was created. continue try: points = int(points.strip()) except ValueError: points = None comment = comment.strip() if student.grade is None: student.grade = Grade(student_pk=student.pk, assignment_pk=assignment.pk, points=points, comment=comment) else: student.grade.points = points student.grade.comment = comment student.grade.save() return redirect(url_for('assignment_view', assignment_pk=assignment_pk))