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
def confirm_reservation(reservation_id): """ This method is used to confirm reservation Linked to route /reservation/confirm/{reservation_id} [PUT] Args: reservation_id (Integer): the restaurant id of the reservation restaurant_id (Integer): univocal identifier of the restaurant Returns: Invalid request if the reservation doesn't exists A success message """ reservation = ReservationManager.retrieve_by_id(reservation_id) if reservation is None: return jsonify({ 'status': 'Bad Request', 'message': 'There is not reservation with this id' }), 400 reservation.set_is_confirmed() ReservationManager.update_reservation(reservation) return jsonify({ 'status': 'Success', 'message': 'Reservation succesfully confirmed' }), 200
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'))
def on_message(self, body, message): message_object = None try: message_object = json.loads(body) except ValueError: self.logger.error('Cannot decode json message! Message=%s' % body) message.ack() return if 'user_id' not in message_object: self.logger.error('Message does not contain user_id!') else: self.logger.info( 'Received a message of user deletion with user_id=%s', message_object['user_id']) from gooutsafe.dao.reservation_manager import ReservationManager try: ReservationManager.delete_all_user_reservation( user_id=message_object['user_id']) except Exception as re: self.logger.error( 'Runtime error during deleting all_user_reservations, %s' % re) # send ack to message message.ack()
def delete_reservation_customer(id, customer_id): """Given a customer and a reservation id, this function delete the reservation from the database. Args: id (int): univocal identifier for the reservation Returns: Redirects the view to the customer profile page. """ ReservationManager.delete_reservation_by_id(id) return redirect(url_for('auth.profile', id=customer_id))
def delete_reservation(id, restaurant_id): """This method deletes a specific reservation for a restaurant Args: id (int): univocal identifier of the reservation restaurant_id (int): univocal identifier of the restaurant Returns: Redirects the view to the general page of the reservation """ ReservationManager.delete_reservation_by_id(id) return redirect( url_for('reservation.reservation_all', restaurant_id=restaurant_id))
def confirm_reservation(res_id): """ This method is used to confirm reservation Args: res_id (Integer): the restaurant id of the reservation Returns: redirect: redirects to the reservations operator page """ reservation = ReservationManager.retrieve_by_id(res_id) reservation.set_is_confirmed() ReservationManager.update_reservation(reservation) return redirect(url_for('reservation.my_reservations'))
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)
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 get_all_reservation_restaurant(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. Linked to route /reservation/restaurant/{restaurant_id} [GET] Args: restaurant_id (int): univocal identifier of the restaurant Returns: Invalid request if restaurant doesn't exists The list of json of the reservations. """ try: reservations = ReservationManager.retrieve_by_restaurant_id( restaurant_id) reservations = [ reservation.serialize() for reservation in reservations ] if not reservations: raise ValueError except Exception as e: return jsonify({ 'message': 'No reservation for this restaurant\n', 'status': 'Bad Request' }), 400 return jsonify({ 'status': 'Success', 'message': 'The reservations were correctly loaded', 'reservations': reservations }), 200
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 delete_reservation(reservation_id): """This method is used to delete a reservation Linked to route /reservation/{reservation_id} [DELETE] Args: reservation_id (int): univocal identifier of the reservation Returns: Invalid request if the deletion of the reservation is not successful A json specifying the info needed to render the reservation page otherwise """ try: ReservationManager.delete_reservation_by_id(reservation_id) except Exception as e: return jsonify({ 'message': 'Error during avg stay updating\n' + str(e), 'status': 'Internal Server Error' }), 500 return jsonify({'message': 'Restaurant successfully deleted'}), 200
def __retrieve_by_customer_id_in_last_14_days(message): from gooutsafe.dao.reservation_manager import ReservationManager reservations = ReservationManager.retrieve_by_customer_id_in_last_14_days( message['customer_id']) reservations = [ reservation.serialize() for reservation in reservations ] return reservations
def __retrieve_all_contact_reservation_by_id(message): from gooutsafe.dao.reservation_manager import ReservationManager reservations = ReservationManager.retrieve_all_contact_reservation_by_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 get_reservation(reservation_id): reservation = ReservationManager.retrieve_by_id(reservation_id) if reservation is None: return jsonify({ 'message': 'No reservation for this id\n', 'status': 'Bad Request' }), 400 return jsonify({ 'status': 'Success', 'message': 'The reservation were correctly loaded', 'reservations': reservation.serialize() }), 200
def validate_reservation(restaurant, start_datetime, people_number): """ This method checks if the new reservation overlap with other already present for the restaurant. Args: restaurant (Restaurant): the reservation restaurant start_datetime (datetime): the datetime of the reservation people_number (Integer): number of people declered in the reservation Returns: Teble, Boolean: false in case there are overlap or a table if the restaurant is open and there aren't overlap """ avg_stay = restaurant.avg_stay if avg_stay is None: end_datetime = start_datetime + timedelta(hours=3) else: h_avg_stay = avg_stay // 60 m_avg_stay = avg_stay - (h_avg_stay * 60) end_datetime = start_datetime + timedelta(hours=h_avg_stay, minutes=m_avg_stay) print(start_datetime) print(end_datetime) if check_rest_ava(restaurant, start_datetime, end_datetime): tables = TableManager.retrieve_by_restaurant_id( restaurant.id).order_by(Table.capacity) for table in tables: if table.capacity >= people_number: reservation_table = table table_reservations = ReservationManager.retrieve_by_table_id( table_id=table.id) if len(table_reservations) != 0: for r in table_reservations: old_start_datetime = r.start_time old_end_datetime = r.end_time print(old_start_datetime) print(old_end_datetime) if start_datetime.date() == old_start_datetime.date(): if check_time_interval(start_datetime.time(), end_datetime.time(), old_start_datetime.time(), old_end_datetime.time()): continue else: return reservation_table else: return reservation_table else: return reservation_table else: continue return False
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'))
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 reservation_details(restaurant_id, reservation_id): """ Given a restaurant, this method returns all its reservations Args: restaurant_id (int): univocal identifier of the restaurant reservation_id (int): univocal identifier of the reservations Returns: [type]: [description] """ reservation = ReservationManager.retrieve_by_id(reservation_id) user = CustomerManager.retrieve_by_id(reservation.user.id) table = reservation.table restaurant = reservation.restaurant return render_template("reservation_details.html", reservation=reservation, user=user, table=table, restaurant=restaurant)
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
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 validate_reservation(tables, times, start_datetime, people_number): """ This method checks if the new reservation overlap with other already present for the restaurant. Args: restaurant (Restaurant): the reservation restaurant start_datetime (datetime): the datetime of the reservation people_number (Integer): number of people declered in the reservation Returns: Teble, Boolean: false in case there are overlap or a table if the restaurant is open and there aren't overlap """ start_datetime = datetime.strptime(start_datetime, "%Y-%m-%d %H:%M:%S") end_datetime = start_datetime + timedelta(hours=3) if not check_rest_ava(times, start_datetime): print('RISTORANTE CHIUSO') return False valid_tables = [ table for table in tables if table.get('capacity') >= people_number ] for table in valid_tables: reservation_table = table print("CONTROLLO PRENOTAZIONI PER IL TAVOLO " + str(table.get('id'))) table_reservations = ReservationManager.retrieve_by_date_time_table( table.get('id'), start_datetime, end_datetime) print("PRENOTAZIONI PRESENTI") print(table_reservations) if len(table_reservations) != 0: print('TAVOLO OCCUPATO') continue #return False else: print('TAVOLO DISPONIBILE') print('TAVOLO N ' + str(reservation_table.get('id'))) return reservation_table.get('id'), start_datetime return False