Esempio n. 1
0
def book(rego):
    car = system.get_car(rego)

    if car is None:
        abort(404)
    if request.method == 'POST':
        date_format = "%Y-%m-%d"
        location = Location(request.form['start'], request.form['end'])

        try:
            start_date = datetime.strptime(request.form['start_date'],
                                           date_format)
            end_date = datetime.strptime(request.form['end_date'], date_format)
            diff = end_date - start_date
            booking = system.make_booking(current_user, diff, car, location)

        except ValueError as error:
            print("error: {0}".format(error))
            error = "Date can not be empty"
            flash(error)
            return render_template('booking_form.html', car=car, error=error)
        except BookingError as error:
            flash(error.msg)
            return render_template('booking_form.html', car=car, error=error)

        if 'check' in request.form:
            fee = car.get_fee(diff.days)
            return render_template('booking_form.html',
                                   confirmation=True,
                                   form=request.form,
                                   car=car,
                                   fee=fee)
        elif 'confirm' in request.form:
            return render_template('booking_confirm.html', booking=booking)
    return render_template('booking_form.html', car=car)
Esempio n. 2
0
def book(rego):
    car = system.get_car(rego)

    if not car:
        abort(404)

    if request.method == 'POST':
        date_format = "%Y-%m-%d"
        start_date = datetime.strptime(request.form['start_date'], date_format)
        end_date = datetime.strptime(request.form['end_date'], date_format)

        num_days = (end_date - start_date).days + 1

        if 'check' in request.form:
            fee = car.get_fee(num_days)

            return render_template('booking_form.html',
                                   confirmation=True,
                                   form=request.form,
                                   car=car,
                                   fee=fee)

        elif 'confirm' in request.form:
            location = Location(request.form['start'], request.form['end'])
            booking = system.make_booking(current_user, num_days, car,
                                          location)

            return render_template('booking_confirm.html', booking=booking)

    return render_template('booking_form.html', car=car)
Esempio n. 3
0
def test_1():

    user_name = "Matt"
    password = "******"
    system.new_customer(Customer(user_name, password, 1531))
    user = system.validate_login(user_name, password)
    assert user != None

    car_name = "Tesla"
    car_model = "model x"
    rego = 0
    system.add_car(PremiumCar(car_name, car_model, str(rego)))
    car = system.get_car(str(rego))
    assert car.get_name() == car_name
    assert car.get_model() == car_model

    location = Location("Sydney", "Mel")
    assert location

    date_format = "%Y-%m-%d"
    start_time = datetime.strptime("2018-11-11", date_format)
    end_time = datetime.strptime("2018-12-12", date_format)

    diff = end_time - start_time

    booking = system.make_booking(current_user, diff, car, location)
    assert booking._period == diff
    assert booking._car == car
    assert booking.location == location
    assert car.get_bookings()[0]
    assert system._bookings[0]
Esempio n. 4
0
def book(rego):
    car = system.get_car(rego)
    if car is None:
        abort(404)
    if request.method == 'POST':
        date_format = "%Y-%m-%d"
        start_date = datetime.strptime(request.form['start_date'], date_format)
        end_date = datetime.strptime(request.form['end_date'], date_format)
        diff = (end_date - start_date).days
        if 'check' in request.form:
            fee = car.get_fee(diff)
            return render_template('booking_form.html',
                                   confirmation=True,
                                   form=request.form,
                                   car=car,
                                   fee=fee)
        elif 'confirm' in request.form:
            location = Location(request.form['start'], request.form['end'])
            try:
                booking = system.make_booking(current_user, diff, car,
                                              location)
                return render_template('booking_confirm.html', booking=booking)

            except BookingError as error:
                return render_template('booking_form.html', error=error.msg)

    return render_template('booking_form.html', car=car)
Esempio n. 5
0
def book(rego):
    car = system.get_car(rego)
    if car is None:
        abort(404)
    if request.method == 'POST':
        date_format = "%Y-%m-%d"
        
        try:
            start_date = convert_time(request.form['start_date'], 0)
            end_date = convert_time(request.form['end_date'], 1)
        except BookingError as err:
            error_string = err.msg
            return render_template('booking_form.html', car=car, errmsg = err.msg)

        diff = end_date - start_date
        if 'check' in request.form:
            fee = car.get_fee(diff.days)
            return render_template(
                'booking_form.html',
                confirmation=True,  
                form=request.form,
                car=car,
                fee=fee
            )
        elif 'confirm' in request.form:
            location = Location(request.form['start'], request.form['end'])
            booking = system.make_booking(current_user, diff, car, location)
            if isinstance(booking, BookingError):
                return render_template('booking_form.html', car=car, errmsg = booking.msg)

            return render_template('booking_confirm.html', booking=booking)
    return render_template('booking_form.html', car=car)
