Example #1
0
def text_callback(text):
    db = DB()
    student_id = text.replace(" ", "")

    # QUERY to check if student exists
    find_student_query = (
        "SELECT COUNT(nfc_id) FROM students WHERE nfc_id = %s")
    find_student_value = ((str(
        student_id.decode('unicode_escape').encode('ascii', 'utf-8')), ))

    if db.get_data(find_student_query, find_student_value) == 0:

        # RETRIEVE Student info via the Artevelde API
        student_res_obj = db.get_student_info(student_id)

        #   QUERY to create new student
        put_new_student_query = (
            "INSERT INTO students (firstname, lastname, email, nfc_id) VALUES (%s,%s,%s,%s)"
        )
        put_new_student_value = (student_res_obj['Voornaam'],
                                 student_res_obj['Naam'],
                                 student_res_obj['Email'], student_id)

        db.write_data(put_new_student_query,
                      put_new_student_value)  # write new student to db

    # query FIND CLASSROOM
    find_class_query = ("SELECT id FROM classrooms WHERE title = %s")
    find_class_value = ((str(
        classroom.decode('unicode_escape').encode('ascii', 'utf-8')), ))

    # query WRITE NEW SCAN
    put_students_query = (
        "INSERT INTO classrooms__students (scan_date, scan_time, classroom_id, student_id, exam) VALUES (%s,%s,%s,%s,%s)"
    )
    put_students_value = (current_date, current_time,
                          db.get_data(find_class_query,
                                      find_class_value), student_id, exam)

    print(student_id)

    db.write_data(put_students_query, put_students_value)
Example #2
0
# Load txt file
if filename:
    with open(filename, 'r') as f:
        datastore = json.load(f)
    f.close()

# check if classroom is set
if datastore["classroom"] == "":
    print("no classroom has been set, what classroom is this?")

    data = {}

    # ask for classroom
    classroom = raw_input("")
    input = db.get_data(
        ("SELECT title FROM classrooms WHERE title = %s"),
        ((str(classroom.decode('unicode_escape').encode('ascii', 'utf-8')), )))

    # validate classroom in db
    while (str(input) == "None"):
        print("Classroom: " + classroom +
              " is wrong or doesn't exist. Use this notation for example: B20")
        classroom = raw_input("")
        input = db.get_data(
            ("SELECT title FROM classrooms WHERE title = %s"),
            ((str(classroom.decode('unicode_escape').encode('ascii',
                                                            'utf-8')), )))

    print("Classroom: " + classroom + " is set!")

    # update txt file with give classroom