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"
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)
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
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
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
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)
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
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]
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
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
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
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)
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
def authenticate(username, inserted_password): """ Authenticate a user checking his credentials :param username: the username inserted by the user :param inserted_password: the password inserted by the user :return: True if the credentials are valid, False if not """ session = start_session() queryset = session.query(User).filter(User.id.__eq__(username)) session.close() try: user = queryset2list(queryset)[0] if user.password == inserted_password: return True else: return False except IndexError: return False
def filter_cars_by_user_parameters(brand, car_type, n_seats, min_power, max_power, fuel, transmission, car_year_from, car_year_to, driver_age, min_price, max_price, date_from, date_to): """ Filters the cars available by the parameters inserted by the user :param brand: the brand inserted by the user :param car_type: the type of the car inserted by the user :param n_seats: the number of seats inserted by the user :param min_power: the car's minimum power inserted by the user :param max_power: the car's maximum power inserted by the user :param fuel: the type of fuel inserted by the user :param transmission: the car's transmission inserted by the user :param car_year_from: the lower car's year bound inserted by the user :param car_year_to: the upper car's year bound inserted by the user :param driver_age: the driver's minimum age inserted by the user :param min_price: the car's minimum price per day inserted by the user :param max_price: the car's maximum price per day inserted by the user :param date_from: the date in which the rent will start :param date_to: the date in which the rent will end :return: the list containing the cars that respect the parameters inserted by the user """ session = start_session() queryset = session.query(Car) if brand != 'all': queryset = filter_cars_by_brand(queryset, brand) if car_type != 'all': queryset = filter_cars_by_type(queryset, car_type) if n_seats != 'all': queryset = filter_cars_by_n_seats(queryset, n_seats) queryset = filter_cars_by_power(queryset, min_power, max_power) if fuel != 'all': queryset = filter_cars_by_fuel(queryset, fuel) if transmission != 'all': queryset = filter_cars_by_transmission(queryset, transmission) queryset = filter_cars_by_year(queryset, car_year_from, car_year_to) if driver_age != "none": queryset = filter_cars_by_driver_min_age(queryset, driver_age) queryset = filter_cars_by_price(queryset, min_price, max_price) results_list = queryset2list(queryset) if are_dates_valid(date_from, date_to): results_list = filter_cars_by_rent_period(results_list, date_from, date_to) return results_list
def check_authentication(session_id, username): """ Check if session_id and username can uniquely identified a user session previously created in oredr to authenticate a user :param session_id: the ID of the user session :param username: the user's email address :return: True if the user can authenticate, otherwise False """ session = start_session() queryset = session.query(UserSession).filter( UserSession.id_session.__eq__(session_id)) try: user_session = queryset2list(queryset)[0] if user_session.id_user == username: return True else: return False except IndexError: return False