コード例 #1
0
def recipeList(tag):
    if 'username' in session:
        loggedin = True
        username = escape(session['username'])
    else:
        loggedin = False
    if request.method == 'POST':
        if 'searched' in request.form:
            if request.form['searched']!= "": #using search bar
                return redirect(url_for("recipeList", tag = request.form['searched']))   
    else:
        tagL = tag.split()
        if len(tagL)>1:
            oldtag = tag
            tag="%20".join(tagL)
        else:
            oldtag = tagL[0]
        num = 1
        reclist = []
        while num <=4:
            db = recipes.getSearchVal(tag,num)
            if db['count'] !=  0:
                reclist = reclist + recipes.getrecipes(db, num)
                num =  num + 1
            else:
                break
        return render_template("recipes.html", loggedin = loggedin, tag = oldtag, reclist = reclist)    
コード例 #2
0
def recipe(tag, num, title):
    title= title.replace("%20"," ")
    tagL = tag.split()
    if len(tagL)>1:
        oldtag = tag
        tag="%20".join(tagL)
    else:
        tag = tagL[0]
    db = recipes.getSearchVal(tag, num)
    nurl = recipes.geturls(db, title)
    rec = recipes.retrecipe(nurl[0]) 
    ing = recipes.reting(nurl[1])
    nutrifact = nutrition.parser(ing)
    n = nutrifact[0]
    allergen= nutrifact[1]
    measurement = nutrifact[2]
    if 'username' in session:
        loggedin = True
        username = escape(session['username'])
    else:
        loggedin = False
    if request.method == 'POST':
        if 'searched' in request.form:
            if request.form['searched']!= "": #using search bar
                return redirect(url_for("recipeList", tag = request.form['searched']))
        else:
            if loggedin: #logged in: add to favorites, redirect to same page, and flash message
                mongo_input =  {'atitle': title,
                                'ing': ing,
                                'rec': rec }
                MongoWork.update_favorites(username, mongo_input)
                print MongoWork.find_favorites(username)
                flash("Added recipe to Favorites!");
                return redirect(url_for("recipe", tag = tag, num=num, title=title))
            else:
                flash("Please log in to use the Add to Favorites feature!")
                return redirect(url_for("recipe", tag = tag, num=num, title=title))
    else: ##GET METHOD
        return render_template("recipe.html", 
                               loggedin=loggedin, 
                               title=title, 
                               rec = rec, 
                               ing = ing,  
                               sizes = "1 meal",
                               serverpcont = "1" ,
                               calories = nutrition.nformat(n,"nf_calories"),
                               fatcals = nutrition.nformat(n,"nf_calories_from_fat"),
                               fat = nutrition.nformat(n,"nf_total_fat"), 
                               fatdv = nutrition.nformat(n,"nf_total_fat",65), 
                               satfat = nutrition.nformat(n,"nf_saturated_fat"), 
                               satfatdv = nutrition.nformat(n,"nf_saturated_fat",20),
                               transfat = nutrition.nformat(n,"nf_trans_fatty_acid"),
                               cholesterol = nutrition.nformat(n,"nf_cholesterol"),
                               cholesteroldv = nutrition.nformat(n,"nf_cholesterol",300),
                               sodium = nutrition.nformat(n,"nf_sodium"),
                               sodiumdv = nutrition.nformat(n,"nf_sodium",2400),
                               carb = nutrition.nformat(n,"nf_total_carbohydrate"), 
                               carbdv = nutrition.nformat(n,"nf_total_carbohydrate",300), 
                               df = nutrition.nformat(n,"nf_dietary_fiber"), 
                               sugar = nutrition.nformat(n,"nf_sugars"),
                               protein = nutrition.nformat(n,"nf_protein"),
                               proteindv = nutrition.nformat(n,"nf_protein",50),
                               vitA = nutrition.nformat(n,"nf_vitamin_a_dv"),
                               vitC = nutrition.nformat(n,"nf_vitamin_a_dv"),
                               calcium = nutrition.nformat(n,"nf_calcium_dv"),
                               iron= nutrition.nformat(n,"nf_iron_dv"),
                               allergens = allergen
                           )