def get_all_reservation_customer(customer_id):
    """Returns the whole list of reservations, given a customer.
    It also gives to the operator the opportunity to filter reservations
    by date, so it's possible to count people.
    Linked to route /reservation/customer/{customer_id} [GET]

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

    Returns:
        Invalid request if customer doesn't exists
        The list of json of the reservations.
    """
    reservations = ReservationManager.retrieve_by_customer_id(customer_id)
    reservations = [reservation.serialize() for reservation in reservations]
    if not reservations:
        return jsonify({
            'message': 'No reservation for this customer\n',
            'status': 'Bad Request'
        }), 400
    return jsonify({
        'status': 'Success',
        'message': 'The reservations were correctly loaded',
        'reservations': reservations
    }), 200
    def __retrieve_by_customer_id(message):
        from gooutsafe.dao.reservation_manager import ReservationManager
        reservations = ReservationManager.retrieve_by_customer_id(
            message['customer_id'])
        reservations = [
            reservation.serialize() for reservation in reservations
        ]

        return reservations
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)
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)
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'))
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))
def contact_tracing(contact_id):
    """This method allows the health authority to retrieve the list of
    contacts, given a positive user

    Args:
        contact_id (id): univocal id of the user

    Returns:
        Redirects the view to the health authority's home page
    """
    if current_user is not None and current_user.type == 'authority':
        customer = CustomerManager.retrieve_by_id(id_=contact_id)
        if customer is not None:
            pos_reservations = ReservationManager.retrieve_by_customer_id(
                user_id=customer.id)
            cust_contacts = []
            restaurant_contacts = []
            date_contacts = []
            for res in pos_reservations:
                contacts = ReservationManager.retrieve_all_contact_reservation_by_id(
                    res.id)
                for c in contacts:
                    cust = CustomerManager.retrieve_by_id(c.user_id)
                    cust_contacts.append(cust)
                    restaurant_contacts.append(
                        RestaurantManager.retrieve_by_id(c.restaurant_id).name)
                    date_contacts.append(c.start_time.date())
            return render_template('contact_tracing_positive.html',
                                   customer=customer,
                                   pos_contact=cust_contacts,
                                   res_contact=restaurant_contacts,
                                   date_contact=date_contacts)
        else:
            return redirect(
                url_for('auth.authority', id=current_user.id, positive_id=0))
    else:
        return redirect(url_for('home.index'))