def enrollment(): if not session.get('username'): return redirect('/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 registered in the course {courseTitle}!", "danger") return redirect('/courses') else: Enrollment(user_id=user_id, courseID=courseID).save() flash(f"You are enrolled in {courseTitle}!", "success") classes = list(User.objects.aggregate(*[ { '$lookup': { 'from': 'enrollment', 'localField': 'user_id', 'foreignField': 'user_id', 'as': 'enrolled1' } }, { '$unwind': { 'path': '$enrolled1', 'includeArrayIndex': 'enrolled1_id', 'preserveNullAndEmptyArrays': False } }, { '$lookup': { 'from': 'course', 'localField': 'enrolled1.courseID', 'foreignField': 'courseID', 'as': 'enrolled2' } }, { '$unwind': { 'path': '$enrolled2', 'preserveNullAndEmptyArrays': False } }, { '$match': { 'user_id': user_id } }, { '$sort': { 'courseID': 1 } } ])) return render_template('enrollment.html', enrollment=True, classes=classes)
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 are already enrolled in this course {courseTitle}!', 'danger') return redirect(url_for('courses')) else: Enrollment(user_id=user_id,courseID=courseID).save() flash(f'You are enrolled in {courseTitle}', 'success') classes = course_list(user_id) return render_template('enrollment.html', title='Enrollment', classes=classes)
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 are already registered in this course {courseTitle}", "danger") return redirect(url_for('courses')) else: Enrollment(user_id=user_id, courseID=courseID).save() flash(f"You are enrolled in {courseTitle}", "success") classes = course_list(user_id) return render_template("enrollment.html", enrollment=True, title="Enrollment", classes=classes)
def mycourse(): 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.query.filter_by(user_id=user_id, course_id=courseID).first(): flash( f"Oops! You are already registered in this course {courseTitle}!", "danger") return redirect(url_for("courses")) else: enroll = Enrollment(user_id=user_id, course_id=courseID) db.session.add(enroll) db.session.commit() flash(f"You are enrolled in {courseTitle}!", "success") ec = Enrollment.query.filter_by(user_id=user_id).all() courses = [ Course.query.filter_by(course_id=i.course_id).first() for i in ec ] return render_template("mycourse.html", mycourse=True, data=courses)
def enrollment(): courseID = request.form.get('courseID') courseTitle = request.form.get('courseTitle') user_id = 1 if courseID: if Enrollment.objects(user_id=user_id, courseID=courseID): flash(f"You are already registered in this course, {courseTitle}.", "danger") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID) flash(f"You are successfully enrolled in {courseTitle}!", "sucess") classes = None term = request.form.get('term') return render_template("enrollment.html", enrollment=True, classes=classes)
def unenroll(): if not session.get('username'): flash("You must be logged in to enroll in classes.", "danger") 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): Enrollment.objects(user_id=user_id, courseID=courseID).delete() flash( f"You have successfully unenrolled in course: {courseTitle} ", "success") classes = course_list(user_id) return render_template("enrollment.html", enrollment=True, title="Enrollment", classes=classes)
def enrollment(): courseID = request.form.get('courseID') courseTitle = request.form.get('title') user_id = 2 if courseID: if Enrollment.objects(user_id=user_id, courseID=courseID): flash( f"Oops! You are already registered in this course {courseTitle}!", "danger") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID).save() flash(f"You are enrolled in {courseTitle}!", "success") classes = None term = request.form.get("term") return render_template("enrollment.html", enrollment=True, title="Enrollment", classes=classes)
def enrollment(): if not session.get('username'): return redirect(url_for('login')) #When using GET instead of POST, use request.args.get instead of request.form.get() 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 already registered for {courseTitle}!", "danger") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID).save() flash(f"You have enrolled in {courseTitle}!", "success") courses = course_list() term = request.form.get('term') return render_template("enrollment.html", enrollment=True, classes=classes, title="Enrollment")
def training_enrollment(training_id): context = get_context() if not context['user_id']: return redirect(url_for('register')) training = Training.objects.get(training_id=training_id) if training: if Enrollment.objects(user_id=context['user_id'], training_id=training_id): flash(f"Ups! Kamu telah terdaftar di pelatihan {training.title}!", "danger") else: Enrollment(user_id=session.get('user_id'), training_id=training_id).save() mail.send_enrollment_info(context['email'], training.title) flash(f"Kamu berhasil terdaftar di pelatihan {training.title}!", "success") return redirect(url_for('training_detail', training_id=training_id))
def enrollment(): # If not signed in, return to login page if not session.get('username'): return redirect(url_for('login')) data = {} data['courseID'] = request.form.get('courseID') # We can access POST form data using request.form.get('name') OR request.form['name'] , data['courseTitle'] = request.form.get('title') # data['title'] = request.form['title'] # but if we use [], the name (i.e. title) must be present else it will crash. Using .get('title') will do automatically do the # error handeling for us. data['term'] = request.form.get('term') # Setting user_id from session varable user_id = session.get('user_id') # Checking if we are coming from course page or enrollment page if data['courseID']: if Enrollment.objects(user_id=user_id, courseID=data['courseID']): flash( f"It seems that you have already enrolled in this course {data['courseTitle']}!", "danger") return redirect(url_for('courses')) else: Enrollment(user_id=user_id, courseID=data['courseID']).save() flash( f"Congratulations! You are enrolled in {data['courseTitle']}.", "success") classes = course_list(user_id) return render_template('enrollment.html', enrollment=True, title="Enrollment", classes=classes)
def post_enrollments(event_id): event = Event.query.filter_by(id=event_id).first() if not event or event.seats <= len(event.enrollments): return make_response(jsonify({"status": "error"}), 400) email = get_jwt_identity() participant = Participant.query.filter_by(email=email).first() enrollment = Enrollment(event=event, participant=participant) try: db.session.add(enrollment) db.session.commit() return make_response( jsonify({"status": "success"}), 201 ) except IntegrityError as e: logger.error(e) return make_response( jsonify({"status": "error"}), 400 )
def login(): if session.get('username'): return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): email = form.email.data password = form.password.data user = User.objects(email=email.first() if user and user.get_password(password): flash(f"{user.first_name}, you are successfully logged in!", "success") session{'user_id'] = user.user_id session['username'] = user.first_name return redirect("/index") else: flash("Sorry, something went wrong.","danger") return render_template("login.html", title="Login", form=form, login=True ) @app.route("/logout") def logout(): session['user_id']=False session.pop('username', None) return redirect(url_for('index')) @app.route("/courses/") @app.route("/courses/<term>") def courses(term= None): if term is None: term = "Spring 2019" classes = Course.objects.order_by("+courseID") return render_template("courses.html", courseData=classes, courses = True, term=term ) @app.route("/register", methods=['POST', 'GET']) def register(): if session.get('username'): return redirect(url_for('index')) form = RegisterForm() if form.validate_on_submit(): user_id = User.objects.count() user_id += 1 email = form.email.data password = form.password.data first_name = form.first_name.data last_name = form.last_name.data user = User(user_id=user_id, email=email, first_name=first_name, last_name=last_name) user.set_password(password) user.save() flash('You are successfully registered', "success") return redirect(url_for('index')) return render_template("register.html", title="Register", form=form, register=True) @app.route("/enrollment", methods=["GET","POST"]) 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"Oops! You are already registered in this course {courseTitle}!", "danger") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID).save() flash(f"You are enrolled in {courseTitle}!", "success") courses = course_list() return render_template("enrollment.html", enrollment=True, title="Enrollment", classes=classes) # @app.route("/api/") # @app.route("/api/<idx>") # def api(idx=None): # if(idx == None): # jdata = courseData # else: # jdata = courseData[int(idx)] # return Response(json.dumps(jdata), mimetype="application/json") @app.route("/user") def user(): #User(user_id=1, first_name="Christian", last_name="Hur", email="*****@*****.**", password="******").save() #User(user_id=2, first_name="Mary", last_name="Jane", email="*****@*****.**", password="******").save() users = User.objects.all() return render_template("user.html", users=users)
def enrollment(): # if already logged in, redirect to homepage if not session.get("username"): return redirect(url_for("login")) # only see enrollment if you are logged in # form.get = less strict, if you have courseID return it, otherwise None courseID = request.form.get("courseID") # request.form["<VALUE>"] is strict, if you don't have it, app will throw stack trace # title = request.form["title"] courseTitle = request.form.get("title") user_id = session.get("user_id") # if you're coming from enrollment page, we don't need ot add anything if courseID: # then we can process the enrollment if Enrollment.objects(user_id=user_id, courseID=courseID): flash( f"Oops! You are already registered in this course {courseTitle}!", "danger", ) return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID).save() flash(f"You 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": {"courseID": 1}}, ] ) ) return render_template( "enrollment.html", enrollment=True, title="Enrollment", classes=classes )
def enrollment(): #if user already loged in jsut check for username if not session.get('username'): return redirect(url_for('login')) courseID = request.form.get('courseID') courseTitle = request.form.get('title') # user_id = 1 #for testing in future will be session varialbe v3.4 user_id = session.get('user_id') if courseID: #check if coming from enrollment page ID will be present in form if Enrollment.objects(user_id=user_id, courseID=courseID): flash( f"Error, you are already registered for this course {courseID}!", "danger") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, courseID=courseID).save() # SAVE data to data base flash( f"You are succesffully enrolled in course {courseTitle} {courseID}!", "success") #classes = None 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') removed in version not needed anymore. #return render_template("enrollment.html", enrollment = True, data={"id":course, "title":title, "term":term}) return render_template("enrollment.html", enrollment=True, title="Enrollment", classes=classes)
def enrollment(): # if user is not logged in, redirect to login page if not session.get('username'): return redirect(url_for('login')) course_id = request.form.get('course_id') course_title = request.form.get('title') term = request.form.get('term') user_id = session.get('user_id') if course_id: if Enrollment.objects(user_id=user_id, course_id=course_id): flash( f"Oops! You are already registered in this course {course_title}!", "danger") return redirect(url_for("courses")) else: Enrollment(user_id=user_id, course_id=course_id).save() flash(f"You are enrolled in {course_title}!", "success") # create aggregation pipeline to return classes the current user is enrolled in classes = list( User.objects.aggregate(*[{ '$lookup': { 'from': 'enrollment', 'localField': 'user_id', 'foreignField': 'user_id', 'as': 'r1' } }, { '$unwind': { 'path': '$r1', 'preserveNullAndEmptyArrays': False } }, { '$lookup': { 'from': 'course', 'localField': 'r1.course_id', 'foreignField': 'course_id', 'as': 'r2' } }, { '$unwind': { 'path': '$r2', 'preserveNullAndEmptyArrays': False } }, { '$match': { 'user_id': user_id } }, { '$sort': { 'course_id': 1 } }])) 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)