Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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}))
Ejemplo n.º 4
0
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")