def enrollment():

    if not session.get('username'):
        return redirect(url_for('login'))
    

    courseID = request.form.get('courseID')
    courseTitle = request.form.get('title')
    #user_id = 1 # temporary
    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")

    #below code got from mongo db database
    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, data={"id" : id, "title": title, "term":term})
    return render_template("enrollment.html", enrollment=True, title="Enrollment", classes = classes)
Ejemplo n.º 2
0
def enrollment():
    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):
            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 have succesfully enrolled in {courseTitle}!",
                  "success")

    classes = course_list(user_id)

    return render_template("enrollment.html",
                           enrollment=True,
                           title="Enrollment",
                           classes=classes)
Ejemplo n.º 3
0
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")

    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)
Ejemplo n.º 4
0
def enroll():
    print(session.keys(), session.values())
    if session.get('user_id') == None:
        return redirect(url_for('login'))
    current_user = session['user_id']
    user_name = session.get('user_name')
    title = request.form.get('title')
    id = request.form.get('id')
    print(id, current_user)
    term = request.form.get('term')
    data = []
    if id:
        classes = Enrollment.objects(user_id=current_user, courseID=id).first()
        if classes:
            #stopping duplicate enrollings
            flash(f'Hey {user_name}, you already enrolled in {title}',
                  'danger')
            return redirect(url_for('courses'))
    Enrollment(user_id=current_user, courseID=id).save()
    #classes=Enrollment.objects(user_id=user).all()
    data = list(
        User.objects.aggregate(*[{
            '$lookup': {
                'from': 'enrollment',
                'localField': 'user_id',
                'foreignField': 'user_id',
                'as': 'r1'
            }
        }, {
            '$unwind': {
                'path': '$r1',
                'includeArrayIndex': 'string',
                'preserveNullAndEmptyArrays': False
            }
        }, {
            '$lookup': {
                'from': 'course',
                'localField': 'r1.courseID',
                'foreignField': 'courseID',
                'as': 'r2'
            }
        }, {
            '$unwind': {
                'path': '$r2',
                'preserveNullAndEmptyArrays': False
            }
        }, {
            '$match': {
                'user_id': current_user
            }
        }, {
            '$sort': {
                'courseID': 1
            }
        }]))
    return render_template('enroll.html',
                           title="Registered Courses",
                           enroll=True,
                           courseData=data)
Ejemplo n.º 5
0
def enrollment():
    if not session.get('username'):
        return redirect(url_for("login"))
    #in get  method, use  request.args.get() and  in post method, use  request.form.get() or  request.form[] (must have some value) and methods=["GET", "POST"] in the route
    courseID = request.form.get('courseID')
    title = request.form.get('title')
    description = request.form.get('description')
    credits1 = request.form.get('credits')
    term1 = request.form.get('term1')
    userid = session.get('userid')
    if courseID:
        if Enrollment.objects(userid=userid, courseID=courseID):
            # flash(f"You are already enrolled in {title} course","danger")
            return redirect(url_for("courses"))
        else:
            Enrollment(userid=userid, courseID=courseID).save()
            # flash(f"You are sucessfully enrolled in {title} course","success")

    classes = list(
        User.objects.aggregate(*[{
            '$lookup': {
                'from': 'enrollment',
                'localField': 'userid',
                'foreignField': 'userid',
                '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': {
                'userid': userid
            }
        }, {
            '$sort': {
                'courseID': 1
            }
        }]))

    return render_template("enrollment.html",
                           enrollment=True,
                           templatetitle="Enrollment",
                           classes=classes)
Ejemplo n.º 6
0
def enrollment():
    if not session.get("username"):
        return redirect(url_for("login"))

    #many to many relationship b/w user and courses
    courseID = request.form.get("id")
    #courseTitle =request.form["title"]
    courseTitle = request.form.get("title")
    #Another method to fetch data
    #[]--> very strict return error
    #get() return None if empty
    user_id = session.get("user_id")
    if courseID:
        if Enrollment.objects(user_id=user_id, courseID=courseID):
            flash("You are already registered in {}".format(courseTitle))
            return redirect(url_for("courses"))
        else:
            Enrollment(user_id=user_id, courseID=courseID).save()
            flash("Successfully Enrolled in {} course".format(courseTitle))

    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, classes=classes)
