Exemple #1
0
def unsubscribe_course_handler(conn):
    try:
        request = get_info_from_request(conn)

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()

            cur.execute("SELECT courses FROM users WHERE id='%(id)s'" %
                        {"id": request["device_address"]})
            courses_str = cur.fetchone()[0]
            courses = json.loads(courses_str)
            courses.remove(request["course"])

            cur.execute(
                "UPDATE users SET courses='%(courses)s' WHERE id='%(id)s'" % {
                    "courses": json.dumps(courses),
                    "id": request["device_address"]
                })

            con.commit()
            cur.close()

        send_post_succsess(conn, "Unsubscribed")
    except HttpError as error:
        raise error
def subscribe_handler(conn):
    try:
        request = get_info_from_request(conn)
        
        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()
            cur.execute("SELECT students FROM open_courses WHERE name='%(name)s'" % {
                "name": request["course"]
            })
            students_str = cur.fetchone()[0]
            students = ast.literal_eval(students_str)
            
            cur.execute("SELECT name FROM users WHERE id='%(id)s'" % {
                "id": request["device_address"]
            })
            student_name = cur.fetchone()[0]

            students.update({request["device_address"]: student_name})
            cur.execute("UPDATE open_courses SET students='%(students)s' WHERE name='%(name)s'" % {
                "students": json.dumps(students),
                "name": request["course"]
            })

            send_post_succsess(conn, "Subscribed")

            con.commit()
            cur.close()

    except HttpError as error:
        raise error
Exemple #3
0
def delete_course_handler(conn):
    try:
        length = int(conn.headers.getheader('Content-Length'))
        course = conn.rfile.read(length)

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()

            cur.execute("SELECT * FROM courses WHERE name='%(name)s'" %
                        {"name": course})
            name, owner_address, students_str, _ = cur.fetchone()

            cur.execute("DELETE FROM courses WHERE name='%(name)s'" %
                        {"name": course})

            cur.execute("SELECT courses FROM users WHERE id='%(id)s'" %
                        {"id": owner_address})
            courses_str = cur.fetchone()[0]
            courses = json.loads(courses_str)
            courses.remove(name)

            cur.execute(
                "UPDATE users SET courses='%(courses)s' WHERE id='%(id)s'" % {
                    "courses": json.dumps(courses),
                    "id": owner_address
                })

            students = json.loads(students_str)
            for student in students:
                cur.execute("SELECT courses FROM users WHERE id='%(id)s'" %
                            {"id": student})
                courses_str = cur.fetchone()
                if (not courses_str):
                    continue
                courses = json.loads(courses_str[0])
                courses.remove(name)

                cur.execute(
                    "UPDATE users SET courses='%(courses)s' WHERE id='%(id)s'"
                    % {
                        "courses": json.dumps(courses),
                        "id": student
                    })

            con.commit()
            cur.close()

        send_post_succsess(conn, "Course deleted")
    except HttpError as error:
        raise error
def create_course_handler(conn):
    try:
        course_info = get_course_info_from_request(conn)

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()

            cur.execute("SELECT * FROM courses WHERE name='%(name)s'" %
                        {"name": course_info["course_name"]})
            course = cur.fetchall()
            if (course):
                con.commit()
                cur.close()
                send_error_with_message(
                    conn, HttpError(404),
                    "This course has already been created")
                return

            cur.execute(
                "INSERT INTO courses VALUES ('%(name)s', '%(address)s', '[]', '{}')"
                % {
                    "name": course_info["course_name"],
                    "address": course_info["device_address"]
                })

            cur.execute("SELECT courses FROM users WHERE id='%(id)s'" %
                        {"id": course_info["device_address"]})
            courses_str = cur.fetchone()[0]
            courses = json.loads(courses_str)
            courses.append(course_info["course_name"])

            cur.execute(
                "UPDATE users SET courses='%(courses)s' WHERE id='%(id)s'" % {
                    "courses": json.dumps(courses),
                    "id": course_info["device_address"]
                })

            con.commit()
            cur.close()

        send_post_succsess(conn, "Course created")
    except HttpError as error:
        raise error
Exemple #5
0
def subscribe_course_handler(conn):
    try:
        course_info = get_course_info_from_request(conn)

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()

            cur.execute("SELECT students FROM courses WHERE name='%(name)s'" %
                        {"name": course_info["course_name"]})
            students_str = cur.fetchone()
            if (not students_str):
                con.commit()
                cur.close()
                send_error_with_message(conn, HttpError(404), "No such course")
                return

            students = ast.literal_eval(students_str[0])
            cur.execute(
                "UPDATE courses SET students='%(students)s' WHERE name='%(name)s'"
                % {
                    "students": json.dumps(students),
                    "name": course_info["course_name"]
                })

            cur.execute("SELECT courses FROM users WHERE id='%(id)s'" %
                        {"id": course_info["device_address"]})
            courses_str = cur.fetchone()[0]
            courses = ast.literal_eval(courses_str)
            courses.append(course_info["course_name"])

            cur.execute(
                "UPDATE users SET courses='%(courses)s' WHERE id='%(id)s'" % {
                    "courses": json.dumps(courses),
                    "id": course_info["device_address"]
                })

            con.commit()
            cur.close()

        send_post_succsess(conn, "Course subscribed")
    except HttpError as error:
        raise error
def open_course_handler(conn):
    try:
        course_info = get_course_info_from_request(conn)

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()
            cur.execute(
                "INSERT INTO open_courses VALUES ('%(name)s', '%(date)s', '{}')"
                % {
                    "name": course_info["course_name"],
                    "date": course_info["course_date"]
                })
            con.commit()
            cur.close()

        send_post_succsess(conn, "Course opened")
    except HttpError as error:
        raise error
Exemple #7
0
def registration_handler(conn):
    try:
        user_info = get_user_info_from_request(conn)

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()
            cur.execute(
                "INSERT INTO users VALUES ('%(id)s', '%(type)s', '%(name)s', '[]')"
                % {
                    "id": user_info["device_address"],
                    "type": user_info["user_type"],
                    "name": user_info["user_name"]
                })
            con.commit()
            cur.close()

        send_post_succsess(conn, "User created")
    except HttpError as error:
        raise error
def close_course_handler(conn):
    try:
        length = int(conn.headers.getheader('Content-Length'))
        course = conn.rfile.read(length)

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()

            cur.execute("SELECT * FROM open_courses WHERE name='%(name)s'" %
                        {"name": course})
            name, date, date_attendance = cur.fetchone()

            cur.execute("DELETE FROM open_courses WHERE name='%(name)s'" %
                        {"name": course})

            cur.execute(
                "SELECT attendance FROM courses WHERE name='%(name)s'" %
                {"name": course})
            attendance_str = cur.fetchone()[0]
            attendance = ast.literal_eval(attendance_str)
            attendance.update({date: ast.literal_eval(date_attendance)})
            print(attendance)

            cur.execute(
                "UPDATE courses SET attendance='%(attendance)s' WHERE name='%(name)s'"
                % {
                    "attendance": json.dumps(attendance),
                    "name": course
                })

            con.commit()
            cur.close()

        send_post_succsess(conn, "Course closed")
    except HttpError as error:
        raise error