Example #1
0
def edit_restaurant(restaurant_id):
    username = login_session.get('username')
    if username is None:
        return redirect('/login')
    restaurant = Restaurant.query.filter_by(id=restaurant_id).one()
    # Check if the current user is the creator
    if login_session.get('user_id') != restaurant.user_id:
        return "<script>function myFunction() {" \
               "alert('You are not authorized to edit this restaurant. " \
               "Please create your own restaurant in order to edit.');" \
               "}</script><body onload='myFunction()'>"
    form = RestaurantForm(obj=restaurant)
    if form.validate_on_submit():
        restaurant.name = form.name.data
        db.session.add(restaurant)
        db.session.commit()
        if app.debug:
            app.logger.debug("Restaurant {} edited!".format(
                (restaurant.id, restaurant.name)))
        flash("Restaurant {} edited!".format((restaurant.id, restaurant.name)))
        return redirect(url_for('restaurant_menu',
                                restaurant_id=restaurant_id))
    else:
        return render_template('editrestaurant.html',
                               form=form,
                               restaurant=restaurant)
def edit_restaurant(restaurant_id):
    username = login_session.get('username')
    if username is None:
        return redirect('/login')
    restaurant = Restaurant.query.filter_by(id=restaurant_id).one()
    # Check if the current user is the creator
    if login_session.get('user_id') != restaurant.user_id:
        return "<script>function myFunction() {" \
               "alert('You are not authorized to edit this restaurant. " \
               "Please create your own restaurant in order to edit.');" \
               "}</script><body onload='myFunction()'>"
    form = RestaurantForm(obj=restaurant)
    if form.validate_on_submit():
        restaurant.name = form.name.data
        db.session.add(restaurant)
        db.session.commit()
        if app.debug:
            app.logger.debug("Restaurant {} edited!".format((restaurant.id,
                                                             restaurant.name)))
        flash("Restaurant {} edited!".format((restaurant.id, restaurant.name)))
        return redirect(url_for('restaurant_menu',
                                restaurant_id=restaurant_id))
    else:
        return render_template('editrestaurant.html', form=form,
                               restaurant=restaurant)
Example #3
0
def new_restaurant():
    username = login_session.get('username')
    if username is None:
        return redirect('/login')
    form = RestaurantForm(request.form)
    if form.validate_on_submit():
        new_rest = Restaurant(name=form.name.data,
                              user_id=login_session['user_id'])
        db.session.add(new_rest)
        db.session.commit()
        if app.debug:
            app.logger.debug("Restaurant {} added!".format(
                (new_rest.id, new_rest.name)))
        flash("Restaurant {} added!".format((new_rest.id, new_rest.name)))
        return redirect(url_for('show_restaurants'))
    else:
        return render_template('new_restaurant.html', form=form)
def new_restaurant():
    username = login_session.get('username')
    if username is None:
        return redirect('/login')
    form = RestaurantForm(request.form)
    if form.validate_on_submit():
        new_rest = Restaurant(name=form.name.data,
                              user_id=login_session['user_id'])
        db.session.add(new_rest)
        db.session.commit()
        if app.debug:
            app.logger.debug("Restaurant {} added!".format((new_rest.id,
                                                            new_rest.name)))
        flash("Restaurant {} added!".format((new_rest.id, new_rest.name)))
        return redirect(url_for('show_restaurants'))
    else:
        return render_template('new_restaurant.html', form=form)