示例#1
0
def validate_login(headers):
    if 'Cookie' not in headers:
        return 0, error(401, "You have to be logged in to see this page")
    else:
        token = headers['Cookie'].split("=")[-1]
        try:
            decoded = jwt.decode(token, app_secret, algorithms='HS256')
            new_user = User(userID=decoded["userID"], emailID=decoded["emailID"], name=decoded["name"])
            return 1, new_user
        except:
            return 0, error(200, "Either the user does not exist or the given password is wrong. Please Try again")
示例#2
0
def start_classroom_session():
    ret_code, user = validate_login(app.headers)
    if(ret_code == 0):
        return user
    else:
        if(app.method == "POST"):
            classID = app.form_data["classID"]
            live_class = LiveClass(classID=classID)
            status = live_class.start(sql_database)
            if(status == 0):
                return error(200, "The Class has already been started, please join the class at: /classrooms/{}/live".format(classID))
            else:
                return redirect("/classrooms/{}/live".format(classID))
        else:
            return error(405)
示例#3
0
def discussions(class_id):
    ret_code, user = validate_login(app.headers)
    if(ret_code == 0):
        return user
    else:
        ret_code, classroom_obj = validate_classroom(class_id, user)
        if(ret_code == 0):
            return classroom_obj
        # print("class ID is:", class_id)
        if app.method == "POST":
            if(user.userID == classroom_obj.creator_userID):
                form_data = app.form_data
                gd_topic = form_data["gd_topic"]
                gd = Group_discussion(classID=classroom_obj.classID, gd_topic=gd_topic)
                gd.add_gd(sql_database)
                # print("classroom_obj", classroom_obj)
                return redirect("/classrooms/{}/group_discussions".format(class_id))
            else:
                return error(200, "You are not a Instructor. Only instructors can create a Group Discussion on Classroom")
        else:
            group_discussions = classroom_obj.get_group_discussions(sql_database)
            gd_list = []
            for gd in group_discussions:
                gd_list.append({"gdID":gd.gdID, "classID":gd.classID, "gd_topic":gd.gd_topic})
            user_details = user_json(user)
            classroom_details = classroom_json(classroom_obj)
            if_class_live = LiveClass(classID=classroom_obj.classID).check_if_live(sql_database)
            return render_html("group_discussions.html", gd_list=gd_list, user_details = user_details, details=classroom_details, if_class_live = if_class_live)
示例#4
0
def access_classroom(class_id):
    ret_code, user = validate_login(app.headers)
    if(ret_code == 0):
        return user
    else:
        ret_code, classroom_obj = validate_classroom(class_id, user)
        if(ret_code == 0):
            return classroom_obj
        if app.method == "POST":
            if(user.userID == classroom_obj.creator_userID):
                form_data = app.form_data
                content = form_data["content"]
                tag = form_data["tag"]
                post = Post(classID=classroom_obj.classID, creator_userID=user.userID, content=content)
                post.add_post(sql_database)
                new_tag = Tag(tagName=tag, postID=post.postID, classID=classroom_obj.classID)
                new_tag.add_tag(sql_database)
                return redirect("/classrooms/{}".format(class_id))
            else:
                return error(200, "You are not a Instructor. Only instructors can post on Classroom")
        else:
            posts = classroom_obj.list_posts(sql_database=sql_database)
            user_details = user_json(user)
            classroom_details = classroom_json(classroom_obj)
            post_list = []
            for post in posts:
                tag_name = post.get_tag(sql_database)
                post_list.append({"postID":post.postID, "classID":post.classID, "timestamp":post.timestamp, "creator_userID":post.creator_userID, "content":post.content, "tag":tag_name})
            
            if_class_live = LiveClass(classID=classroom_obj.classID).check_if_live(sql_database)
            return render_html("class.html", posts=post_list[::-1], details=classroom_details, user_details = user_details, if_class_live = if_class_live)
