def create_new_restaurant(form: RestaurantForm, user_id: int,
                              max_sit: int):
        """
        This method contains all logic save inside the a new restaurant
        :return:
        """
        restaurant = Restaurant()
        form.populate_obj(restaurant)
        restaurant.owner_id = user_id
        restaurant.likes = 0
        restaurant.covid_measures = form.covid_measures.data

        db.session.add(restaurant)
        db.session.commit()

        for i in range(int(form.n_tables.data)):
            new_table = RestaurantTable()
            new_table.restaurant_id = restaurant.id
            new_table.max_seats = max_sit
            new_table.available = True
            new_table.name = ""

            db.session.add(new_table)
            db.session.commit()

        # inserimento orari di apertura
        days = form.open_days.data
        for i in range(len(days)):
            new_opening = OpeningHours()
            new_opening.restaurant_id = restaurant.id
            new_opening.week_day = int(days[i])
            new_opening.open_lunch = form.open_lunch.data
            new_opening.close_lunch = form.close_lunch.data
            new_opening.open_dinner = form.open_dinner.data
            new_opening.close_dinner = form.close_dinner.data
            db.session.add(new_opening)
            db.session.commit()

        # inserimento tipi di cucina
        cuisin_type = form.cuisine.data
        for i in range(len(cuisin_type)):
            new_cuisine = Menu()
            new_cuisine.restaurant_id = restaurant.id
            new_cuisine.cusine = cuisin_type[i]
            new_cuisine.description = ""
            db.session.add(new_cuisine)
            db.session.commit()

        return restaurant
Esempio n. 2
0
def create_restaurant():
    """
    This flask method give the possibility with a POST request to create a new
    restaurant inside the system
    """
    form = RestaurantForm()
    if request.method == "POST":
        if form.validate_on_submit():
            q = db.session.query(Restaurant).filter_by(
                name=form.name.data,
                phone=form.phone.data,
                lat=form.lat.data,
                lon=form.lon.data,
            )
            if q.first() is not None:
                return render_template(
                    "create_restaurant.html",
                    form=form,
                    _test="rest_already_here_test",
                    message="Restaurant {} in {}:{} already existed".format(
                        form.name.data, form.lat.data, form.lon.data),
                )
            q_user = db.session.query(User).filter_by(
                id=current_user.id).first()
            if q_user is None:
                return render_template(
                    "create_restaurant.html",
                    _test="anonymus_user_test",
                    form=form,
                    message="User not logged",
                )

            # set the owner
            newrestaurant = RestaurantServices.create_new_restaurant(
                form, q_user.id, _max_seats)
            session["RESTAURANT_ID"] = newrestaurant.id
            return redirect("/")
    return render_template("create_restaurant.html",
                           _test="create_rest_test",
                           form=form)
Esempio n. 3
0
    def test_create_restaurant(self):
        """
        test create user
        :return:
        """
        form = RestaurantForm()
        form.name = "Gino Sorbillo"
        form.phone = "096321343"
        form.lat = 12
        form.lon = 12
        form.n_tables.data = 50
        form.covid_measures.data = "We can survive"
        form.cuisine.data = ["Italian food"]
        form.open_days.data = ["0"]
        form.open_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.close_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.open_dinner.data = datetime.time(datetime(2020, 7, 1, 18, 00))
        form.close_dinner.data = datetime.time(datetime(2020, 6, 1, 22, 00))
        q_user = db.session.query(User).filter_by(
            email="*****@*****.**").first()
        assert q_user is not None
        restaurant = RestaurantServices.create_new_restaurant(
            form, q_user.id, 6)
        assert restaurant is not None

        del_restaurant_on_db(restaurant.id)
