Exemplo n.º 1
0
def check_if_active(user, movie):
    ritorno = False
    user_id = get_data.get_user_id(user)
    movie_id = get_data.get_movie_id(movie)
    sql = "select booking_id from bookings where user_id = %d and movie_id = %d\
    and Actual_Return_Date is null"%(user_id, movie_id)
    db = dbutils.connect()
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        #Memorizzo il campo booking_id
        data = cursor.fetchone()
        #Se la query non restituisce nulla significa che il Film è già stato restituito
        if data is None:
            check = False
        #Altrimenti la Prenotazione è ancora attiva
        else:
            check = True
        #Chiudo la Connessione al DB
        db.close()
    #Se venissero lanciate Eccezioni
    except(MySQLdb.Error, MySQLdb.Warning) as e:
        print(e)
        db.close()
        print("Errore Database")
    return check
Exemplo n.º 2
0
def restituisci(user, titolo_film):
    active_booking = check_if_active(user, titolo_film)
    user_id = get_data.get_user_id(user)
    movie_id = get_data.get_movie_id(titolo_film)
    data_corrente = get_data.get_current_date()
    sql = "update bookings set Actual_Return_date = '%s' where user_id = %d and\
    movie_id = %d and Actual_Return_Date is null"%(data_corrente, user_id, movie_id)
    db = dbutils.connect()
    cursor = db.cursor()
    #Se il Film effettivamente non è stata ancora restituito
    if active_booking is True:
        try:
            cursor.execute(sql)
            #Con commit invio i dati alla Tabella
            db.commit()
            print("Hai restituito il film %s in data: %s"%(titolo_film, data_corrente))
            db.close()
        except(MySQLdb.Error, MySQLdb.Warning) as e:
            print(e)
            db.rollback()
            print("Errore Database")
            db.close()
    #Se invece è già stato restituito
    else:
        print("Impossibile restituire il Film specificato... Forse risulta già restituito?")
    return
Exemplo n.º 3
0
def print_active_bookings(username):
    user_id = get_data.get_user_id(username)
    sql = "select movies.movie_id, title, director, booking_date, return_date from users,\
    bookings, movies where movies.movie_id = bookings.movie_id and bookings.user_id = %d \
    and users.user_id = %d and Actual_Return_Date is null" % (user_id, user_id)
    db = dbutils.connect()
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        data = cursor.fetchall()
        for row in data:
            movie_id = row[0]
            title = row[1]
            director = row[2]
            booking_date = row[3]
            return_date = row[4]
            print(
                "ID Film: %d Titolo: %s Regista: %s Data Prestito: %s Data Restituzione: %s\
            " % (movie_id, title, director, booking_date, return_date))
        db.close()
    except (MySQLdb.Error, MySQLdb.Warning) as e:
        print(e)
        print("Error on displaying data...")
        db.close()
    return
Exemplo n.º 4
0
def borrow(user, titolo):
    user_id = get_data.get_user_id(user)
    movie_id = get_data.get_movie_id(titolo)
    booking_id = randint(2, 50)
    data_corrente = get_data.get_current_date()
    data_futura = get_data.get_future_date()
    sql = "insert into bookings (booking_id, user_id, movie_id, booking_date, return_date) values\
    (%d, %d, %d, '%s', '%s')"%(booking_id, user_id, movie_id, data_corrente, data_futura)
    db = dbutils.connect()
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        #Con commit invio i dati alla Tabella
        db.commit()
        print("Hai preso in prestito il film %s"%(titolo))
        db.close()
    except(MySQLdb.Error, MySQLdb.Warning) as e:
        print(e)
        print("Errore nell'aggiungere la prenotazione al DB")
        db.rollback()
        db.close()
    return