コード例 #1
0
def get_rest_with_name_and_phone(name, phone):
    """
    This method factorize the code to get an restaurant with a name
    :param name: the email that we want use to query the user
    :return: return the user if exist otherwise None
    """
    reservations = BookingServices.get_all_booking()
    for res in reservations:
        if res["name"] == name and res["phone"] == phone:
            return res
    return None
コード例 #2
0
    def test_reservation_local_ok_by_email(self):
        """
        This test cases, tru to test the logic inside the services to avoid
        stupid result

        http://localhost:5000/my_reservations?fromDate=2013-10-07&toDate=2014-10-07&[email protected]
        :return:
        """
        owner = create_user_on_db(randrange(1, 500000), role_id=2)
        assert owner is not None

        rest = create_restaurants_on_db(user_id=owner.id,
                                        user_email=owner.email)
        assert rest is not None

        user = create_user_on_db(randrange(1, 500000), )
        assert user is not None

        date_time = datetime(2022, 10, 28, 21, 30)

        reservation = create_random_booking(1, rest.id, user, date_time,
                                            "*****@*****.**")
        assert reservation is not None

        from_date = datetime(2022, 9, 28)
        to_date = datetime(2022, 11, 28)

        print(BookingServices.get_all_booking())

        search_reservation = BookingServices.get_reservation_by_constraint(
            user.id, from_date, to_date, rest.id)
        print(search_reservation)
        assert len(search_reservation) != 0

        del_booking(reservation["id"], user.id)
        del_user_on_db(user.id)
        del_user_on_db(owner.id)
        del_restaurant_on_db(rest.id)
コード例 #3
0
def create_random_booking(num: int, rest_id: int, user: UserModel, date_time,
                          friends):
    """
    Function to make
    :param num:
    :param rest_id:
    :param user:
    :param date_time:
    :param friends:
    :return:
    """
    people_number = len(friends.split(";")) + 1
    reservation = BookingServices.book(rest_id, user, date_time, people_number,
                                       friends, True)
    return reservation
コード例 #4
0
def delete_reservation(reservation_id):

    deleted = BookingServices.delete_book(reservation_id, current_user.id)

    reservations_as_list = UserService.get_customer_reservation(
        None, None, current_user.id)
    form = ReservationForm()
    return render_template(
        "user_reservations.html",
        reservations_as_list=reservations_as_list,
        my_date_formatter_iso=my_date_formatter_iso,
        deleted=deleted,
        _test="del_rest_test",
        form=form,
    )
コード例 #5
0
    def test_reservation_local_ko_by_email(self):
        """
        This test cases, tru to test the logic inside the services to avoid
        stupid result

        http://localhost:5000/my_reservations?fromDate=2013-10-07&toDate=2014-10-07&[email protected]
        :return:
        """
        user = create_user_on_db(randrange(1, 500000), role_id=2)
        assert user is not None

        from_date = datetime(2013, 10, 7)
        to_date = datetime(2014, 10, 7)

        def_rest = RestaurantServices.get_all_restaurants()[0]
        assert def_rest is not None
        all_reservation = BookingServices.get_reservation_by_constraint(
            user.id,
            from_date,
            to_date,
            def_rest["id"],
        )
        assert len(all_reservation) == 0
コード例 #6
0
def del_booking(reservation_id, customer_id):
    return BookingServices.delete_book(reservation_id, customer_id)
コード例 #7
0
def del_booking_services(id: int, user_id: int):
    """
    :param id: reservation is:
    :return:
    """
    BookingServices.delete_book(id, user_id)
