Esempio n. 1
0
def save_car_reservation(car_id, username, date_from, date_to):
    """
    Save the reservation into the database
    :param car_id: the ID of the car selected for the rent
    :param username: the username of the user that subscribed the reservation
    :param date_from: the date in which the rent will start
    :param date_to: the date in which the rent will end
    :return: None
    """
    car = get_car_identified_by_id(car_id)
    price = calc_total_price(car.price, date_from, date_to)
    session = start_session()
    new_car_reservation = CarReservation(car_id, username, date_from, date_to,
                                         price)
    session.add(new_car_reservation)
    session.commit()
    queryset = session.query(CarReservation).filter(
        and_(CarReservation.id_car.__eq__(car_id),
             CarReservation.id_user.__eq__(username),
             CarReservation.date_from.__eq__(date_from),
             CarReservation.date_to.__eq__(date_to),
             CarReservation.price.__eq__(price)))
    reservation = queryset2list(queryset)[0]
    session.close()
    return reservation.id_reservation
Esempio n. 2
0
def add_car(brand, model, car_year, n_seats, car_type, engine, fuel, power,
            transmission, min_age, price, photo_name, preview):
    """
    Save a new car into the database
    :param brand: the car's brand
    :param model: the car's model
    :param car_year: the car's year
    :param n_seats: the number of seats of the car
    :param car_type: the type of the car
    :param engine: the engine displacement of the car
    :param fuel: the type of fuel of the car
    :param power: the power of the car
    :param transmission: the car's transmission
    :param min_age: the driver's minimum age for driving the car
    :param price: the price of the car
    :param photo_name: the link to the car's image
    :param preview:
    :return: True if the admin want to show car in the home page
    """
    photo_link = "/static/media/cars/" + photo_name
    session = start_session()
    new_car = Car(brand, model, car_year, n_seats, car_type, engine, fuel,
                  power, transmission, min_age, price, photo_link, preview)
    session.add(new_car)
    session.commit()
    queryset = session.query(func.max(Car.id).label("max_id"))
    res = queryset.one()
    session.close()
    return res.max_id
Esempio n. 3
0
def is_car_available_in_the_selected_period(date_from, date_to, car_id):
    """
    Check if the car identified by car_id is available in the selected period
    :param date_from: the date in which the rent will start
    :param date_to: the date in which the rent will end
    :param car_id: the car's ID
    :return: True if the car is available, False otherwise
    """
    session = start_session()
    queryset = session.query(CarReservation).filter(
        CarReservation.id_car.__eq__(car_id))
    reservations_list = queryset2list(queryset)
    try:
        date_from = datetime.strptime(date_from, '%Y-%m-%d')
        date_to = datetime.strptime(date_to, '%Y-%m-%d')
        is_available = True
        for reservation in reservations_list:
            if dates_intervals_are_overlapped(reservation.date_from,
                                              reservation.date_to,
                                              date_from.date(),
                                              date_to.date()):
                is_available = False
        return is_available
    except ValueError:
        return False
Esempio n. 4
0
def get_all_reservations_list(reservation_filter):
    """
    Get the list of reservations
    :param reservation_filter: the filter to apply on reservations during the query
    :return: a list containing objects of type CarReservation
    """
    today = datetime.datetime.today().date()
    session = start_session()
    if reservation_filter == "completed":
        queryset = session.query(CarReservation).filter(
            CarReservation.date_to < today)
    elif reservation_filter == "in-progress":
        queryset = session.query(CarReservation).filter(
            CarReservation.date_from <= today, CarReservation.date_to >= today)
    elif reservation_filter == "reserved":
        queryset = session.query(CarReservation).filter(
            CarReservation.date_from > today)
    elif reservation_filter == "starting-today":
        queryset = session.query(CarReservation).filter(
            CarReservation.date_from == today)
    elif reservation_filter == "ending-today":
        queryset = session.query(CarReservation).filter(
            CarReservation.date_to == today)
    else:
        queryset = session.query(CarReservation)
    session.close()
    reservations_list = queryset2list(queryset)
    return sorted(reservations_list,
                  key=lambda reservation: reservation.id_reservation,
                  reverse=True)
