예제 #1
0
    def get_available_cars(start_time, end_time):
        """
        This method is responsible for fetching all the available cars in the inventory.
        Args:
            start_time: Trip start date.
            end_time:  Trip end date.
        Return: 
            List of available cars at that time period.
        """
        car_data = []
        with terminating_sn() as session:
            # bookingStartDate > demandStartDate and bookingEndDate < demandEndate
            q1 = session.query(BookingDetails.car_id).filter(
                BookingDetails.start_time > start_time,
                BookingDetails.end_time < end_time).all()

            # bookingStartDate < demandStartDate and bookingEndDate < demandEndate
            q2 = session.query(BookingDetails.car_id).filter(
                BookingDetails.start_time < start_time,
                BookingDetails.end_time < end_time).all()

            # bookingStartDate < demandStartDate and demandEndate < bookingEndDate
            q3 = session.query(BookingDetails.car_id).filter(
                BookingDetails.start_time < start_time,
                end_time < BookingDetails.end_time).all()

            # bookingStartDate > demandStartDate and demandEndate > demandStartDate and demandEndate < bookingEndDate
            q4 = session.query(BookingDetails.car_id).filter(
                BookingDetails.start_time > start_time,
                end_time > BookingDetails.start_time,
                end_time < BookingDetails.end_time).all()

            booked_car = q1 + q2 + q3 + q4
            booked_car_set = set()
            for car in booked_car:
                booked_car_set.add(car.car_id)

            car_list = session.query(Cars).filter(
                Cars.id.notin_(booked_car_set)).all()

        if not car_list:
            return {
                "success": True,
                "status_code": success_status,
                "message": cars_not_available
            }

        for car in car_list:
            local_dict = {}
            local_dict['reg_number'] = car.reg_number
            local_dict['category'] = car.category
            local_dict['colour'] = car.colour
            local_dict['model'] = car.model
            local_dict['current_milage'] = car.current_milage
            local_dict['location'] = car.location

            car_data.append(local_dict)

        return car_data
예제 #2
0
def init_db():
    # Run Migrations
    with terminating_sn() as session:
        category = session.query(Categories).first()
        if not category:
            InitializeData.init_customers()
            InitializeData.init_category()
            InitializeData.init_common_price()
            InitializeData.init_car_details()
예제 #3
0
    def init_common_price():
        """
        This function is responsible for saving the price details in the DB.
        The list is globally declared.
        args: empty
        returns: Json with status and message.
        """
        with terminating_sn() as session:
            base_price = price_list[0]
            km_price = price_list[1]
            data = Price(base_price, km_price)
            session.add(data)
            session.commit()

        return {"success": True, "msg": "Initial price data migrated"}
예제 #4
0
    def book_ride(user, reg_number, start_time, end_time):
        """
        This method is to book the car on the mentioned dates.
        Args:
             user: user_id
             reg_number: car registraion number
             start_time: epoc time when to start the booking
             end_time: epoc time when to end the booking
        Return:
             JSON with success status.
        """

        renting_days = int((end_time - start_time) / (24 * 60 * 60))

        if renting_days < 0:
            return {
                "success": False,
                "status_code": bad_request_status,
                "message": input_not_valid
            }

        with terminating_sn() as session:
            car_id = session.query(
                Cars.id).filter(Cars.reg_number == reg_number)

            if not car_id.all():
                return {
                    "success": False,
                    "status_code": bad_request_status,
                    "message": input_not_valid
                }

            query = BookingDetails(rental_id=user,
                                   car_id=car_id,
                                   start_time=start_time,
                                   end_time=end_time,
                                   renting_days=renting_days)
            session.add(query)
            session.flush()
            session.commit()

        return {
            "success": True,
            "status_code": success_status,
            "msg": "Trip booked.",
            "booking_id": query.booking_number
        }
예제 #5
0
    def init_category():
        """
        This function is responsible for saving the categories record in the DB.
        The list is globally declared.
        args: empty
        returns: Json with status and message.
        """
        with terminating_sn() as session:
            for category in car_category_list:
                name = category[0]
                capacity = category[1]
                data = Categories(name, capacity)
                session.add(data)
            try:
                session.commit()
            except exc.IntegrityError as e:
                return {"success": False, "msg": e._message()}

        return {"success": True, "msg": "Initial data migrated"}
