def one_booking(booking_id): """ Deals with one single booking that a user has made. Buttons are displayed on the booking.html page as follows. If booking is active and car is locked: Display the unlock car button for the user to unlock the car Dispay the cancel button for the user If booking is active and car is unlocked: Display return car button for the user :param booking_id: The booking_id is used to identify the perticular booking the user is interacting now with. :return: Re-directs to the home page with a success flash depending on what button the user clicked on, else flashses an error message. """ res = BookingApi.get_booking(booking_id) if "OK" not in res.status: flash("booking non-existant", "danger") return redirect(url_for('main.home')) unlockcarform = UnlockcarForm() cancelbookingform = CancelbookingForm() returncarform = ReturncarForm() enablefaceunlock = Enablefaceunlock() disablefaceunlock = Disablefaceunlock() booking_data = res.get_json() if booking_data['active']: submitted = [*request.form.keys()] for operation in submitted: if operation in BookingButtonOps.supported: getattr(BookingButtonOps, operation)(booking_id) bookingdata = BookingApi.get_booking(booking_id).get_json() if bookingdata.get('error'): flash("booking non-existant", "danger") return redirect(url_for('main.home')) cardata = CarsApi.get_car(bookingdata['car_id']).get_json() show_google_calender_button = False if not bookingdata.get('calendar_id'): show_google_calender_button = True return render_template( 'booking.html', show_google_calender_button=show_google_calender_button, booking=bookingdata, car=cardata, unlockcarform=unlockcarform, cancelbookingform=cancelbookingform, returncarform=returncarform, enablefaceunlock=enablefaceunlock, disablefaceunlock=disablefaceunlock) # etc
def submit_disablefaceunlock(booking_id): g.api_request_json = {'userface_status': False} res = BookingApi.patch_booking(booking_id) if "OK" not in res.status: flash("{}".format(res.get_json()['error']), 'danger') else: flash("face unlock disable", "success")
def submit_cancel(booking_id): # contact api res = BookingApi.delete_booking(booking_id) if "OK" in res.status: flash("Booking Cancelled", "primary") else: flash("{}".format(res.get_json()['error']), 'danger')
def one_car(car_id): ''' Redirects user to the car of their choice to be able to be booked :variable form: The booking form that is then sent to the html to be displayed :variable r: The booking API that enables the car to be booked specifically :variable g: Requests the car_id from the database using the api :type form: BookingForm :type r: BookingApi :type g: Object :return: Returns booking.html with the cars data being sent, as well as the form :rtype: HTML/CSS ''' form = BookingForm() # cancelbookingform = CancelbookingForm() if form.validate_on_submit(): g.api_request_json = { "car_id": car_id, "end_time": str(form.end_time.data) } r = BookingApi.new_booking() if "CREATED" in r.status: booking = r.get_json() return redirect( url_for('main.one_booking', booking_id=booking['id'])) else: flash("{}".format(r.get_json()['error']), 'danger') data = CarsApi.get_car(car_id).get_json() return render_template('cars.html', cars=data, form=form)
def submit_unlock(booking_id): # Updates the car lock status to false when the user unlocks the car. booking = BookingApi.get_booking(booking_id).get_json() g.api_request_json = {'locked': False} res = CarsApi.patch_car(booking['car_id']) if "OK" not in res.status: flash("{}".format(res.get_json()['error']), 'danger') else: flash("car unlocked", "success")
def submit_return(booking_id): # the state of the car True. # set booking active false, add end time to booking, lock car # set car locked by deactivating booking current_date = datetime.now() g.api_request_json = { 'active': False, 'end_time': str(current_date), } res = BookingApi.patch_booking(booking_id) if "OK" in res.status: flash("Booking Complete", "primary") else: flash("{}".format(res.get_json()['error']), 'danger')
def rental_history(): """ Rental History: This page will show you all about all the previous bookings of the entire system Booking Variable gets Jsonified bookings of all the previous bookings and active ones Returns a rendered history page. """ bookings = [] res = BookingApi.get_bookings() if "OK" in res.status: bookings = res.get_json() else: flash("{}".format(res.get_json()['error']), "danger") return render_template('rental_history.html', bookings=bookings)
def history_booking(): """ History: This page will show you all about all the previous bookings of the the current user Booking Variable gets Jsonified bookings of all the previous bookigns related to the user_id, that isn't active. Returns a rendered histroy page. """ bookings = [] res = BookingApi.get_bookings(query_params={'user_id': g.user.id}) if "OK" in res.status: bookings = res.get_json() else: flash("{}".format(res.get_json()['error']), "danger") if not res.get_json(): flash("You have no previous bookings", 'info') return render_template('history.html', bookings=bookings)
def add_google_calendar_id(): """ Add's the google calender_id to the bookings table in the database through the BookingsApi json_data variable: contains the Jsonified data of the request being sent. booking_id and calender_id: consist of the id's from the jsonified data Returns a success response if valid, else returns a error 400 resonse. """ try: json_data = request.get_json() booking_id = json_data['booking_id'] calendar_id = json_data['calendar_event_id'] g.api_request_json = {'calendar_id': calendar_id} res = BookingApi.patch_booking(booking_id) if "OK" in res.status: return make_response("success", 200) return make_response("failed", 400) except BaseException as e: return make_response("error {}".format(e), 400)