示例#1
0
def enroll(student_id):
    search_param = request.args.get('q')
    student = get_object_or_404(Student, Student.student_id == student_id)

    if search_param:
        courses = Course.select().join(StudentEnrollment).where(
            StudentEnrollment.student != student
            & (
                Course.name.contains(search_param)
            )
        )
    else:
        courses = Course.select().where(
            Course.id.not_in(
                StudentEnrollment.select(
                StudentEnrollment.course
            ).join(Course).where(
                    StudentEnrollment.student == student
                )
            )
        )

    return render_template(
        'student/enroll.html',
        title="Enroll",
        student=student,
        courses=courses,
        search=search_param
    )
示例#2
0
def enroll_student(student_id):
    selected_courses = request.form.getlist('selected_courses')
    student = get_object_or_404(Student, Student.student_id == student_id)

    # FIXME: kill the operation if a nonexistant course id is found in the form
    # FIXME: use the database.commit() functionality here instead
    for course in Course.select().where(Course.id.in_(selected_courses)):
        enrollment = StudentEnrollment(
            student = student.id,
            course = course.id
        )
        enrollment.save()
    return redirect(student.absolute_url())