Esempio n. 5
0
def edit_user_info(name, surname, birthdate, old_username, new_username):
    """
    Edit the information associated to an acount
    :param name: the new user's name
    :param surname: the new user's surname
    :param birthdate: the new user's date of birth
    :param old_username: the old user's email address
    :param new_username: the new user's email address
    :return: "OK" if the information are successfully edited, otherwise it returns an error message
    """
    if is_blank_input(name) or is_blank_input(surname) or is_blank_input(
            birthdate) or is_blank_input(new_username):
        return "Error: The form has not been completely filled."
    if get_age(birthdate) < 18:
        return "Error: The age value must be greater than or equal to 18."
    session = start_session()
    user = session.query(User).get(old_username)
    if old_username != new_username:
        queryset = session.query(User).filter(User.id.__eq__(new_username))
        users_list = queryset2list(queryset)
        if users_list.__len__() == 0:
            user.id = new_username
        else:
            return "Error: There is already another user subscribed with this email address."
    else:
        pass
    user.surname = surname
    user.name = name
    user.birthdate = birthdate
    session.query(CarReservation).filter(
        CarReservation.id_user.__eq__(old_username)).update(
            {CarReservation.id_user: new_username}, synchronize_session=False)
    session.commit()
    session.close()
    return "OK"
Esempio n. 6
0
def get_max_car_power_value():
    """
    Get the car's maximum power available
    :return: the car's maximum power
    """
    session = start_session()
    queryset = session.query(func.max(Car.power).label("max_value"))
    res = queryset.one()
    return res.max_value
Esempio n. 7
0
def get_max_car_price_per_day():
    """
    Get the car's maximum price per day available
    :return: the car's maximum price per day available
    """
    session = start_session()
    queryset = session.query(func.max(Car.price).label("max_value"))
    res = queryset.one()
    return res.max_value
Esempio n. 8
0
def get_oldest_car_age():
    """
    Get the car's year of the oldest car
    :return: the car's year of the oldest car
    """
    session = start_session()
    queryset = session.query(func.min(Car.car_year).label("oldest"))
    res = queryset.one()
    return res.oldest
Esempio n. 9
0
def get_total_price(reservation_id):
    """
    Get the rent's total price given the reservation id
    :param reservation_id: the reservation's id
    :return: the total price for the rent saved into the database
    """
    session = start_session()
    reservation = session.query(CarReservation).get(reservation_id)
    return reservation.price
Esempio n. 10
0
def get_users_list():
    """
    Get the list of all users
    :return: a list containing objects of type User
    """
    session = start_session()
    queryset = session.query(User)
    session.close()
    return queryset2list(queryset)
Esempio n. 11
0
def delete_news(news_id):
    """
    Delete the news identified by news_id
    :param news_id: the ID of the new we want to delete
    :return: None
    """
    session = start_session()
    session.query(News).filter(News.id.__eq__(news_id)).delete()
    session.commit()
    session.close()
Esempio n. 12
0
def get_cars_list():
    """
    Get the list of all cars saved into the database
    :return: a list containing objects of type Car
    """
    session = start_session()
    queryset = session.query(Car)
    cars_list = queryset2list(queryset)
    session.close()
    return cars_list
Esempio n. 13
0
def get_user_by_id(user_id):
    """
    Get the user given his email address
    :param user_id: the user's ID
    :return: an object of type User
    """
    session = start_session()
    queryset = session.query(User).filter(User.id.__eq__(user_id))
    session.close()
    return queryset2list(queryset)[0]
Esempio n. 14
0
def get_reservation_identified_by_id(reservation_id):
    """
    Get the reservation identified by the ID
    :param reservation_id: the reservation's ID
    :return: the object of type Reservation associated to the ID
    """
    session = start_session()
    reservation = session.query(CarReservation).get(reservation_id)
    session.close()
    return reservation
Esempio n. 15
0
def get_cars_preview():
    """
    Get the cars to show in the home poge
    :return: the list of cars to show in the home page
    """
    session = start_session()
    queryset = session.query(Car).filter(Car.preview.__eq__(True))
    cars_list = queryset2list(queryset)
    session.close()
    return cars_list
Esempio n. 16
0
def save_news(news_content):
    """
    Save a new news into the database
    :param news_content: the text of the news
    :return: None
    """
    session = start_session()
    news = News(news_content)
    session.add(news)
    session.commit()
    session.close()
Esempio n. 17
0
def delete_all_user_reservations(user_id):
    """
    Delete all reservations associated to the user identified by user_id
    :param user_id: the user's ID
    :return: None
    """
    session = start_session()
    session.query(CarReservation).filter(
        CarReservation.id_user.__eq__(user_id)).delete()
    session.commit()
    session.close()
Esempio n. 18
0
def delete_user(user_id):
    """
    Delete the user identified by user_id
    :param user_id: the user's ID
    :return: None
    """
    session = start_session()
    session.query(User).filter(User.id.__eq__(user_id)).delete()
    session.commit()
    session.close()
    delete_all_user_reservations(user_id)
