def create_reservation(restaurant_id):
    """This method allows the customer to create a new reservation, on a specific restaurant,
    depending on its opening hours and the available tables

    Args:
        restaurant_id (int): univocal identifier of the restaurant
    
    """
    if current_user.type == 'customer':
        form = ReservationForm()
        restaurant = RestaurantManager.retrieve_by_id(restaurant_id)
        if request.method == 'POST':
            if form.validate_on_submit():
                start_date = form.data['start_date']
                start_time = form.data['start_time']
                people_number = form.data['people_number']
                start_time_merged = datetime.combine(start_date, start_time)
                table = validate_reservation(restaurant, start_time_merged,
                                             people_number)
                if table != False:
                    reservation = Reservation(current_user, table, restaurant,
                                              people_number, start_time_merged)
                    ReservationManager.create_reservation(reservation)
                    return redirect(
                        url_for('reservation.customer_my_reservation'))
                else:
                    flash(
                        "There aren't free tables for that hour or the restaurant is close"
                    )
            else:
                flash("Take a look to the inserted data")
        return render_template('create_reservation.html',
                               restaurant=restaurant,
                               form=form)
    return redirect(url_for('home.index'))
Пример #2
0
def create_reservation():
    """This method is used to create a new reservation
        Linked to route /reservation/ [POST]

    Returns: 
        Invalid request if the creation of the reservation is not successful
        A json specifying the info needed to render the reservation page otherwise
    """
    try:
        json_data = request.get_json()
        user_id = json_data['user_id']
        restaurant_id = json_data['restaurant_id']
        start_time = json_data['start_time']
        people_number = json_data['people_number']
        tables = json_data['tables']
        times = json_data['times']
        table_id, start_time = validate_reservation(tables, times, start_time,
                                                    people_number)
        if table_id is False:
            raise ValueError
        reservation = Reservation(user_id, table_id, restaurant_id,
                                  people_number, start_time)
        ReservationManager.create_reservation(reservation)
    except Exception as e:
        return jsonify({
            'status':
            'Bad request',
            'message':
            'The data provided were not correct.\n' + str(e)
        }), 400

    return jsonify({
        'status': 'Success',
        'message': 'Reservation succesfully added'
    }), 200
Пример #3
0
def edit_reservation(reservation_id):
    """Allows the customer to edit a single reservation,
    if there's an available table within the opening hours
    of the restaurant.
    Linked to route reservation/{reservation_id} [PUT]

    Args:
        reservation_id (int): univocal identifier of the reservation
        restaurant_id (int): univocal identifier of the restaurant

    Returns:
        Invalid request for wrong data or if the reservation doesn't exists
        The json of the edited reservation
    """
    try:
        json_data = request.get_json()
        start_time = json_data['start_time']
        people_number = json_data['people_number']
        tables = json_data['tables']
        times = json_data['times']
        old_reservation = ReservationManager.retrieve_by_id(reservation_id)
        user_id = old_reservation.user_id
        restaurant_id = old_reservation.restaurant_id
        ReservationManager.delete_reservation(old_reservation)
        table_id, start_time = validate_reservation(tables, times, start_time,
                                                    people_number)
        if table_id is False:
            ReservationManager.create_reservation(old_reservation)
            raise ValueError
        reservation = Reservation(user_id, table_id, restaurant_id,
                                  people_number, start_time)
        ReservationManager.create_reservation(reservation)
    except Exception as e:
        print("MUSCAAAAAAAAA")
        return jsonify({
            'status':
            'Bad request',
            'message':
            'The data provided were not correct.\n' + str(e)
        }), 400

    return jsonify({
        'status': 'Success',
        'message': 'Reservation succesfully added'
    }), 200