Ejemplo n.º 1
0
def editMenuItem(restaurant_id, menu_id):
    """page to edit a menu item."""

    if 'access_token' not in flask_session:
        return logInRedirect()
    restaurant = session.query(Restaurant).filter_by(id = restaurant_id).first()
    user_id = getUserId(flask_session['email'],flask_session['google_plus_id'])
    if not restaurant.user_id == user_id:
        return redirect(url_for("publicMenu",restaurant_id = restaurant_id))
        flash("Only restaurant owners can edit items.")

    if request.method == "POST":
        edited_name = request.form['edited_name']
        print "\neditMenuItem POST triggered, name is: ", edited_name
        old_name = session.query(MenuItem).filter_by(id = menu_id).first().name

        result = session.execute(""" UPDATE menu_item
                                     SET name=:edited_name
                                     WHERE id=:edited_menu_item_id; """,
                                 { "edited_name": edited_name,
                                   "edited_menu_item_id": menu_id}
                                 )
        session.commit()
        flash( "item '" +  old_name + "' edited to '" + edited_name + "'. Jawohl!")
        return redirect(url_for("showMenu", restaurant_id=restaurant_id))

    else:
        menuItem = session.query(MenuItem).filter_by(id = menu_id).first()
        return render_template('editMenuItem.html',
                                  restaurant = restaurant,
                                  menuItem = menuItem )
Ejemplo n.º 2
0
def deleteMenuItem(restaurant_id, menu_id):
    """page to delete a menu item."""

    if 'access_token' not in flask_session:
        return logInRedirect()
    restaurant = session.query(Restaurant).filter_by(id = restaurant_id).first()
    user_id = getUserId(flask_session['email'],flask_session['google_plus_id'])
    if not restaurant.user_id == user_id:
        flash("Only restaurant owners can delete items.")
        return redirect(url_for("publicMenu",restaurant_id = restaurant_id))

    if request.method == "POST":
        print "\ndeleteMenuItem POST triggered!, menu_id is: ", menu_id
        deletedMenuItem = session.query(MenuItem).filter_by(id = menu_id).first()
        session.delete(deletedMenuItem)
        session.commit()
        flash( "item '" + deletedMenuItem.name + "' deleted. Auf Wiedersehen!")
        return redirect(url_for("showMenu", restaurant_id=restaurant_id))

    else:
        print "restaurants/delete accessed..."
        menuItem = session.query(MenuItem).filter_by(id = menu_id).first()
        return render_template( 'deleteMenuItem.html',
                                   menuItem = menuItem,
                                   restaurant = restaurant )
Ejemplo n.º 3
0
def publicMenu(restaurant_id):
    """ displays all menu items for a restaurant id, read-only."""

    restaurant = session.query(Restaurant).filter_by(id = restaurant_id).first()
    menuItems = session.query(MenuItem).filter_by(restaurant_id = restaurant_id)
    creator = getUserInfo(restaurant.user_id)

    return render_template( 'publicMenu.html',
                            menuItems = menuItems,
                            restaurant = restaurant,
                            creator= creator )
Ejemplo n.º 4
0
def showMenu(restaurant_id):
    """ displays all menu items for a restaurant id, with all CRUD options."""

    if 'access_token' not in flask_session:
        return logInRedirect()
    restaurant = session.query(Restaurant).filter_by(id = restaurant_id).first()
    user_id = getUserId(flask_session['email'],flask_session['google_plus_id'])
    if not restaurant.user_id == user_id:
        return redirect(url_for("publicMenu",restaurant_id = restaurant_id))

    menuItems = session.query(MenuItem).filter_by(restaurant_id = restaurant_id)
    creator = getUserInfo(restaurant.user_id)

    return render_template( 'showMenu.html',
                            restaurant = restaurant,
                            menuItems = menuItems,
                            creator = creator )
Ejemplo n.º 5
0
def createUser(flask_session):
    newUser = User(
        name=flask_session["username"],
        picture=flask_session["picture"],
        link=flask_session["link"],
        email=flask_session["email"],
        google_plus_id=flask_session["google_plus_id"],
    )
    session.add(newUser)
    session.commit()
    user = session.query(User).filter_by(google_plus_id=flask_session["google_plus_id"]).one()
    return user.id
Ejemplo n.º 6
0
def deleteRestaurant(restaurant_id):
    """page to delete a restaurant (authorized only for creators)."""

    if "access_token" not in flask_session:
        return logInRedirect()
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).first()
    user_id = getUserId(flask_session["email"], flask_session["google_plus_id"])
    if not restaurant.user_id == user_id:
        flash("Only restaurant owners can delete restaurants.")
        return redirect(url_for("publicMenu", restaurant_id=restaurant_id))

    if request.method == "POST":
        print "\ndeleteRestaurant POST triggered!"
        deletedRestaurant = session.query(Restaurant).filter_by(id=restaurant_id).first()
        session.delete(deletedRestaurant)
        session.commit()
        flash("item '" + deletedRestaurant.name + "' deleted. Auf Wiedersehen!")
        return redirect(url_for("showRestaurants"))

    else:
        print "restaurants/id/delete accessed..."
        return render_template("deleteRestaurant.html", restaurant=restaurant)
Ejemplo n.º 7
0
def newMenuItem(restaurant_id):
    """page to create a new menu item."""

    if 'access_token' not in flask_session:
        return logInRedirect()
    restaurant = session.query(Restaurant).filter_by(id = restaurant_id).first()
    user_id = getUserId(flask_session['email'],flask_session['google_plus_id'])
    if not restaurant.user_id == user_id:
        flash("Only restaurant owners can add new items.")
        return redirect(url_for("publicMenu",restaurant_id = restaurant_id))

    if request.method == "POST":
        new_name = request.form['new_name']
        print "\nnewMenuItem POST triggered, name is: ", new_name
        newMenuItem = MenuItem( name=new_name,
                                restaurant_id=restaurant.id )
        session.add(newMenuItem)
        session.commit()
        flash( "new item '" + new_name + "' created!")
        print "POST worked!"
        return redirect(url_for("showMenu", restaurant_id=restaurant.id))

    else:
        return render_template('newMenuItem.html', restaurant = restaurant)
Ejemplo n.º 8
0
def menuItemJSON(restaurant_id, menu_id):
    restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one()
    menuItem = session.query(MenuItem).filter_by(restaurant_id = restaurant_id).filter_by(id = menu_id).one()
    return jsonify(MenuItem = menuItem.serialize)
Ejemplo n.º 9
0
def restaurantMenuJSON(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one()
    menuItems = session.query(MenuItem).filter_by(restaurant_id = restaurant_id).all()
    return jsonify(MenuItems = [menuItem.serialize for menuItem in menuItems])
Ejemplo n.º 10
0
def publicRestaurants():
    if "access_token" not in flask_session:
        return logInRedirect()
    restaurants = session.query(Restaurant).all()
    return render_template("publicRestaurants.html", restaurants=restaurants)
Ejemplo n.º 11
0
def showRestaurants():
    restaurants = session.query(Restaurant).all()
    return render_template("showRestaurants.html", restaurants=restaurants)
Ejemplo n.º 12
0
def getUserIdFromGooglePlusID(google_plus_id):
    user = session.query(User).filter_by(google_plus_id=google_plus_id).one()
    return user.id
Ejemplo n.º 13
0
def getUserIdFromEmail(email):
    user = session.query(User).filter_by(email=email).one()
    return user.id
Ejemplo n.º 14
0
def getUserInfo(user_id):
    user = session.query(User).filter_by(id=user_id).one()
    return user