def editMenu(category_id, menu_id): category = session.query(Category).filter_by(id=category_id).one() item = session.query(Menu).filter_by(id=menu_id).one() ingredients = session.query(Ingredient).filter_by(menu_id=item.id).first() directions = session.query(Direction).filter_by(menu_id=item.id).first() # The user has to be the one who created this menu. if item.user_id != login_session.get('user_id'): return "<script>function myFunction() {" \ "alert('You are not authorized to edit this menu.');}" \ "</script><body onload='myFunction()'>" if directions is not None: directions = session.query(Direction).filter_by(menu_id=item.id).all() else: directions = [] if ingredients is not None: ingredients = session.query(Ingredient)\ .filter_by(menu_id=item.id).all() else: ingredients = [] if request.method == 'POST': item.name = request.form['name'] item.picture = request.form['picture'] item.servings = request.form['servings'] item.calories = request.form['calories'] item.hour = request.form['hour'] item.minute = request.form['minute'] session.add(item) session.commit() amount = request.form.getlist('amount') descriptions = request.form.getlist('description') # Store ingredients for i in range(0, len(descriptions)): description = descriptions[i] amt = amount[i] # amt = amt.encode("utf-8") description = description.replace('\u00b0', '') description = description.replace('\u00a0', '') # If the description is empty while amt is filled if len(description) == 0 and len(amount[i]) != 0: abort(400, description="You only entered " "the amount of ingredient: " "description is missing.") # If the description is empty, ignore it. elif len(description) == 0: del amount[i] del descriptions[i] # If the description is filled. else: ingredient = session.query(Ingredient)\ .filter_by(description=description).first() if ingredient is None: ingredient = Ingredient(amount=amt, description=description, menu_id=menu_id) session.add(ingredient) session.commit() else: # Update amount if ingredient.amount != amt: ingredient.amount = amt session.add(ingredient) session.commit() # Ignore all ingredients that a user clicked 'delete' amounts = request.form.getlist('delete-amount') descriptions = request.form.getlist('delete-description') for description in descriptions: if len(description) > 0: ingredient = session.query(Ingredient)\ .filter_by(description=description).one() session.delete(ingredient) session.commit() # Add directions in database directions = request.form.getlist('direction') for direction in directions: # Make sure direction is filled if len(direction) > 0: valid = session.query(Direction)\ .filter_by(direction=direction).first() # Prevent from making a duplicate if valid is None: direction = Direction(menu_id=menu_id, direction=direction) session.add(direction) session.commit() # Delete the directions from our database directions = request.form.getlist('delete-dir') for direction in directions: if len(direction) > 0: valid = session.query(Direction)\ .filter_by(direction=direction).first() # If the data was stored in our database if valid is not None: session.delete(valid) session.commit() ingredients = session.query(Ingredient)\ .filter_by(menu_id=menu_id).all() directions = session.query(Direction)\ .filter_by(menu_id=menu_id).all() return redirect(url_for('menuDetails', category_id=category.id, menu_id=menu_id, ingredients=ingredients, directions=directions)) else: return render_template('editMenu.html', ingredients=ingredients, category=category, item=item, directions=directions, user=login_session)
def newMenu(category_id): category = session.query(Category).filter_by(id=category_id).one() if request.method == 'POST': valid = session.query(Menu)\ .filter_by(name=request.form['name']).one_or_none() # Prevent from making a duplicate if valid is None: menu = Menu(user_id=login_session['user_id'], name=request.form['name'], picture=request.form['picture'], servings=request.form['servings'], calories=request.form['calories'], hour=request.form['hour'], minute=request.form['minute'], category_id=category_id) session.add(menu) session.commit() addedmenu = session.query(Menu)\ .filter_by(name=request.form['name']).one() amount = request.form.getlist('amount') descriptions = request.form.getlist('description') # Store ingredients for i in range(0, len(descriptions)): description = descriptions[i] amt = amount[i] # amt = amt.encode("utf-8") description = description.replace('\u00b0', '') description = description.replace('\u00a0', '') # If the description is empty while amt is filled if len(description) == 0 and len(amount[i]) != 0: abort(400, description="You only entered " "the amount of ingredient: " "description is missing.") # If the description is empty, ignore it. elif len(description) == 0: del amount[i] del descriptions[i] # If the description is filled. else: ingredient = session.query(Ingredient) \ .filter_by(description=description).first() if ingredient is None: ingredient = Ingredient(amount=amt, description=description, menu_id=addedmenu.id) session.add(ingredient) session.commit() else: # Update amount if ingredient.amount != amt: ingredient.amount = amt session.add(ingredient) session.commit() # Ignore all ingredients that a user clicked 'delete' amounts = request.form.getlist('delete-amount') descriptions = request.form.getlist('delete-description') for description in descriptions: if len(description) > 0: ingredient = session.query(Ingredient) \ .filter_by(description=description).one() session.delete(ingredient) session.commit() # Add directions in database directions = request.form.getlist('direction') for direction in directions: # Make sure direction is filled if len(direction) > 0: valid = session.query(Direction) \ .filter_by(direction=direction).first() # Prevent from making a duplicate if valid is None: direction = Direction(menu_id=addedmenu.id, direction=direction) session.add(direction) session.commit() # Delete the directions from our database directions = request.form.getlist('delete-dir') for direction in directions: if len(direction) > 0: valid = session.query(Direction) \ .filter_by(direction=direction).first() # If the data was stored in our database if valid is not None: session.delete(valid) session.commit() ingredients = session.query(Ingredient) \ .filter_by(menu_id=addedmenu.id).all() directions = session.query(Direction) \ .filter_by(menu_id=addedmenu.id).all() return redirect(url_for('menuList', category_id=category.id)) else: abort(400, description="The menu name you " "entered is already being used.") else: return render_template('newMenu.html', category=category, user=login_session)