示例#5
0
def join_live_class(classID):
    ret_code, user = validate_login(app.headers)
    if(ret_code == 0):
        return user
    else:
        if app.method == "GET":
            ret_code, classroom_obj = validate_classroom(classID, user)
            if(ret_code == 0):
                return classroom_obj
            live_class = LiveClass(classID=classID)
            if_live = live_class.check_if_live(sql_database)
            if not if_live:
                return error(400, "The class has not started yet.")
            user_attendance = Attendance(liveclassID=live_class.liveclassID, userID=user.userID, classID = classroom_obj.classID)
            user_attendance.mark_attendance(sql_database)
            user_details = user_json(user)
            classroom_details = Classroom(classID=classID).get_class_details(sql_database)
            return render_html("live_class.html", live_class = live_class, user_attendance = user_attendance, user_details=user_details, details=classroom_details, if_class_live = if_live)
        else:
            return error(405)
示例#6
0
def signup():
    if app.method == "GET":
        return render_html("signup.html")
    elif app.method == "POST":
        form_data = app.form_data
        name, email, passwd  = form_data["name"], form_data["email"], form_data["pwd"]
        new_user = User(emailID=email, password=passwd, name=name)
        user_id = new_user.add_user(sql_database)
        if(user_id == -1):
            return error(200, "The User Already exists. Please try another Email ID.")        
        jwt_token = jwt.encode({'emailID': email, 'userID': user_id, 'name': name}, app_secret, algorithm='HS256')
        return redirect(new_route="/classrooms", token=jwt_token.decode("utf-8"))
示例#7
0
def validate_classroom(classID, user):

    joined_classes = user.list_classrooms(sql_database)
    class_ids = [joined_class.classID for joined_class in joined_classes]
    if classID not in class_ids:
        return 0, error(200, "You are not a part of this classroom")
    classroom_obj = Classroom(classID=classID)
    for joined_class in joined_classes:
        if joined_class.classID == classID:
            classroom_obj = joined_class
            break
    return 1, classroom_obj
示例#8
0
def login():
    if app.method == "POST":
        form_data = app.form_data
        email, passwd  = form_data["email"], form_data["pwd"]
        user = User(password = passwd, emailID=email)
        ret = user.login_user(sql_database)
        if(ret == 0):
            return error(200, "Either the user does not exist or the given password is wrong. Please Try again")
        user_id = ret[0]
        name = ret[1]
        jwt_token = jwt.encode({'emailID': email, 'userID': user_id, 'name': name}, app_secret, algorithm='HS256')
        return redirect(new_route="/classrooms", token=jwt_token.decode("utf-8"))
    else:
        return render_html("login.html")
示例#9
0
def end_classroom_session():
    ret_code, user = validate_login(app.headers)
    if(ret_code == 0):
        return user
    else:
        if(app.method == "POST"):
            classID = app.form_data["classID"]
            live_class = LiveClass(classID=classID)
            if_live = live_class.check_if_live(sql_database)
            if if_live:
                status = live_class.end_class(sql_database)
            return redirect("/classrooms/{}".format(classID))
        else:
            return error(405)
示例#10
0
def show_attendace(classID):
    ret_code, user = validate_login(app.headers)
    if(ret_code == 0):
        return user
    else:
        if app.method == "GET":
            ret_code, classroom_obj = validate_classroom(classID, user)
            if(ret_code == 0):
                return classroom_obj
            user_details = user_json(user)
            attendance = classroom_obj.get_attendance(sql_database)
            classroom_details = classroom_json(classroom_obj)
            if_class_live = LiveClass(classID=classroom_obj.classID).check_if_live(sql_database)
            return render_html("attendance.html", final_tally = attendance, user_details=user_details, details=classroom_details, if_class_live = if_class_live)
        else:
            return error(405)