コード例 #8
0
    def search_contacts(user_email: str, user_phone: str):
        if user_email == "" and user_phone == "":
            return "Insert an email or a phone number"
        response = UserService.search_possible_contacts(user_email, user_phone)

        if response is None:
            return "The customer not registered or not positive"

        contact_users_GUI = []
        # now we have the information about the positivity of the user in response
        date_marking = response["from_date"]
        user_id = response["user_id"]

        # start to check contacts
        # API: get all reservation of the customer between date_marking and date_marking -14
        date_marking = datetime.strptime(date_marking, "%Y-%m-%d")
        to_date = date_marking - timedelta(days=14)
        reservations_customer = BookingServices.get_reservation_by_constraint(
            user_id, from_data=to_date, to_data=date_marking)

        i = 1
        if reservations_customer is not None:
            all_reservations = BookingServices.get_reservation_by_constraint(
                from_data=to_date, to_data=date_marking)
            if all_reservations is None:
                return None

            for reservation in reservations_customer:
                restaurant_id = reservation["table"]["restaurant"]["id"]

                start = datetime.strptime(reservation["reservation_date"],
                                          "%Y-%m-%dT%H:%M:%SZ")
                end = datetime.strptime(reservation["reservation_end"],
                                        "%Y-%m-%dT%H:%M:%SZ")
                current_app.logger.debug(
                    "I'm working with reserv from {} to {}".format(start, end))
                for one_reservation in all_reservations:
                    restaurant_id_contact = one_reservation["table"][
                        "restaurant"]["id"]
                    if restaurant_id_contact != restaurant_id:
                        continue
                    start_contact = datetime.strptime(
                        one_reservation["reservation_date"],
                        "%Y-%m-%dT%H:%M:%SZ")
                    end_contact = datetime.strptime(
                        one_reservation["reservation_end"],
                        "%Y-%m-%dT%H:%M:%SZ")

                    # if people are in the same restaurant in the same day
                    if start.date() == start_contact.date():
                        openings = RestaurantServices.get_opening_hours_restaurant(
                            restaurant_id)

                        dayNumber = start.weekday()
                        restaurant_hours = []
                        current_app.logger.debug(
                            "I got openings. Start is {}".format(dayNumber))
                        for opening in openings:
                            if opening["week_day"] == dayNumber:
                                restaurant_hours.append(
                                    datetime.strptime(opening["open_lunch"],
                                                      "%H:%M"))
                                restaurant_hours.append(
                                    datetime.strptime(opening["close_lunch"],
                                                      "%H:%M"))
                                restaurant_hours.append(
                                    datetime.strptime(opening["open_dinner"],
                                                      "%H:%M"))
                                restaurant_hours.append(
                                    datetime.strptime(opening["close_dinner"],
                                                      "%H:%M"))

                        # if people are in the restaurant at lunch or dinner
                        if (restaurant_hours[0].time() <= start.time()
                                and restaurant_hours[1].time() >= end.time()
                            ) or (restaurant_hours[2].time() <= start.time()
                                  and
                                  restaurant_hours[3].time() >= end.time()):
                            # people are in the same restaurant at lunch
                            # if they are in the same time
                            if (end_contact < start
                                    or start_contact > end) is False:
                                # they are contacts!
                                # API: get user email and name of the contact
                                user = UserService.get_user_by_id(
                                    one_reservation["customer_id"])
                                if user is not None:
                                    contact_users_GUI.append([
                                        i,
                                        user.firstname + " " + user.lastname,
                                        user.dateofbirth,
                                        user.email,
                                        user.phone,
                                    ])
                                    i += 1
        return contact_users_GUI
