def newIngredient(): if request.method == 'POST': newIngredient = Ingredient(name = request.form['name']) session.add(newIngredient) session.commit() flash("new ingredient item created!") return redirect(url_for('showIngredients')) else: return render_template('newIngredient.html')
jsondata=json.loads(data) # Add Fake User User1 = User(name="Admin", email="*****@*****.**", picture='https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png') session.add(User1) session.commit() for obj in jsondata: if session.query(Category).filter_by(name=obj['category']).one_or_none() is None: cat1 = Category(name=obj['category'], user_id=User1.id) session.add(cat1) session.commit() cat1= session.query(Category).filter_by(name=obj['category']).one() if session.query(Menu).filter_by(name=obj['name']).one_or_none() is None: menu1 = Menu(name=obj['name'], category_id=cat1.id, picture=obj['picture'], servings=obj['servings'], calories=obj['cal/serv'], user_id=cat1.user_id, hour=obj['time']['hour'], minute=obj['time']['minute']) session.add(menu1) session.commit() menu1 = session.query(Menu).filter_by(name=obj['name']).one() for ingre in obj['ingredients']: ingredient = Ingredient(amount=ingre['amount'], description=ingre['description'], menu_id=menu1.id) session.add(ingredient) session.commit() for direction in obj['directions']: direct = Direction(direction=direction, menu_id=menu1.id) session.add(direct) session.commit()
def editRecipe(recipe_id): '''handler to edit an existing recipe''' # change first list elements of cuisines and meals to 'choose one' from # 'all' cuisines[0] = "Choose One" meals[0] = "Choose One" if recipeExists(recipe_id): # if recipe if exists in database # query author, ingredients and process recipe from database author = session.query(Recipe).filter_by(id=recipe_id).one().user recipeToEdit = session.query(Recipe).filter_by(id=recipe_id).one() oldIngredients = session.query( Ingredient).filter_by(recipe_id=recipe_id).all() oldProcesses = session.query(Process).filter_by( recipe_id=recipe_id).all() if 'username' in login_session: if author.id == login_session['user_id']: # if author and user have the same user id # essentially the same process as used in creating a recipe if request.method == "POST": error = "" if request.form['cuisine'] == "Choose One": error = "You must choose a cuisine type!" elif request.form['meal'] == "Choose One": error = "You must choose a meal type!" cuisine = request.form['cuisine'] meal = request.form['meal'] name = request.form['name'] ingredients = request.form['ingredients'] ingredientList = ingredients.split("\n") process = request.form['process'] processList = process.split("\n") picture = request.form['picture'] if picture == "": # generic food image in case user does not include one picture = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Good_Food_Display_-_NCI_Visuals_Online.jpg/1024px-Good_Food_Display_-_NCI_Visuals_Online.jpg" user_id = login_session['user_id'] date = datetime.now() if error != "": flash(error) return render_template( 'editrecipe.html', name=name, ingredients=ingredients, process=process, picture=picture, cuisine=cuisine, meal=meal, meals=meals, cuisines=cuisines) recipeToEdit.name = name recipeToEdit.cuisine = cuisine recipeToEdit.meal = meal recipeToEdit.date = date recipeToEdit.picture = picture # update values of recipe to new values session.add(recipeToEdit) session.commit() # delete old ingredients from database for ingredient in oldIngredients: session.delete(ingredient) session.commit() # add new ingredients to database for ingredient in ingredientList: newIngredient = Ingredient( ingredient=ingredient, recipe_id=recipe_id) session.add(newIngredient) session.commit() # delete old process from database for process in oldProcesses: session.delete(process) session.commit() # add new process to database for process in processList: newProcess = Process( process=process, recipe_id=recipe_id) session.add(newProcess) session.commit() flash( 'New Recipe %s Successfully Editted' % recipeToEdit.name) return redirect(url_for('showRecipe', recipe_id=recipe_id)) else: ingredientString = "" for ingredient in oldIngredients: ingredientString += ingredient.ingredient + "\n" processString = "" for process in oldProcesses: processString += process.process + "\n" return render_template( 'editrecipe.html', recipe=recipeToEdit, ingredientString=ingredientString, processString=processString, meals=meals, cuisines=cuisines) else: # redirect and error if user id is not equal to author id flash('You are not authorized to edit this recipe') return redirect(url_for('showRecipe', recipe_id=recipe_id)) else: # redirect and error if user is not logged in flash('User not logged in') return redirect(url_for('showRecipe', recipe_id=recipe_id)) else: # redirect and error if recipe does not exist flash('Recipe does not exist') return redirect('/')
def addRecipe(): '''handler for adding a new recipe''' # change first list elements of cuisines and meals to 'choose one' from # 'all' cuisines[0] = "Choose One" meals[0] = "Choose One" if request.method == 'POST': error = "" if request.form['cuisine'] == "Choose One": # error is user submits a recipe and doesn't choose a cuisine type error = "You must choose a cuisine type!" elif request.form['meal'] == "Choose One": # error is user submits a recipe and doesn't choose a meal type error = "You must choose a meal type!" cuisine = request.form['cuisine'] meal = request.form['meal'] name = request.form['name'] ingredients = request.form['ingredients'] '''splits ingredient list into individual elements, not useful in this iteration of the web app, but included in case the ability to look up recipes by ingredient is introduced in a future version''' ingredientList = ingredients.split("\n") process = request.form['process'] processList = process.split("\n") picture = request.form['picture'] if picture == "": # generic food image in case user does not include one picture = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Good_Food_Display_-_NCI_Visuals_Online.jpg/1024px-Good_Food_Display_-_NCI_Visuals_Online.jpg" user_id = login_session['user_id'] date = datetime.now() if error != "": '''if there is an error, rerender webpage with prefilled out form and error message''' flash(error) return render_template( 'addrecipe.html', name=name, ingredients=ingredients, process=process, picture=picture, cuisine=cuisine, meal=meal, meals=meals, cuisines=cuisines) # commit new recipe to database newRecipe = Recipe( name=name, cuisine=cuisine, meal=meal, date=date, picture=picture, user_id=user_id) session.add(newRecipe) session.commit() recipe_id = session.query(Recipe).filter_by(name=name).one().id for ingredient in ingredientList: # commit individual ingredients to database newIngredient = Ingredient( ingredient=ingredient, recipe_id=recipe_id) session.add(newIngredient) session.commit() for process in processList: # commit individual method steps to database newProcess = Process(process=process, recipe_id=recipe_id) session.add(newProcess) session.commit() # redirect user to newly created recipe webpage and success message flash('New Recipe %s Successfully Created' % newRecipe.name) return redirect(url_for('showRecipe', recipe_id=recipe_id)) # return to recipe (no s!) else: if 'username' not in login_session: # redirect to '/' if user not logged in, error message flash('User not logged in') return redirect('/') else: return render_template( 'addrecipe.html', meals=meals, cuisines=cuisines)
newUser = User(name="Sean Casey", email="*****@*****.**") session.add(newUser) session.commit() print "Added users" newRecipe = Recipe( name="Panko Fish", cuisine="Japanese", meal="Dinner", date=datetime.now(), picture= "https://www.takemefishing.org/tmf/assets/images/fish/albacore-464x170.png", user_id=2) session.add(newRecipe) session.commit() recipe_id = session.query(Recipe).filter_by(user_id=2).first().id newIngredient = Ingredient(ingredient="3/4 cup Japanese panko breadcrumbs", recipe_id=recipe_id) session.add(newIngredient) session.commit() newIngredient = Ingredient(ingredient="3/4 cup Parmesan cheese finely grated", recipe_id=recipe_id) session.add(newIngredient) session.commit() newIngredient = Ingredient( ingredient="3 tablespoons unsalted butter room temperature", recipe_id=recipe_id) session.add(newIngredient) session.commit() newIngredient = Ingredient(ingredient="3 tablespoons mayonnaise", recipe_id=recipe_id) session.add(newIngredient) session.commit()
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)
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database_setup import Ingredient, Base, RecipeItem engine = create_engine('sqlite:///ingredientrecipe.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Recipe for beef ingredient1 = Ingredient(name="Beef") session.add(ingredient1) session.commit() recipeItem1 = RecipeItem( name="Quick Beef Stir Fry", method= "Heat vegetable oil in a large wok or skillet over medium-high heat; cook and stir beef until browned, 3 to 4 minutes. Move beef to the side of the wok and add broccoli, bell pepper, carrots, green onion, and garlic to the center of the wok. Cook and stir vegetables for 2 minutes. Stir beef into vegetables and season with soy sauce and sesame seeds. Continue to cook and stir until vegetables are tender, about 2 more minutes.", time_needed="25m", ingredient=ingredient1) session.add(recipeItem1) session.commit() recipeItem2 = RecipeItem( name="Mushroom Slow Cooker Roast Beef", method=
session.add(aliment01) session.add(aliment02) session.add(aliment03) session.add(aliment04) session.add(aliment05) session.add(aliment06) session.add(aliment07) session.add(aliment08) session.add(aliment09) session.add(aliment10) session.commit() # Fill recipes and ingredients recipe01 = Recipe(name="Spanish tortilla") ingredient01 = Ingredient(recipe=recipe01, aliment=aliment01, amount=6, units="units") ingredient02 = Ingredient(recipe=recipe01, aliment=aliment02, amount=1, units="nibbe") # = pellizco ingredient03 = Ingredient(recipe=recipe01, aliment=aliment03, amount=4, units="units") ingredient04 = Ingredient(recipe=recipe01, aliment=aliment04, amount=2, units="units") session.add(recipe01) session.add(ingredient01)