def enrollment(): if not session.get('username'): return redirect(url_for('login')) courseID = request.form.get('courseID') courseTitle = request.form.get('title') user_id = session.get('user_id') if courseID: if Enrollment.objects(user_id=user_id, courseID=courseID): flash(f"You had already registered for this course {courseTitle}", "warning") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID).save() flash(f"You have been successfully enrolled for {courseTitle}", "success") classes = list( User.objects.aggregate(*[{ '$lookup': { 'from': 'enrollment', 'localField': 'user_id', 'foreignField': 'user_id', 'as': 'r1' } }, { '$unwind': { 'path': '$r1', 'includeArrayIndex': 'r1_id', 'preserveNullAndEmptyArrays': False } }, { '$lookup': { 'from': 'course', 'localField': 'r1.courseID', 'foreignField': 'courseID', 'as': 'r2' } }, { '$unwind': { 'path': '$r2', 'preserveNullAndEmptyArrays': False } }, { '$match': { 'user_id': user_id } }, { '$sort': { 'courseID': 1 } }])) # term = request.form.get('term') return render_template("enrollment.html", title="Enrollment", enrollment=1, classes=classes)
def enrollment(): if not session.get("username"): flash(f"You need to log in to view your schedule.", "warning") return redirect(url_for("login")) courseID = request.form.get("courseID") courseTitle = request.form.get("title") user_id = session.get("user_id") if courseID: if Enrollment.objects(user_id=user_id, courseID=courseID): flash( f"Oops! You are already enrolled in the course {courseTitle}!", "warning") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID).save() flash(f"You are now enrolled in {courseTitle}!", "success") classes = list( User.objects.aggregate(*[{ '$lookup': { 'from': 'enrollment', 'localField': 'user_id', 'foreignField': 'user_id', 'as': 'result_1' } }, { '$unwind': { 'path': '$result_1', 'includeArrayIndex': 'string', 'preserveNullAndEmptyArrays': False } }, { '$lookup': { 'from': 'course', 'localField': 'result_1.courseID', 'foreignField': 'courseID', 'as': 'result_2' } }, { '$unwind': { 'path': '$result_2', 'preserveNullAndEmptyArrays': False } }, { '$match': { 'user_id': user_id } }, { '$sort': { 'courseID': 1 } }])) enrollment_page = render_template("enrollment.html", enrollment=True, classes=classes) return enrollment_page
def enrollment(): # If not signed in, goto login page if not session.get('username'): return redirect(url_for('login')) courseID = request.form.get("courseID") courseTitle = request.form.get("title") user_id = session.get('user_id') if courseID: if Enrollment.objects(user_id=user_id, courseID=courseID): flash("Oops, you are already registered in this course {}".format(courseTitle), "danger") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID).save() flash("You are enrolled in {}".format(courseTitle), "success") classes = course_list() return render_template( "enrollment.html", enrollment=True, title="Enrollment", classes=classes )