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 edit_reservation(restaurant_id, reservation_id):
    """Allows the customer to edit a single reservation,
    if there's an available table within the opening hours
    of the restaurant.

    Args:
        reservation_id (int): univocal identifier of the reservation
        customer_id (int): univocal identifier of the customer

    Returns:
        Redirects the view to the customer profile page.
    """
    form = ReservationForm()

    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)
            str_start_time = datetime.strftime(start_time_merged,
                                               "%Y-%m-%d %H:%M:%S")
            response = ReservationManager.edit_reservation(
                reservation_id, restaurant_id, str_start_time, people_number)
            if response.status_code != 200:
                flash(
                    "There aren't free tables for that hour or the restaurant is close"
                )
            else:
                flash("Reservation successfully updated")
    return redirect(url_for('auth.profile', id=current_user.id))
def customer_my_reservation():
    """Given the current user, this method returns all its reservations

    """
    form = ReservationForm()
    reservations = ReservationManager.retrieve_by_customer_id(current_user.id)
    reservations.sort(key=lambda reservation: reservation.timestamp,
                      reverse=True)
    return render_template('customer_reservations.html',
                           reservations=reservations,
                           form=form)
Пример #4
0
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
    
    """
    restaurant_id = int(restaurant_id)
    if current_user.type == 'customer':
        _, _, details = ReservationManager.get_restaurant_detatils(
            restaurant_id)
        restaurant = details['restaurant']
        restaurant_name = restaurant['name']
        form = ReservationForm()
        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)
                str_start_time = datetime.strftime(start_time_merged,
                                                   "%Y-%m-%d %H:%M:%S")
                user_id = current_user.id
                response = ReservationManager.create_reservation(
                    restaurant_id, user_id, str_start_time, people_number)
                if response.status_code != 200:
                    flash(
                        "There aren't free tables for that hour or the restaurant is close"
                    )
                    return redirect('/restaurants/' + str(restaurant_id))
                else:
                    return redirect(url_for('auth.profile',
                                            id=current_user.id))
            else:
                flash("Take a look to the inserted data")
        return render_template('create_reservation.html',
                               restaurant_name=restaurant_name,
                               form=form)
    return redirect(url_for('home.index'))
def edit_reservation(reservation_id, customer_id):
    """Allows the customer to edit a single reservation,
    if there's an available table within the opening hours
    of the restaurant.

    Args:
        reservation_id (int): univocal identifier of the reservation
        customer_id (int): univocal identifier of the customer

    Returns:
        Redirects the view to the customer profile page.
    """
    form = ReservationForm()
    reservation = ReservationManager.retrieve_by_customer_id(
        user_id=customer_id)[0]
    restaurant = RestaurantManager.retrieve_by_id(reservation.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.set_people_number(people_number)
                reservation.set_start_time(start_time_merged)
                reservation.set_table(table)
                ReservationManager.update_reservation(reservation)
            else:
                flash(
                    "There aren't free tables for that hour or the restaurant is closed"
                )
        else:
            flash("The form is not correct")

    return redirect(url_for('auth.profile', id=customer_id))
Пример #6
0
def my_profile():
    """This method allows the customer to see its personal page

    Returns:
        Redirects the view to personal page of the customer
    """
    reservations = ReservationManager.retrieve_by_customer_id(current_user.id)
    form = ReservationForm()
    social_form = AddSocialNumberForm()
    customer = CustomerManager.retrieve_by_id(current_user.id)
    restaurants = RestaurantManager.retrieve_all()

    return render_template('customer_profile.html', customer=customer,
                           reservations=reservations, restaurants=restaurants, 
                           form=form, social_form=social_form)
Пример #7
0
def profile(id):
    """This method allows the customer to see its personal page

    Args:
        id (int): univocal identifier of the customer

    Returns:
        Redirects the view to personal page of the customer
    """
    if current_user.id == id:
        form = ReservationForm()
        social_form = AddSocialNumberForm()

        # restaurants = RestaurantManager.retrieve_all()
        resp = ReservationManager.get_all_reservation_customer(id)
        if resp.status_code != 200:
            return render_template('customer_profile.html',
                                   social_form=social_form)

        json_response = resp.json()
        restaurants = []
        reservations = json_response['reservations']
        for res in reservations:
            # time reformat
            start_time = datetime.strptime(res['start_time'],
                                           "%Y-%m-%d %H:%M:%S")
            res['start_time'] = datetime.strftime(start_time, "%Y-%m-%d %H:%M")
            print()
            # restaurant details extraction
            rest_dict = {}
            restaurant_id = res['restaurant_id']
            _, _, details = ReservationManager.get_restaurant_detatils(
                restaurant_id)
            restaurant = details['restaurant']
            rest_dict['name'] = restaurant['name']
            rest_dict['address'] = restaurant['address']
            restaurants.append(rest_dict)

        return render_template('customer_profile.html',
                               reservations=reservations,
                               restaurants=restaurants,
                               form=form,
                               social_form=social_form)

    return redirect(url_for('home.index'))
Пример #8
0
def profile(id):
    """This method allows the customer to see its personal page

    Args:
        id (int): univocal identifier of the customer

    Returns:
        Redirects the view to personal page of the customer
    """
    if current_user.id == id:
        reservations = ReservationManager.retrieve_by_customer_id(id)
        form = ReservationForm()
        social_form = AddSocialNumberForm()
        customer = CustomerManager.retrieve_by_id(id)
        restaurants = RestaurantManager.retrieve_all()
        return render_template('customer_profile.html', customer=customer,
                               reservations=reservations, restaurants=restaurants, 
                               form=form, social_form=social_form)

    return redirect(url_for('home.index'))