コード例 #9
0
    def search_contacts_for_email(user_email: str, user_phone: str):

        if user_email == "" and user_phone == "":
            return "Insert an email or a phone number"

        friends = []
        contacts = []
        past_restaurants = []
        future_restaurants = []

        if user_email != "":
            URL = USER_MICROSERVICE_URL + "/positiveinfo/email/" + str(
                user_email)
        else:
            URL = USER_MICROSERVICE_URL + "/positiveinfo/phone/" + str(
                user_phone)

        # check if the user exists
        # check if the user is positive (get also date of marking) (API)
        response = HttpUtils.make_get_request(URL)
        if response is None:
            return "Error, please try again"

        # now we have the information about the positivity of the user in response
        date_marking = response["from_date"]
        user_id = response["user_id"]

        # start to check contacts
        # API: get all reservation of the customer between date_marking and date_marking -14
        date_marking = datetime.strptime(date_marking, "%Y-%m-%d")
        to_date = date_marking - timedelta(days=14)
        reservations_customer = BookingServices.get_reservation_by_constraint(
            user_id, from_data=to_date, to_data=date_marking)

        if reservations_customer is not None:

            # API: get all reservations between date_marking and date_m -14
            all_reservations = BookingServices.get_reservation_by_constraint(
                from_data=to_date, to_data=date_marking)
            for reservation in reservations_customer:
                restaurant_id = reservation["table"]["restaurant"]["id"]
                restaurant = RestaurantServices.get_rest_by_id(restaurant_id)
                if restaurant is None:
                    continue
                friends = friends + reservation["people"]
                start = datetime.strptime(reservation["reservation_date"],
                                          "%Y-%m-%dT%H:%M:%SZ")
                end = datetime.strptime(reservation["reservation_end"],
                                        "%Y-%m-%dT%H:%M:%SZ")
                past_restaurants.append({
                    "email":
                    restaurant.owner_email,
                    "name":
                    restaurant.name,
                    "date":
                    start.strftime("%Y-%m-%dT%H:%M:%SZ"),
                })
                for one_reservation in all_reservations:
                    restaurant_id_contact = one_reservation["table"][
                        "restaurant"]["id"]
                    if restaurant_id_contact != restaurant_id:
                        continue

                    start_contact = datetime.strptime(
                        one_reservation["reservation_date"],
                        "%Y-%m-%dT%H:%M:%SZ")
                    end_contact = datetime.strptime(
                        one_reservation["reservation_end"],
                        "%Y-%m-%dT%H:%M:%SZ")
                    # if people are in the same restaurant in the same day
                    if start.date() != start_contact.date():
                        continue
                    openings = RestaurantServices.get_opening_hours_restaurant(
                        restaurant_id)

                    dayNumber = start.weekday()
                    restaurant_hours = []
                    for opening in openings:
                        if opening["week_day"] == dayNumber:
                            restaurant_hours.append(
                                datetime.strptime(opening["open_lunch"],
                                                  "%H:%M"))
                            restaurant_hours.append(
                                datetime.strptime(opening["close_lunch"],
                                                  "%H:%M"))
                            restaurant_hours.append(
                                datetime.strptime(opening["open_dinner"],
                                                  "%H:%M"))
                            restaurant_hours.append(
                                datetime.strptime(opening["close_dinner"],
                                                  "%H:%M"))

                    # if people are in the restaurant at lunch or dinner
                    if (restaurant_hours[0].time() <= start.time()
                            and restaurant_hours[1].time() >= end.time()) or (
                                restaurant_hours[2].time() <= start.time()
                                and restaurant_hours[3].time() >= end.time()):
                        # people are in the same restaurant at lunch
                        # if they are in the same time
                        if not ((end_contact < start) or
                                (start_contact > end)):
                            # they are contacts!
                            # API: get user email and name of the contact
                            user = UserService.get_user_by_id(
                                one_reservation["customer_id"])
                            if user is not None:
                                contacts.append({
                                    "email":
                                    user.email,
                                    "name":
                                    user.firstname,
                                    "restaurant_name":
                                    restaurant.name,
                                    "date":
                                    start.strftime("%Y-%m-%dT%H:%M:%SZ"),
                                })
                                friends = friends + one_reservation["people"]
        if user_email != "":
            customer_email = user_email
        else:
            URL = USER_MICROSERVICE_URL + "/" + str(user_id)
            user = HttpUtils.make_get_request(URL)
            customer_email = user["email"]

        # API booking: get all future booking of the customer
        future_reservations = BookingServices.get_reservation_by_constraint(
            user_id, from_data=to_date, to_data=date_marking)
        for future_reservation in future_reservations:
            date = datetime.strptime(reservation["reservation_date"],
                                     "%Y-%m-%dT%H:%M:%SZ")
            restaurant_id = future_reservation["table"]["restaurant"]["id"]
            restaurant = RestaurantServices.get_rest_by_id(restaurant_id)
            if restaurant is not None:
                future_restaurants.append({
                    "email":
                    restaurant.owner_email,
                    "name":
                    restaurant.name,
                    "date":
                    date.strftime("%Y-%m-%dT%H:%M:%SZ"),
                    "customer_email":
                    customer_email,
                })
        return {
            "friends": friends,
            "contacts": contacts,
            "past_restaurants": past_restaurants,
            "reservation_restaurants": future_restaurants,
        }