Esempio n. 6
0
def test_3():
    with pytest.raises(Exception) as err:
        user_name = "Taylor"
        password = "******"
        system.new_customer(Customer(user_name, password, 1531))
        user = system.validate_login(user_name, password)
        assert user != None

        car_name = "Tesla"
        car_model = "model x"
        rego = 0
        system.add_car(PremiumCar(car_name, car_model, str(rego)))
        car = system.get_car(str(rego))
        assert car.get_name() == car_name
        assert car.get_model() == car_model

        location = Location("Sydney", "Mel")
        assert location

        date_format = "%Y-%m-%d"
        start_time = datetime.strptime("", date_format)
        end_time = datetime.strptime("2018-11-10", date_format)

        diff = end_time - start_time

        booking = system.make_booking(current_user, diff, car, location)
        assert err.errors['start_date'] == 'The date is not valid'
Esempio n. 7
0
    def make_booking(self, customer, input_start_date, input_end_date, car,
                     start_location, end_location):
        # Prevent the customer from referencing 'current_user';
        customer = copy.copy(customer)
        ##check fileds are not empty
        if not start_location:
            raise BookingError("start_location",
                               "Specify a valid start loaction!")
        if not end_location:
            raise BookingError("end_location", "Specify a valid end loaction!")

        if not input_start_date:
            raise BookingError("start_date", "Specify a valid start date!")
        if not input_end_date:
            raise BookingError("end_date", "Specify a valid end date!")
            ##check end

        ##check end_date < start_date
        date_format = "%Y-%m-%d"
        start_date = datetime.strptime(input_start_date, date_format)
        end_date = datetime.strptime(input_end_date, date_format)
        if end_date < start_date:
            raise BookingError("period", "Specify a valid booking period!")
            ##check end

        ##all filed valid ,then make booking
        location = Location(start_location, end_location)
        new_booking = Booking(customer, start_date, end_date, car, location)
        self._bookings.append(new_booking)
        car.add_booking(new_booking)
        return new_booking
Esempio n. 8
0
def test_2():
    user_name = "Issav"
    password = "******"
    system.new_customer(Customer(user_name, password, 1531))
    user = system.validate_login(user_name, password)
    assert user != None

    car_name = "Tesla"
    car_model = "model x"
    rego = 0
    system.add_car(PremiumCar(car_name, car_model, str(rego)))
    car = system.get_car(str(rego))
    assert car.get_name() == car_name
    assert car.get_model() == car_model

    location = Location("Sydney", "Mel")
    assert location

    date_format = "%Y-%m-%d"
    start_time = datetime.strptime("2018-11-11", date_format)
    end_time = datetime.strptime("2018-11-10", date_format)

    diff = end_time - start_time

    booking = system.make_booking(current_user, diff, car, location)
    system = CarRentalSystem()
Esempio n. 9
0
 def test_premium_car(self):
     location = Location('A', 'B')
     date_format = "%Y-%m-%d"
     start_date = datetime.strptime("2018-04-12", date_format)
     end_date = datetime.strptime("2018-04-17", date_format)
     diff = end_date - start_date
     car = self.premium_car
     booking = self.system.make_booking(self.user, diff.days, car, location)
     assert booking is not None
     assert booking.booking_fee == 5 * 150 * 1.15
Esempio n. 10
0
 def test_large_car_long_period(self):
     location = Location('A', 'B')
     date_format = "%Y-%m-%d"
     start_date = datetime.strptime("2018-04-12", date_format)
     end_date = datetime.strptime("2018-04-22", date_format)
     diff = end_date - start_date
     car = self.large_car
     booking = self.system.make_booking(self.user, diff.days, car, location)
     assert booking is not None
     assert booking.booking_fee == 10 * 100 * 0.95
Esempio n. 11
0
def book(rego):
    car = system.get_car(rego)
    if car is None:
        abort(404)
    if request.method == 'POST':
        form_start_date = request.form['start_date']
        form_end_date   = request.form['end_date']

        if 'check' in request.form:
            try:
                period = system.check_booking_period(form_start_date, form_end_date)
            except BookingError as err:
                return render_template (
                    'booking_form.html',
                    form=request.form, #gives access to form for use in booking_form.html through jinja :
                    car=car,
                    error_message = err.error_message,
                    error = True
                )
            except ValueError as err:
                error_message = "Incorrectly formatted date. Required date format: YYYY-MM-DD"
                return render_template (
                    'booking_form.html',
                    form=request.form, #gives access to form for use in booking_form.html through jinja :
                    car=car,
                    error_message = error_message,
                    error = True
                )
            
            fee = car.get_fee(period)
 
            return render_template(
                'booking_form.html',
                confirmation=True,
                form=request.form,
                car=car,
                fee=fee,
            )
        elif 'confirm' in request.form:
            location = Location(request.form['start'], request.form['end'])
            period = system.check_booking_period(form_start_date, form_end_date)
            try:
                booking = system.make_booking(current_user, period, car, location)
            except BookingError as err:
                return render_template (
                    'booking_form.html',
                    form=request.form, #gives access to form for use in booking_form.html through jinja :
                    car=car,
                    error_message = err.error_message,
                    error = True
                )
            return render_template('booking_confirm.html', booking=booking)
    return render_template('booking_form.html', car=car)
