Esempio n. 1
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. 2
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)