Esempio n. 4
0
def create_restaurants_on_db(
    name: str = "Gino Sorbillo", user_id: int = None, tables: int = 50
):
    form = RestaurantForm()
    form.name.data = name
    form.phone.data = "1234"
    form.lat.data = 183
    form.lon.data = 134
    form.n_tables.data = tables
    form.covid_measures.data = "We can survive"
    form.cuisine.data = ["Italian food"]
    form.open_days.data = ["0"]
    form.open_lunch.data = time(hour=12, minute=00)
    form.close_lunch.data = time(hour=15, minute=00)
    form.open_dinner.data = time(hour=19, minute=00)
    form.close_dinner.data = time(hour=22, minute=00)
    return RestaurantServices.create_new_restaurant(form, user_id, 6)
Esempio n. 5
0
def my_data():
    """
    TODO(vincenzopalazzo) add information
    This API call give the possibility to the user TODO
    """
    message = None
    if request.method == "POST":
        # update query
        q = Restaurant.query.filter_by(id=session["RESTAURANT_ID"]).update({
            "name":
            request.form.get("name"),
            "lat":
            request.form.get("lat"),
            "lon":
            request.form.get("lon"),
            "covid_measures":
            request.form.get("covid_measures"),
        })
        # if no resturant match the update query (session problem probably)
        if q == 0:
            message = "Some Errors occurs"
        else:
            db.session.commit()
            message = "Restaurant data has been modified."

    # get the resturant info and fill the form
    # this part is both for POST and GET requests
    q = Restaurant.query.filter_by(id=session["RESTAURANT_ID"]).first()
    if q is not None:
        form = RestaurantForm(obj=q)
        form2 = RestaurantTableForm()
        tables = RestaurantTable.query.filter_by(
            restaurant_id=session["RESTAURANT_ID"])
        return render_template(
            "restaurant_data.html",
            form=form,
            only=["name", "lat", "lon", "covid_measures"],
            tables=tables,
            form2=form2,
            message=message,
        )
    else:
        return redirect("/create_restaurant")
Esempio n. 6
0
def create_restaurant():
    if current_user is not None and hasattr(current_user, 'id'):
        if (current_user.role == 'customer' or current_user.role == 'ha'):
            return make_response(
                render_template(
                    'error.html',
                    message=
                    "You are not a restaurant owner! Redirecting to home page",
                    redirect_url="/"), 403)

        form = RestaurantForm()

        if request.method == 'POST':

            if form.validate_on_submit():

                # if one or more fields that must not be present are
                must_not_be_present = [
                    'owner_id', 'capacity', 'tot_reviews', 'avg_rating',
                    'likes'
                ]
                if any(k in must_not_be_present for k in request.form):
                    return make_response(
                        render_template('create_restaurant.html',
                                        form=RestaurantForm()), 400)

                working_days_to_add = []
                tables_to_add = []
                dishes_to_add = []
                new_restaurant = Restaurant()

                # check that all restaurant/working days/tables/dishes fields are correct
                try:
                    working_days_to_add = _check_working_days(
                        form.workingdays.data)
                    del form.workingdays

                    tables_to_add, tot_capacity = _check_tables(
                        form.tables.data)
                    del form.tables

                    dishes_to_add = _check_dishes(form.dishes.data)
                    del form.dishes

                    form.populate_obj(new_restaurant)
                    new_restaurant.owner_id = current_user.id
                    new_restaurant.capacity = tot_capacity
                except:
                    return make_response(
                        render_template('create_restaurant.html',
                                        form=RestaurantForm()), 400)

                db.session.add(new_restaurant)
                db.session.commit()

                # database check when insert the tables and dishes
                for l in [working_days_to_add, tables_to_add, dishes_to_add]:
                    for el in l:
                        el.restaurant_id = new_restaurant.id
                        db.session.add(el)
                db.session.commit()
                return redirect('/')

            else:
                # invalid form
                return make_response(
                    render_template('create_restaurant.html', form=form), 400)

        return render_template('create_restaurant.html', form=form)

    else:
        return make_response(
            render_template(
                'error.html',
                message="You are not logged! Redirecting to login page",
                redirect_url="/login"), 403)