def filtered_reservations(): try: json_data = request.get_json() restaurant_id = json_data['restaurant_id'] start_time = json_data['start_time'] end_time = json_data['end_time'] start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S") end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") reservations = ReservationManager.retrieve_by_date_and_time( restaurant_id, start_time, end_time) reservations = [ reservation.serialize() for reservation in reservations ] if not reservations: raise ValueError except Exception as e: return jsonify({ 'message': 'No reservation for those dates\n', 'status': 'Bad Request' }), 400 return jsonify({ 'status': 'Success', 'message': 'The reservations were correctly loaded', 'reservations': reservations }), 200
def reservation_all(restaurant_id): """Returns the whole list of reservations, given a restaurant. It also gives to the operator the opportunity to filter reservations by date, so it's possible to count people. Args: restaurant_id (int): univocal identifier of the restaurant Returns: The template of the reservations. """ filter_form = FilterForm() reservations = ReservationManager.retrieve_by_restaurant_id(restaurant_id) restaurant = RestaurantManager.retrieve_by_id(restaurant_id) people = 0 for r in reservations: if r.is_confirmed: people = people + r.people_number if request.method == 'POST': if filter_form.is_submitted(): filter_date = filter_form.data['filter_date'] start_time = filter_form.data['start_time'] end_time = filter_form.data['end_time'] if filter_date is not None and start_time is not None and end_time is not None: start_date_time = datetime.combine(filter_date, start_time) end_date_time = datetime.combine(filter_date, end_time) res = ReservationManager.retrieve_by_date_and_time( restaurant_id, start_date_time, end_date_time) people = 0 for r in res: if r.is_confirmed: people = people + r.people_number return render_template("restaurant_reservation.html", restaurant=restaurant, reservations=res, filter_form=filter_form, people=people) else: flash("The form is not correct") reservations.sort(key=lambda reservation: reservation.start_time) return render_template("restaurant_reservation.html", restaurant=restaurant, reservations=reservations, filter_form=filter_form, people=people)