def get_tables(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 tables = RestaurantService.get_tables(restaurant_id) return list_obj_json("tables", tables)
def get_random_reviews(restaurant_id, number): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 reviews = RestaurantService.get_reviews_random(restaurant_id, number) return list_obj_json("Reviews", reviews)
def get_photos(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 photos = RestaurantService.get_photos(restaurant_id) return list_obj_json("photos", photos)
def get_openings(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 openings = RestaurantService.get_openings(restaurant_id) return list_obj_json("openings", openings)
def delete_restaurant(restaurant_id): if restaurant_id is None: return error_message("400", "restaurant_id not specified"), 400 restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 RestaurantService.delete_restaurant(restaurant_id) return _get_response("OK", 200)
def people_in(restaurant_id): openings = RestaurantService.get_openings(restaurant_id) if openings is None or len(openings) == 0: return {"lunch": 0, "dinner": 0, "now": 0} openings = BookingService.filter_openings(openings, datetime.today().weekday())[0] openings_model = OpeningHoursModel() openings_model.fill_from_json(openings) tables = RestaurantService.get_tables(restaurant_id) if tables is None or len(tables) == 0: return {"lunch": 0, "dinner": 0, "now": 0} tables_id = [table["id"] for table in tables] current_app.logger.debug("TABLES IDs: {}".format(tables_id)) reservations_l = (db_session.query(Reservation).filter( Reservation.table_id.in_(tables_id), extract("day", Reservation.reservation_date) == extract( "day", datetime.today()), extract("month", Reservation.reservation_date) == extract( "month", datetime.today()), extract("year", Reservation.reservation_date) == extract( "year", datetime.today()), extract("hour", Reservation.reservation_date) >= extract( "hour", openings_model.open_lunch), extract("hour", Reservation.reservation_date) <= extract( "hour", openings_model.close_lunch), ).all()) reservations_d = (db_session.query(Reservation).filter( Reservation.table_id.in_(tables_id), extract("day", Reservation.reservation_date) == extract( "day", datetime.today()), extract("month", Reservation.reservation_date) == extract( "month", datetime.today()), extract("year", Reservation.reservation_date) == extract( "year", datetime.today()), extract("hour", Reservation.reservation_date) >= extract( "hour", openings_model.open_dinner), extract("hour", Reservation.reservation_date) <= extract( "hour", openings_model.close_dinner), ).all()) reservations_now = (db_session.query(Reservation).filter( Reservation.checkin is True, Reservation.reservation_date <= datetime.now(), Reservation.reservation_end >= datetime.now(), ).all()) current_app.logger.debug("End of queries") return { "lunch": len(reservations_l), "dinner": len(reservations_d), "now": len(reservations_now), }, 200
def create_dish(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 body = request.get_json() dish = RestaurantService.create_dish(body["name"], body["price"], restaurant_id) return serialize(dish), 201
def create_table(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 body = request.get_json() table = RestaurantService.create_table(body["name"], body["max_seats"], restaurant_id) return serialize(table), 201
def create_review(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 body = request.get_json() review = RestaurantService.create_review(body["review"], body["stars"], body["reviewer_email"], restaurant_id) return serialize(review), 201
def create_menu_photo(menu_id): menu = RestaurantService.get_menu(menu_id) if menu is None: return error_message("404", "Menu not found"), 404 body = request.get_json() photo = RestaurantService.get_menu_photo_with_url(body["url"]) if photo is not None: return error_message("409", "URL already present"), 409 photo = RestaurantService.create_menu_photo(body["url"], body["caption"], menu_id) return serialize(photo), 201
def create_photo(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 body = request.get_json() photo = RestaurantService.get_photo_with_url(body["url"]) if len(photo) != 0: return error_message("409", "URL already present"), 409 photo = RestaurantService.create_restaurant_photo(body["url"], body["caption"], restaurant_id) return serialize(photo), 201
def get_restaurant_id_by_owner_email(owner_email): restaurant = RestaurantService.get_restaurants_by_owner_email(owner_email) if restaurant is None: return error_message("404", "Owner not found"), 404 else: return serialize(restaurant)
def get_all_bookings_restaurant(fromDate=False, toDate=False, restaurant_id=False): reservations = db_session.query(Reservation) tables = RestaurantService.get_tables(restaurant_id) ints = [table["id"] for table in tables] current_app.logger.debug("TABLES INTS: {}".format(ints)) reservations = reservations.filter(Reservation.table_id.in_(ints)) # Filtering stuff if fromDate is not False: reservations = reservations.filter( Reservation.reservation_date >= datetime.strptime( fromDate, "%Y-%m-%dT%H:%M:%SZ")) if toDate is not False: reservations = reservations.filter( Reservation.reservation_end <= datetime.strptime( toDate, "%Y-%m-%dT%H:%M:%SZ")) reservations = reservations.all() if reservations is None: return HttpUtils.error_message(404, "No Reservations") current_app.logger.debug("reservations len={}".format(len(reservations))) for i, reservation in enumerate(reservations): reservations[i] = BookingService.replace_with_customer(reservation) return BookingService.reservations_to_json(reservations, "customer"), 200
def get_table(table_id): if table_id is None: return error_message("400", "table not specified"), 400 table = RestaurantService.get_table(table_id) if table is None: return error_message("404", "Table not found"), 404 restaurant = RestaurantService.get_restaurant(table.restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 total_table = { "name": table.name, "max_seats": table.max_seats, "available": table.available, "id": table.id, "restaurant": serialize(restaurant) } return total_table
def get_menus(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 menus = RestaurantService.get_menus(restaurant_id) all_menus = [] # get menu photos for each menu for menu in menus: photos = RestaurantService.get_menu_photos(menu.id) json_menu = serialize(menu) photos_list = [] for photo in photos: photos_list.append(serialize(photo)) json_menu["photos"] = photos_list all_menus.append(json_menu) return json.loads(json.dumps({"menus": all_menus}))
def get_avg_rating_restaurant(restaurant_id): """ get avg of rating for a restaurant """ if restaurant_id is None: return error_message("400", "dish_id not specified"), 400 rating = RestaurantService.get_avg_rating_restaurant(restaurant_id) if rating == -1: return _get_response(rating, 404) else: return _get_response(rating, 200)
def update_restaurant_info(): """ update the restaurant infos """ data = request.get_json() result = RestaurantService.update_restaurant_info(data) if result is False: return error_message("500", "Restaurant data has not been modified."), 500 else: return _get_response("Restaurant data has been modified.", 200)
def create_restaurant(): body = request.get_json() # check if the restaurant already exists (phone number, lat, lon, name) name = body["restaurant"]["name"] phone = body["restaurant"]["phone"] lat = body["restaurant"]["lat"] lon = body["restaurant"]["lon"] # if the restaurant already exists: error if RestaurantService.get_restaurant_with_info(name, phone, lat, lon) is not None: return error_message("409", "Restaurant already exists"), 409 restaurant = RestaurantService.create_restaurant(body, _max_seats) json_resp = serialize(restaurant) if restaurant is None: return _get_response("An error occur during the restaurants creation", 500) current_app.logger.debug("Result is: {}".format(json_resp)) return _get_response(json_resp, 201, True)
def get_all_bookings(user_id=False, fromDate=False, toDate=False, restaurant_id=False): reservations = db_session.query(Reservation) # Filtering stuff if user_id is not False: current_app.logger.debug( "Adding reservation with filter by user id: {}".format(user_id)) reservations = reservations.filter(Reservation.customer_id == user_id) if fromDate is not False: current_app.logger.debug( "Adding reservation with filter from date: {}".format(fromDate)) reservations = reservations.filter( Reservation.reservation_date >= datetime.strptime( fromDate, "%Y-%m-%dT%H:%M:%SZ")) if toDate is not False: current_app.logger.debug( "Adding reservation with filter to date: {}".format(toDate)) reservations = reservations.filter( Reservation.reservation_end <= datetime.strptime( toDate, "%Y-%m-%dT%H:%M:%SZ")) if restaurant_id is not False: current_app.logger.debug( "Adding reservation with filter by restaurant id: {}".format( restaurant_id)) tables = RestaurantService.get_tables(restaurant_id) ints = [table["id"] for table in tables] current_app.logger.debug("TABLES INTS: {}".format(ints)) reservations = reservations.filter(Reservation.table_id.in_(ints)) reservations = reservations.all() if reservations is None: return HttpUtils.error_message(404, "No Reservations") current_app.logger.debug("reservations len={}".format(len(reservations))) for i, reservation in enumerate(reservations): reservations[i] = BookingService.replace_with_restaurant(reservation) current_app.logger.debug("adding people") listfriends = [] current_app.logger.debug("added empty list") friends = (db_session.query(Friend).filter_by( reservation_id=reservation.id).all()) current_app.logger.debug("Got friends: {}".format(len(friends))) for friend in friends: listfriends.append(friend.email.strip()) current_app.logger.debug("Frinds: {}".format(listfriends)) reservations[i].people = listfriends return BookingService.reservations_to_json(reservations), 200
def get_restaurant_more_info(restaurant_id): # get restaurant and check if restaurant exists restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 info = dict() info["Restaurant"] = serialize(restaurant) # get menus menus = RestaurantService.get_menus(restaurant_id) all_menus = [] # get menu photos for each menu for menu in menus: photos = RestaurantService.get_menu_photos(menu.id) json_menu = serialize(menu) photos_list = [] for photo in photos: photos_list.append(serialize(photo)) json_menu["photos"] = photos_list all_menus.append(json_menu) info["Menus"] = all_menus # get photos about restaurant photos = RestaurantService.get_photos(restaurant_id) info["Photos"] = list_obj_json("", photos) # get dishes dishes = RestaurantService.get_dishes(restaurant_id) info["Dishes"] = list_obj_json("", dishes) # get openings openings = RestaurantService.get_openings(restaurant_id) info["Openings"] = list_obj_json("", openings) return json.loads(json.dumps({"Restaurant_info": info}))
def create_booking(private=False): if private is False: json = request.get_json() else: json = private current_app.logger.debug("Request received: {}".format(json)) restaurant_id = json["restaurant_id"] user_id = json["user_id"] raw_friends = json["raw_friends"] py_datetime = datetime.strptime(json["datetime"], "%Y-%m-%dT%H:%M:%SZ") people_number = json["people_number"] current_app.logger.debug("Translated obj to vars") # split friends mail and check if the number is correct if people_number > 1: splitted_friends = raw_friends.split(";") if len(splitted_friends) != (people_number - 1): return HttpUtils.error_message( 400, "You need to specify ONE mail for each person") current_app.logger.debug("Friends: {}".format(str(splitted_friends))) # if user wants to book in the past.. if py_datetime < datetime.now() and "is_debug" not in json: return HttpUtils.error_message(400, "You can not book in the past!") # check if the user is positive current_user = UserService.get_user_info(user_id) current_app.logger.debug("user is positive ? {}".format( current_user.is_positive)) if current_user.is_positive: return HttpUtils.error_message(401, "You are marked as positive!"), 401 week_day = py_datetime.weekday() # check if the restaurant is open. 12 in open_lunch means open at lunch. 20 in open_dinner means open at dinner. openings = RestaurantService.get_openings(restaurant_id) current_app.logger.debug("Got {} openings".format(len(openings))) # the restaurant is closed if openings is None or len(openings) == 0: current_app.logger.debug("No open hours") return HttpUtils.error_message(404, "The restaurant is closed") opening_hour_json = BookingService.filter_openings(openings, week_day=week_day)[0] current_app.logger.debug( "Got openings this day: {}".format(opening_hour_json)) # the restaurant is closed if opening_hour_json is None: # TODO: Test print("No Opening hour") return HttpUtils.error_message(404, "The restaurant is closed") # bind to obj opening_hour = OpeningHoursModel() opening_hour.fill_from_json(opening_hour_json) current_app.logger.debug("Binded, weekday: {}".format( str(opening_hour.week_day))) # check if restaurant is open response = BookingService.check_restaurant_openings( opening_hour, py_datetime) current_app.logger.debug("Restaurant checked, i got: {}".format( str(response))) if response is not True: return response # now let's see if there is a table """ get the time delta (avg_time) e name from the restaurant """ restaurant_info = RestaurantService.get_info(restaurant_id) restaurant_name = restaurant_info["name"] avg_time = restaurant_info["avg_time"] """ get all the reservation (with the reservation_date between the dates in which I want to book) or (or the reservation_end between the dates in which I want to book) the dates in which I want to book are: start = py_datetime end = py_datetime + avg_time always filtered by the people_number """ # from the list of all tables in the restaurant (the ones in which max_seats < number of people requested) # drop the reserved ones all_table_list = RestaurantService.get_tables(restaurant_id) if all_table_list is None: return HttpUtils.error_message(500, "Can't retrieve restaurant tables") free_tables = BookingService.get_free_tables(all_table_list, people_number, py_datetime, avg_time) # if there are tables available.. get the one with minimum max_seats current_app.logger.debug("OK, There are {} tables available".format( len(free_tables))) if len(free_tables) > 0: chosen_table = BookingService.get_min_seats_table(free_tables) current_app.logger.debug( "OK, table {} has been chosen".format(chosen_table)) # get table name table_name = BookingService.get_table_name(all_table_list, chosen_table) current_app.logger.debug("His name is: {}".format(table_name)) # register on db the reservation new_reservation = Reservation() new_reservation.reservation_date = py_datetime new_reservation.reservation_end = py_datetime + timedelta( minutes=avg_time) new_reservation.customer_id = user_id new_reservation.table_id = chosen_table new_reservation.people_number = people_number db_session.add(new_reservation) db_session.flush() current_app.logger.debug("Reservation saved.") if people_number > 1: # register friends for friend_mail in splitted_friends: new_friend = Friend() new_friend.reservation_id = new_reservation.id new_friend.email = friend_mail.strip() db_session.add(new_friend) else: splitted_friends = [] db_session.commit() SendEmailService.booking_confirmation( current_user.email, current_user.firstname, restaurant_name, splitted_friends, new_reservation.reservation_date, ) return { "id": new_reservation.id, "restaurant_name": restaurant_name, "table_name": table_name, }, 200 else: return HttpUtils.error_message(404, "No tables available")
def calculate_rating_for_all_restaurant(): """ calculate rating for all restaurant(celery) """ done = RestaurantService.calculate_rating_for_all_restaurant() return _get_response(done, 200)
def get_restaurants_by_keyword(name): restaurants = RestaurantService.get_restaurants_by_keyword_name(name) return list_obj_json("restaurants", restaurants)
def delete_table(table_id): if table_id is None: return error_message("400", "table_id not specified"), 400 RestaurantService.delete_table(table_id) return _get_response("OK", 200)
def get_restaurant_name(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 else: return _get_response(restaurant.name, 200)
def get_restaurant(restaurant_id): restaurant = RestaurantService.get_restaurant(restaurant_id) if restaurant is None: return error_message("404", "Restaurant not found"), 404 else: return serialize(restaurant)
def get_restaurants(): restaurants = RestaurantService.get_all_restaurants() return list_obj_json("restaurants", restaurants)