Esempio n. 12
0
 def test_booking(self):
     location = Location('A', 'B')
     date_format = "%Y-%m-%d"
     start_date = datetime.strptime("2018-04-12", date_format)
     end_date = datetime.strptime("2018-04-12", date_format)
     diff = end_date - start_date
     car = self.system.get_car('0')
     assert car is not None
     booking = self.system.make_booking(self.user, diff.days, car, location)
     assert booking is not None
     assert booking.location == location
     assert booking.booking_fee == 0
Esempio n. 13
0
def book(rego):
    car = system.get_car(rego)

    if car is None:
        abort(404)
    if request.method == 'POST':

        try:
            errors = check_input_fields_booking(request.form['start_location'],
                                                request.form['end_location'],
                                                request.form['start_date'],
                                                request.form['end_date'])
            if errors:
                raise BookingException(errors)
            else:
                date_format = "%Y-%m-%d"
                start_date = datetime.strptime(request.form['start_date'],
                                               date_format)
                end_date = datetime.strptime(request.form['end_date'],
                                             date_format)
                diff = end_date - start_date
                if 'check' in request.form:
                    fee = car.get_fee(diff.days)
                    return render_template('booking_form.html',
                                           confirmation=True,
                                           form=request.form,
                                           car=car,
                                           fee=fee,
                                           errors={})
                elif 'confirm' in request.form:
                    #debug = open("debug.txt", 'a+')
                    #debug.write("IN BOOKING")
                    #debug.close()
                    location = Location(request.form['start_location'],
                                        request.form['end_location'])
                    booking = system.make_booking(current_user, diff.days, car,
                                                  location)

                    return render_template('booking_confirm.html',
                                           booking=booking,
                                           errors={})
        except BookingException as be:
            return render_template('booking_form.html',
                                   form=request.form,
                                   car=car,
                                   errors=be.errors)
    return render_template('booking_form.html', car=car, errors={})
Esempio n. 14
0
    def test_unsuccessful_booking_no_end_date_specified(self):
        with pytest.raises(Exception) as error:
            date_format = "%Y-%m-%d"
            date = datetime.strptime('2018-10-20', date_format).date()
            start_loc = "Start"
            end_loc = "End"
            start_date = "2018-5-23"
            end_date = ""
            location = Location(start_loc, end_loc)
            num_days = (end_date - start_date).days + 1

            car = self.system.cars[0]
            customer = self.system.login_customer('Matt', 'pass')

            original_booking_size = len(self.system._bookings)
            self.system.make_booking(customer, num_days, car, location,
                                     start_date, end_date, start_loc, end_loc)
Esempio n. 15
0
def book(rego):
    car = system.get_car(rego)

    if not car:
        abort(404)
    
    if request.method == 'POST':
        date_format = "%Y-%m-%d"
        
  
        s_date = request.form['start_date']
        e_date = request.form['end_date']
        if s_date == '':
            return render_template('booking_form.html', error = 'Specify a valid start date')
        elif e_date == '':
            return render_template('booking_form.html', error = 'Specify a valid end date')
           
        start_date  = datetime.strptime(request.form['start_date'], date_format)
        end_date    = datetime.strptime(request.form['end_date'],   date_format)
        start_loc = request.form['start']
        end_loc = request.form['end']
        
        num_days = (end_date - start_date).days + 1

        if 'check' in request.form:
            fee = car.get_fee(num_days)
            
            return render_template(
                'booking_form.html',
                confirmation=True,
                form=request.form,
                car=car,
                fee=fee
            ) 

        elif 'confirm' in request.form:
            location = Location(request.form['start'], request.form['end'])    
                   
            try:    
                booking  = system.make_booking(current_user, num_days, car, location, start_date, end_date, start_loc, end_loc)
            except BookingError as error:                 
                return render_template('booking_form.html', error = error.message)
            else:
                return render_template('booking_confirm.html', booking = booking)
            
    return render_template('booking_form.html', car=car)