예제 #6
0
    def init_customers():
        """
        This function is responsible for saving the customer record in the DB.
        The list of customers is globally declared.
        args: empty
        returns: Json with status and message.
        """
        with terminating_sn() as session:
            for customer in customers_list:
                name = customer[0]
                address = customer[1]
                dob = customer[2]
                email = customer[3]
                cust = Customers(name, address, dob, email)
                session.add(cust)
            try:
                session.commit()
            except exc.IntegrityError as e:
                return {"success": False, "msg": e._message()}

        return {"success": True, "msg": "Initial Customers migrated"}
예제 #7
0
    def start_ride(booking_id, start_km):
        """
        This function is for starting the already booked trip and to move it to the intransit mode.
        Args:
             booking_id: Valid booking id.
             start_km: starting km information.
        Return:
            JSON with success status.
        """

        with terminating_sn() as session:
            query = session.query(BookingDetails).filter(BookingDetails.booking_number==int(booking_id)).\
            update({BookingDetails.start_km: float(start_km), BookingDetails.is_completed: 0})

            session.flush()
            session.commit()

        return {
            "success": True,
            "status_code": success_status,
            "msg": "Trip started."
        }
예제 #8
0
    def init_car_details():
        """
        This function is responsible for saving the car details in the DB.
        The list is globally declared.
        args: empty
        returns: Json with status and message.
        """
        with terminating_sn() as session:
            for car in cars_list:
                reg = car[0]
                category = car[1]
                colour = car[2]
                model = car[3]
                milage = car[4]
                location = car[5]
                data = Cars(reg, category, colour, model, milage, location)
                session.add(data)
            try:
                session.commit()
            except exc.IntegrityError as e:
                return {"success": False, "msg": e._message()}

        return {"success": True, "msg": "Initial data migrated"}
예제 #9
0
    def price_calculate(number_of_days, total_km, car_category):
        """
        This method is for calculating the total fare as per the car category.
        Args:
            number_of_days: total number of trip days.
            total_km: Total car driven in the trip.
            car_category: 
        """
        final_price = 0
        with terminating_sn() as session:
            base_price = session.query(Price.base_price).first()[0]
            km_price = session.query(Price.km_price).first()[0]

        # common for the all the categoies and only calculation for the compact category.
        trip_fare = base_price * number_of_days

        if car_category == 2:
            trip_fare = FareCalculations.premium_price(trip_fare, km_price,
                                                       total_km)
        elif car_category == 3:
            trip_fare = FareCalculations.minivan_price(trip_fare, km_price,
                                                       total_km)

        return trip_fare
예제 #10
0
    def end_ride(booking_id, end_km, current_milage, end_time):
        """
        This function is for ending the already started trip.
        Args:
             booking_id: Valid booking id.
             end_km: ending km information.
             current_milage: car's current millage noted from the car's dashboard.
             end_time: end time of the trip.
        Return:
            JSON with success status.
        """

        with terminating_sn() as session:
            booking_query = session.query(BookingDetails).\
                filter(BookingDetails.booking_number==int(booking_id)).\
                first()
            if not booking_query:
                raise Exception({
                    "success": False,
                    "status_code": bad_request_status,
                    "message": input_not_valid
                })

            if booking_query.is_completed == 1:
                raise Exception({
                    "success": False,
                    "status_code": bad_request_status,
                    "message": input_not_valid
                })
            car_id = booking_query.car_id
            start_time = booking_query.start_time

            car_query = session.query(Cars).filter(Cars.id == car_id).first()
            car_category = car_query.category
            start_km = booking_query.start_km
            number_of_days = int((end_time - start_time) / (24 * 60 * 60))
            total_km = end_km - start_km

            # To check if days and total_km are not in negative, to avoid fraud.
            FraudDetection.basic_fraud_detection(number_of_days, total_km)

            total_fare = FareCalculations.price_calculate(
                number_of_days, total_km, car_category)

            car_query.current_milage = current_milage
            booking_query.renting_days = number_of_days
            booking_query.end_km = end_km
            booking_query.end_km = end_km
            booking_query.is_completed = 1
            booking_query.total_fare = total_fare

            session.merge(car_query)
            session.merge(booking_query)
            session.commit()

        return {
            "success": True,
            "status_code": success_status,
            "msg": "Trip Completed.",
            "total_fare": total_fare
        }