示例#11
0
def access_students(class_id):
    ret_code, user = validate_login(app.headers)
    if(ret_code == 0):
        return user
    else:
        ret_code, classroom_obj = validate_classroom(class_id, user)
        if(ret_code == 0):
            return classroom_obj
        if(user.userID == classroom_obj.creator_userID):
            students_list = classroom_obj.list_students(sql_database)
            user_details = user_json(user)
            classroom_details = classroom_json(classroom_obj)
            if_class_live = LiveClass(classID=classroom_obj.classID).check_if_live(sql_database)
            return render_html("students.html", students_list = students_list, details=classroom_details, user_details = user_details, if_class_live = if_class_live)
        else:
            return error(200, "You are not an Instructor. Only instructors can view list of students in the Classroom")
示例#12
0
def access_discussion(gdID, class_id):
    ret_code, user = validate_login(app.headers)
    if(ret_code == 0):
        return user
    else:
        ret_code, classroom_obj = validate_classroom(class_id, user)
        if(ret_code == 0):
            return classroom_obj
        gd_list = classroom_obj.get_group_discussions(sql_database)
        gd_obj = None
        gdIDs = [gd.gdID for gd in gd_list]
        if gdID not in gdIDs:
            return error(200, "Group discussion not found in this classroom")
        for gd in gd_list:
            if gd.gdID == gdID:
                gd_obj = gd
                break
        instructor = (user.userID == classroom_obj.creator_userID)
        print("GD_ID is - ", gd_obj.gdID)
        if app.method == "POST":
            form_data = app.form_data
            private = form_data["if_private"]
            if(private == "1"):
                # Private message
                if instructor:
                    studentID = form_data["receiver"]
                    private_msg = form_data["private_msg"]
                    private_msg_obj = GD_message(gdID=gd_obj.gdID, sender_userID=user.userID, private=studentID, content=private_msg)
                    private_msg_obj.add_msg(sql_database)
                else:
                    private_msg = form_data["private_msg"]
                    private_msg_obj = GD_message(gdID=gd_obj.gdID, sender_userID=user.userID, private=classroom_obj.creator_userID, content=private_msg)
                    private_msg_obj.add_msg(sql_database)
            else:
                # Public message
                public_msg = form_data["public_msg"]
                public_msg_obj = GD_message(gdID=gd_obj.gdID, sender_userID=user.userID, private=-1, content=public_msg)
                public_msg_obj.add_msg(sql_database)
            return redirect("/classrooms/{}/group_discussions/{}".format(class_id, gd_obj.gdID))

        else:
            public_list, tmp_private_list = gd_obj.get_messages(sql_database)
            for priv_msg in tmp_private_list:
                user_tmp_obj = User(userID = priv_msg["sender_userID"])
                username_tmp = user_tmp_obj.get_name_of_user(sql_database)
                # print(username_tmp)
                receiver_tmp_obj = User(userID = priv_msg["private"])
                priv_msg["recvname"] = receiver_tmp_obj.get_name_of_user(sql_database)
                priv_msg["username"] = username_tmp
                priv_msg["senderID"] = priv_msg["sender_userID"]
            for pub_msg in public_list:
                user_tmp_obj = User(userID = pub_msg["sender_userID"])
                username_tmp = user_tmp_obj.get_name_of_user(sql_database)
                # print(username_tmp)
                pub_msg["username"] = username_tmp
            private_list = []
            if instructor:
                # teacher view
                private_list = tmp_private_list
            else:
                # student view
                private_list = []
                for priv_msg in tmp_private_list:
                    if priv_msg["sender_userID"] == user.userID or priv_msg["private"] == user.userID:
                        private_list.append(priv_msg)
            user_details = user_json(user)
            classroom_details = classroom_json(classroom_obj)
            if_class_live = LiveClass(classID=classroom_obj.classID).check_if_live(sql_database)
            return render_html("discussion.html", private_list = private_list, public_list = public_list, is_instructor = instructor, gd_topic=gd_obj.gd_topic, gdID = gd_obj.gdID, user_details = user_details, details=classroom_details, if_class_live = if_class_live)