Esempio n. 16
0
    def test_successful_make_booking(self):
        print("test_successful_make_booking")
        date_format = "%Y-%m-%d"
        date = datetime.strptime('2018-10-20', date_format).date()
        start_loc = "Start"
        end_loc = "End"
        start_date = "2018-5-20"
        end_date = "2018-5-23"
        location = Location(start_loc, end_loc)

        original_booking_size = len(self.system._bookings)

        car = self.system.cars[0]
        customer = self.system.login_customer('Matt', 'pass')
        self.system.make_booking(customer, 3, car, location, start_date,
                                 end_date, start_loc, end_loc)
        assert len(self.system._bookings) == original_booking_size + 1
        assert self.system._bookings[
            original_booking_size].location.pickup == start_loc
        assert self.system._bookings[
            original_booking_size].location.dropoff == end_loc
        assert self.system._bookings[original_booking_size].booking_fee == 150
Esempio n. 17
0
def book(rego):
    car = system.get_car(rego)
    if car is None:
        abort(404)
    if request.method == 'POST':
        date_format = "%Y-%m-%d"
        """
        try:
            if not request.form.get('start_date', ''):
                raise BookingError("start date", "Specify a valid start date")
            if not request.form.get('end_date', ''):
                raise BookingError("end date", "Specify a valid end date")
        except BookingError as errmsg:
            msg = "Error with " + errmsg.name + ": " + errmsg.msg
            return render_template('booking_form.html', car=car, err=msg)
        else:
        """

        start_date = datetime.strptime(request.form['start_date'], date_format)
        end_date = datetime.strptime(request.form['end_date'], date_format)
        diff = end_date - start_date
        if 'check' in request.form:
            fee = car.get_fee(diff.days)
            return render_template('booking_form.html',
                                   confirmation=True,
                                   form=request.form,
                                   car=car,
                                   fee=fee)
        elif 'confirm' in request.form:
            location = Location(request.form['start'], request.form['end'])
            booking = system.make_booking(current_user, diff, car, location)
            if isinstance(booking, str):
                return render_template('booking_form.html',
                                       car=car,
                                       err=booking)
            return render_template('booking_confirm.html', booking=booking)
    return render_template('booking_form.html', car=car)
Esempio n. 18
0
from src.CarRentalSystem import CarRentalSystem
from src.Location import Location
from src.Customer import Customer
from src.Car import Car, SmallCar
from src.Booking import BookingError
import pytest

# Tests for function "CarRentalSystem.make_booking()"
system = CarRentalSystem()

customer            = Customer("username", "password", "license")
empty_start_date    = ""
empty_end_date      = ""
car                 = SmallCar("name", "model", "rego")
location_no_pickup  = Location("", "dropoff")
location_no_dropoff = Location("pickup", "")
location            = Location("pickup", "dropoff")

def test_empty_input_1():
    with pytest.raises(BookingError) as err: 
        system.check_booking_period(empty_start_date, "2018-05-15")
    
def test_empty_input_2():
    with pytest.raises(BookingError) as err: 
        system.check_booking_period("2018-05-15", empty_end_date)

def test_empty_input_3():
    with pytest.raises(BookingError) as err: 
        system.make_booking(customer, 5, car, location_no_pickup)
    
def test_empty_input_4():
Esempio n. 19
0
 def test_location_coordinats(self):
     location = Location(1, 2)
     assert location.X() == 1, 'Location X error'
     assert location.Y() == 2, 'Location Y error'
Esempio n. 20
0
 def test_hash(self):
     loc = Location(1,1)
     assert type(hash(loc)) == int
Esempio n. 21
0
from datetime import datetime

def generate_car_list(car_names, car_models):
    car_list = []
    car_id = 0
    for car_name in car_names:
        for car_model in car_models:
            car_list.append(SmallCar(car_name, car_model, car_id))
            car_id += 1
            car_list.append(MediumCar(car_name, car_model, car_id))
            car_id += 1
            car_list.append(LargeCar(car_name, car_model, car_id))
            car_id += 1
            car_list.append(PremiumCar(car_name, car_model, car_id))
    return car_list

user = Customer('admin', 'pass', 10)
location = Location('start_location', 'end_location')
car_list = generate_car_list(('X', 'Y', 'Z'), ('A', 'B', 'C'))
date_format = "%Y-%m-%d"
start_date = datetime.strptime("2018-12-11", date_format)
end_date = datetime.strptime("2018-12-31", date_format)
diff = end_date - start_date

def test_booking():    
  for car in car_list:
    booking = Booking(user,diff,car,location)
    assert booking.location == location
    assert booking.customer == user
    assert booking.period == diff
Esempio n. 22
0
 def test_location_equal(self):
     location1 = Location(1, 2)
     location2 = Location(1, 2)
     location3 = Location(2, 3)
     assert location1 == location2
     assert location1 != location3
Esempio n. 23
0
 def test_location_distance_to(self):
     location1 = Location(0, 0)
     location2 = Location(3, 4)
     assert location1.DistanceTo(location2) == 5, 'Location Distance error'