Esempio n. 19
0
def delete_reservation(reservation_id):
    """
    Delete the reservation identified by reservation_id
    :param reservation_id: the reservation's ID
    :return: None
    """
    session = start_session()
    session.query(CarReservation).filter(
        CarReservation.id_reservation.__eq__(reservation_id)).delete()
    session.commit()
    session.close()
Esempio n. 20
0
def get_news_list():
    """
    Get the news
    :return: a list containing all the news
    """
    session = start_session()
    queryset = session.query(News)
    news_list = queryset2list(queryset)
    session.close()
    return sorted(news_list, key=lambda news: news.id, reverse=True)
    return news_list
Esempio n. 21
0
def get_car_identified_by_id(id):
    """
    Get the car identified by the id
    :param id: the ID of the car we want to get
    :return: the desired object of type Car
    """
    session = start_session()
    queryset = session.query(Car).filter(Car.id.__eq__(id))
    car = queryset2list(queryset)[0]
    session.close()
    return car
Esempio n. 22
0
def delete_all_reservations_associated_to_car_id(car_id):
    """
    Delete all the reservations associated with the car identified by car_id
    :param car_id: the ID of the car we have deleted
    :return: None
    """
    session = start_session()
    session.query(CarReservation).filter(
        CarReservation.id_car.__eq__(car_id)).delete()
    session.commit()
    session.close()
Esempio n. 23
0
def delete_car(car_id):
    """
    Delete the car specified by car_id
    :param car_id: the ID of car we want to delete
    :return: None
    """
    session = start_session()
    session.query(Car).filter(Car.id.__eq__(car_id)).delete()
    session.commit()
    session.close()
    delete_all_reservations_associated_to_car_id(car_id)
Esempio n. 24
0
def delete_session(session_id):
    """
    Delete the user session identified by session_id
    :param session_id: the ID of the session we want to delete
    :return: None
    """
    session = start_session()
    session.query(UserSession).filter(
        UserSession.id_session.__eq__(session_id)).delete()
    session.commit()
    session.close()
Esempio n. 25
0
def edit_session(session_id, new_username):
    """
    Edit the username associated to a user session
    :param session_id: the ID of the session to edit
    :param new_username: the new username for the session
    :return: None
    """
    session = start_session()
    user_session = session.query(UserSession).get(session_id)
    user_session.id_user = new_username
    session.commit()
    session.close()
Esempio n. 26
0
def get_user_reservations_list(user_id):
    """
    Get the user's reservation list
    :param user_id: the user's id
    :return: the list containing objects of type CarReservation
    """
    session = start_session()
    queryset = session.query(CarReservation).filter(
        CarReservation.id_user.__eq__(user_id))
    reservations_list = queryset2list(queryset)
    return sorted(reservations_list,
                  key=lambda reservation: reservation.date_from,
                  reverse=True)
Esempio n. 27
0
def get_user_by_session_id(session_id):
    """
    Get the the user associated to the user session identified by session_id
    :param session_id: the ID of the session
    :return: the username of the user associated to the user session
    """
    session = start_session()
    queryset = session.query(UserSession).filter(
        UserSession.id_session.__eq__(session_id))
    try:
        user_session = queryset2list(queryset)[0]
        return user_session.id_user
    except IndexError:
        return None
Esempio n. 28
0
def generate_session(username):
    """
    Generate a new user session for the user identified by the username
    :param username: the user's email address
    :return: the session ID that identifies the user session just created
    """
    session_id = str(uuid.uuid1())
    session = start_session()
    session.query(UserSession).filter(
        UserSession.id_user.__eq__(username)).delete()
    user_session = UserSession(session_id, username)
    session.add(user_session)
    session.commit()
    session.close()
    return session_id
Esempio n. 29
0
def update_account_type(user_id, account_type):
    """
    Update the privileges of the user identified by user_id
    :param user_id: the ID of the user we want to change his privileges
    :param account_type: admin or standard user
    :return: None
    """
    session = start_session()
    user = session.query(User).get(user_id)
    if account_type == 'admin':
        user.is_admin = True
    else:
        user.is_admin = False
    session.commit()
    session.close()
Esempio n. 30
0
def has_user_age_requirement(username, car_id):
    """
    Check if the user identified by the username has the age to rent the car identified by car_id
    :param username: the user's email address
    :param car_id: the car's ID
    :return: True if the user has the age for renting the car, False otherwise
    """
    session = start_session()
    queryset = session.query(User).filter(User.id.__eq__(username))
    user = queryset2list(queryset)[0]
    queryset = session.query(Car).filter(Car.id.__eq__(car_id))
    car = queryset2list(queryset)[0]
    session.close()
    if get_age(str(user.birthdate)) >= car.min_age:
        return True
    else:
        return False