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"Opps, you are already registered in this course {courseTitle}", "danger") return redirect(url_for('courses')) else: enrollment = Enrollment(user_id=user_id, courseID=courseID) enrollment.save() flash(f"You are enrolled in {courseTitle}", "success") classes = course_list() return render_template("enrollment.html", enrollment=True, title="Enrollment", classes=classes)
def enrollment(): if not session.get('first_name'): 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're already registered in this course { courseTitle }", "danger") return redirect(url_for('courses')) else: enrollment = Enrollment(user_id=user_id, courseID=courseID) enrollment.save() flash(f"You're enrolled in { 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': { 'r2.courseID': 1 } }])) # classes = list(Course.objects.aggregate(*[ # { # '$lookup': { # 'from': 'enrollment', # 'localField': 'courseID', # 'foreignField': 'courseID', # 'as': 'r1' # } # }, { # '$match': { # 'r1.user_id': user_id # } # } # ])) return render_template('enrollment.html', classes=classes, title="Enrollment", enrollment=True)