def updateRecipe(userViewID, recipeName): user_id = userViewID recipe = modelPG.Recipe.getRecipeIDByName(recipeName, userViewID) recipe.recipe_name = request.form.get("recipe_name") recipe.user_notes = request.form.get("user_notes") recipeName = recipe.recipe_name components = modelPG.Component.getComponentsByRecipeID(recipe.id) # Get rid of old recipe components for comp in components: modelPG.session.delete(comp) modelPG.session.commit() # Get new values for recipe components, names and percentages chem_list = request.form.getlist("chem") percentages = request.form.getlist("percentage") i = 0 for chem in chem_list: addComp = modelPG.Component() addComp.chem_name = chem addComp.chem_id = modelPG.Chem.getChemIDByName(addComp.chem_name) addComp.percentage = float(percentages[i]) addComp.recipe_id = recipe.id if (percentages[i] != 0) or percentages[i]: modelPG.session.add(addComp) modelPG.session.commit() i += 1 else: modelPG.session.commit() return redirect("/recipecomps/%d/%s" % (user_id, recipeName))
def load_components(session): with open('testfiles/components.csv', 'rb') as csvfile: linereader = csv.reader(csvfile, delimiter=',') for row in linereader: component = modelPG.Component() component.chem_id = row[0] component.recipe_id = row[1] component.percentage = row[2] session.add(component) session.commit()
def renderEnterRecipeForm(): chems = modelPG.session.query(modelPG.Chem).all() chemNames = [chem.chem_name for chem in chems] batchComp = [] components = modelPG.Component() if "user_id" in session: user_id = session["user_id"] else: user_id = None lbschecked = "" kgchecked = 'checked = "checked"' wholeNumList = [] frctnList = [] messageToUser = "" batchsize = 0.0 recipename = "" percentage = 0.0 session["chem"] = "" session["percentage"] = 0.0 return render_template("enter_recipe.html", chem_names=chemNames, batchComp=batchComp, user_id=user_id, lbschecked=lbschecked, kgchecked=kgchecked, wholeNumList=wholeNumList, frctnList=frctnList, chem_list=chems, messageToUser=messageToUser, components=components, batchsize=batchsize, recipename=recipename, percentage=percentage)
def addRecipe(userViewID): displayRecipes = showUserRecipeList(userViewID) recipe_name = request.form.get('recipename') notes = request.form.get('usercomments') dupe = modelPG.session.query(modelPG.Recipe)\ .filter_by(recipe_name=recipe_name)\ .filter_by(user_id=session["user_id"]).all() if dupe != []: flash("Duplicate Recipe Name") return redirect("/addRecipe/" + str(userViewID)) newRecipe = modelPG.Recipe() newRecipe.user_id = session["user_id"] newRecipe.recipe_name = recipe_name newRecipe.user_notes = notes displayRecipes.append(newRecipe) modelPG.session.add(newRecipe) modelPG.session.commit() batchComp = [] batchsize = None chem_list = request.values.getlist("chem") percentages = request.values.getlist("percentage") i = 0 for chem in chem_list: comp = modelPG.Component() comp.chem_name = chem comp.chem_id = modelPG.Chem.getChemIDByName(comp.chem_name) comp.percentage = float(percentages[i]) i += 1 comp.recipe_id = newRecipe.id batchComp.append(float(comp.percentage)) modelPG.session.add(comp) modelPG.session.commit() kgchecked = 'checked = "checked"' lbschecked = "" unitSys = "kg" wholeNumList = [] frctnList = [] messageToUser = None components = modelPG.Component.getComponentsByRecipeID(newRecipe.id) return render_template("recipecomps.html", user_id=userViewID, recipe_name=newRecipe.recipe_name, components=components, batchComp=batchComp, batchsize=batchsize, displayRecipes=displayRecipes, user_notes=newRecipe.user_notes, kgchecked=kgchecked, lbschecked=lbschecked, unitSys=unitSys, wholeNumList=wholeNumList, frctnList=frctnList, messageToUser=messageToUser)
def enterRecipe(): recipeName = request.form.get('recipename') batchsize = request.form.get('batchsize') newRecipe = modelPG.Recipe() newRecipe.recipe_name = recipeName batchComp = [] sizeflt = float(batchsize) kgchecked = 'checked = "checked"' lbschecked = "" unitSys = "kg" percent_list = [] wholeNumList = [] frctnList = [] messageToUser = None chem_list = request.values.getlist('chem') percentages = request.values.getlist('percentage') if percentages == []: flash("no entry") return redirect("/enterRecipe") i = 0 for chem in chem_list: comp = modelPG.Component() comp.chem_name = chem comp.chem_id = modelPG.Chem.getChemIDByName(comp.chem_name) comp.percentage = float(percentages[i]) percent_list.append(comp.percentage) i += 1 comp.recipe_id = newRecipe.id batchComp.append(float(comp.percentage)) onehundred = converter.checkPercent(percent_list) newPercent = converter.getPercentMult(percent_list) if onehundred is False: messageToUser = "******" else: messageToUser = None for i in range(len(batchComp)): batchComp[i] = (sizeflt * batchComp[i]) * newPercent wholeNumList.append(int(batchComp[i])) if unitSys == "kg": frctnList.append(int(converter.frctnKilosToGrams(batchComp[i]))) kgchecked = 'checked = "checked"' else: frctnList.append(int(converter.frctnPoundsToOunces(batchComp[i]))) lbschecked = 'checked = "checked"' return render_template("calc_recipe.html", recipe_name=newRecipe.recipe_name, chem_list=chem_list, percentages=percentages, batchComp=batchComp, batchsize=batchsize, kgchecked=kgchecked, lbschecked=lbschecked, unitSys=unitSys, wholeNumList=wholeNumList, frctnList=frctnList, messageToUser=messageToUser)