Пример #1
0
def student_attendance_handler(conn):
    try:
        request = get_info_from_request(conn)
        attendance_str = None

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()
            cur.execute("SELECT attendance FROM courses WHERE name='%(name)s'" % {
                "name": request["course"]
            })
            attendance_str = cur.fetchone()[0]
            con.commit()
            cur.close()

        attendance = ast.literal_eval(attendance_str)

        result = dict()
        dates = attendance.keys()
        for date in dates:
            result[date] = 1 if request["device_address"] in attendance[date] else 0

        print(result)
        send_get_succsess(conn, json.dumps(result))

    except HttpError as error:
        raise error
def is_open_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})
            result = cur.fetchone()
            if result:
                cur.execute(
                    "SELECT owner_address FROM courses WHERE name='%(name)s'" %
                    {"name": course})
                owner_address = cur.fetchone()[0]
                send_get_succsess(
                    conn,
                    json.dumps({
                        "course": "open",
                        "owner_address": owner_address
                    }))
            else:
                send_get_succsess(conn, json.dumps({"course": "close"}))
            con.commit()
            cur.close()

    except HttpError as error:
        raise error
Пример #3
0
def courses_handler(conn):
    try:
        user_target = get_user_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": user_target})
            courses = cur.fetchone()
            send_get_succsess(conn, courses[0])
            con.commit()
            cur.close()

    except HttpError as error:
        raise error
def teacher_attendance_handler(conn):
    try:
        length = int(conn.headers.getheader('Content-Length'))
        course = conn.rfile.read(length)
        attendance_str = None

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()
            cur.execute("SELECT * FROM courses WHERE name='%(name)s'" % {
                "name": course
            })
            _, _, students_str, attendance_str = cur.fetchone()
            con.commit()
            cur.close()

        attendance = ast.literal_eval(attendance_str)
        students = ast.literal_eval(students_str)
        names = dict()
        for students_date in attendance:
            student_addresses = attendance[students_date].keys()
            for student_address in student_addresses:
                if attendance[students_date][student_address] not in names:
                    names[attendance[students_date][student_address]] = {student_address : 0}
                else:
                    name_size = len(names[attendance[students_date][student_address]])
                    names[attendance[students_date][student_address]].update({student_address : name_size})

        result = dict()
        dates = attendance.keys()
        for date in dates:
            result[date] = dict()
            for name in names:
                for student in names[name]:
                    result[date].update({name + (str(names[name][student]) if names[name][student] else "") : 
                        1 if (student in attendance[date] and attendance[date][student] == name) else 0})

        send_get_succsess(conn, json.dumps(result))

    except HttpError as error:
        raise error
Пример #5
0
def user_handler(conn):
    try:
        user_target = get_user_from_request(conn)
        user_type = None

        con = sql.connect('data/data.db')
        with con:
            cur = con.cursor()
            cur.execute("SELECT type FROM users WHERE id='%(id)s'" %
                        {"id": user_target})
            user_type = cur.fetchone()
            con.commit()
            cur.close()

        if user_type:
            send_get_succsess(conn, user_type[0])
        else:
            send_get_succsess(conn, "unknown")
    except HttpError as error:
        raise error
        """