Ejemplo n.º 7
0
def enrollment():
    # If NOT Logged In : go to login
    if not session.get('username'):
        return redirect(url_for('login'))
    # If Logged In
    courseID = request.form.get("courseID")
    title = request.form.get("title")
    user_id = session.get('user_id')
    # Only if its from the page Courses, so we add the course to the existing Enrollment (in db)
    if courseID:
        if Enrollment.objects(user_id=user_id, courseID=courseID):
            flash(f"Oops ! You are already enrolled in this course {title}",
                  'danger')
            return redirect(url_for('courses'))
        else:
            Enrollment(user_id=user_id, courseID=courseID).save()
            flash('You are successfully enrolled ! ', '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)
Ejemplo n.º 8
0
def enrollment():
    if not session.get('user_id'):
        return redirect(url_for('login'))
    # id = request.args.get('courseID')   # Works when we use method="GET" inside courses html which calls enrollment method
    # title = request.args.get('title')
    # term = request.args.get('term')
    course_id_from_form = request.form.get('course_id')
    title_from_form = request.form.get('title')
    user_id_from_session = session.get('user_id')
    # Check if course is already enrolled by this user. If not, then enroll and take him to enrollment oage
    if course_id_from_form:
        if Enrollment.objects(user_id=user_id_from_session, course_id=course_id_from_form):
            flash(f"Oops! You are already enrolled in this course {title_from_form}!", "danger")
            return redirect(url_for('courses'))
        else:
            Enrollment(user_id=user_id_from_session, course_id=course_id_from_form).save()
            flash(f"You have been successfully enrolled in {title_from_form}!", "success")
    # Fetch list of enrolled courses by this user
    enrolled_courses = 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.course_id',
                'foreignField': 'course_id',
                'as': 'r2'
            }
        }, {
            '$unwind': {
                'path': '$r2',
                'preserveNullAndEmptyArrays': False
            }
        }, {
            '$match': {
                'user_id': user_id_from_session
            }
        }, {
            '$sort': {
                'course_id': 1
            }
        }
    ]))
    return render_template("enrollment.html", enrollment=True, title="Enrollment", enrolled_courses=enrolled_courses)
Ejemplo n.º 9
0
def enrollment():
    courseID = request.form.get('courseID')
    title = request.form.get('title')
    user_id = 2

    if courseID and title:
        #Avoid duplicate enrollment
        if Enrollment.objects(user_id=user_id, courseID=courseID):
            flash(f"Sorry, you are already enrolled in the course {title}.",
                  "danger")
            return redirect(url_for("courses"))
        else:
            Enrollment(user_id=user_id, courseID=courseID).save()
            flash(f"You are enrolled in {title}.", "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", classes=classes, enrollment=True)
Ejemplo n.º 10
0
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 enrolled in this course {courseTitle}', 'danger')
        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': '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', title='Enrollment', classes=classes, enrollment=True )
Ejemplo n.º 11
0
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'):
        flash("Login in to enroll or view enrolled courses !!","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):
            flash(f"Oops! You already Registered in {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)    
Ejemplo n.º 13
0
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 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)
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
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")
Ejemplo n.º 17
0
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))
Ejemplo n.º 18
0
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)
Ejemplo n.º 19
0
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")

    classes = course_list(user_id)
    return render_template("enrollment.html",
                           enrollment=True,
                           title="Enrollment",
                           classes=classes)
Ejemplo n.º 20
0
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
    )
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
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)
Ejemplo n.º 23
0
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)