def recipe(): # User reached route via POST (as by submitting a form via POST) if request.method == "GET": # Get the recipe id idr = request.args.get("id") # Lookup the required data with the recipe recipe = lookup(idr) # Get the recipe image and name data = database.get_recipe(idr) # Get the data from favorites table in database if the data is not in meals table if not data: data = database.get_fav_idr(session['user_id'], idr) # If meal already in favorites, display unfavorite instead of favorite favorite = False if "user_id" in session: check = database.get_fav_idr(session['user_id'], idr) if check: favorite = True # Renders template with variables return render_template("recipe.html", recipe=recipe, data=data, id=idr, favorite=favorite) else: return redirect("/")
def add_or_delete_recipe(rid): """ POST lisää kirjautuneelle käyttäjälle uuden suosikkireseptin. DELETE poistaa suosikkireseptin. """ if request.method == DELETE: db.delete_recipe_from_user(g.user["_id"], rid) return json() recipe = db.get_recipe(rid) if not recipe: return json("fail", {"rid": "recipe not found"}) recipe = {"rid": recipe["_id"], "name": recipe["name"]} db.add_fav_to_user(g.user["_id"], recipe) return json()
def recette(recipe_id = None): if recipe_id and request.method != 'POST': app.logger.debug('Recipe ID' + str(recipe_id)) recipe = get_recipe(recipe_id) if not recipe: app.logger.debug('404 NOT FOUND') abort(404) app.logger.debug(recipe['name']) return render_template('recette.html', recipe = recipe) elif recipe_id and request.method == 'POST' and "_method" not in request.form.keys(): app.logger.debug(request.form) new_comment = { "author": request.form['author'], "date": date.today().strftime("%d-%m-%Y"), "content": request.form['content'] } app.logger.debug(new_comment) add_comment(recipe_id, new_comment) recipe = get_recipe(recipe_id) if not recipe: app.logger.debug('404 NOT FOUND') abort(404) app.logger.debug(recipe['comments']) return render_template('recette.html', recipe = recipe, add = True) elif recipe_id and (("_method" in request.form.keys() and request.form['_method'] == 'PUT') or request.method == 'PUT'): app.logger.debug(request.form) if len(request.form['old_date']) > 10: new_date = request.form['old_date'][:10] + " (modifié le " + date.today().strftime("%d-%m-%Y") + ")" elif len(request.form['old_date']) == 10: new_date = request.form['old_date'] + " (modifié le " + date.today().strftime("%d-%m-%Y") + ")" edited_comment = { "author": request.form['author'], "date": new_date, "content": request.form['new_content'] } edit_comment(recipe_id, edited_comment, request.form['old_date'], request.form['old_content']) recipe = get_recipe(recipe_id) if not recipe: app.logger.debug('404 NOT FOUND') abort(404) return render_template('recette.html', recipe = recipe, edit = True) elif recipe_id and (("_method" in request.form.keys() and request.form['_method'] == 'DELETE') or request.method == 'DELETE'): app.logger.debug(request.form) deleted_comment = { "author": request.form['author'], "date": request.form['date'], "content": request.form['content'] } delete_comment(recipe_id, deleted_comment) recipe = get_recipe(recipe_id) if not recipe: app.logger.debug('404 NOT FOUND') abort(404) return render_template('recette.html', recipe = recipe, delete = True) app.logger.debug('404 NOT FOUND') abort(404)
def bites(): """ GET palauttaa kirjautuneen käyttäjän annokset annetulta aikaväliltä. URL-parametrit: - start: inklusiivinen alkupäivämäärä (YYYYmmdd) - end: inklusiivinen loppupäivämäärä (YYYYmmdd) POST lisää uuden annoksen. POST-parametrit: - fid: elintarvikkeen id (ei pakollinen, jos rid on määritelty) - rid: reseptin nimi (ei pakollinen, jos fid on määritelty) - amount: määrä grammoissa - date: päivämäärä (YYYYmmdd) Huom! Jos sekä fid että rid ovat määritelty, käytetään fid:tä. """ if request.method == GET: try: start = datetime.strptime(request.args.get("start"), DATEFORMAT) end = datetime.strptime(request.args.get("end"), DATEFORMAT) except TypeError: return json("fail", {"parameters": "missing date parameters"}) except ValueError: return json("fail", {"parameters": "invalid date parameters"}) return json(data=db.get_bites_by_user(g.user["_id"], start, end)) # POST - lisätään annos: try: amount = int(request.form["amount"]) bite = { "uid": g.user["_id"], "fid": request.form["fid"], "amount": amount, "date": datetime.strptime(request.form["date"], DATEFORMAT) } if "fid" in request.form: bite["fid"] = request.form["fid"] else: bite["rid"] = request.form["rid"] except KeyError: return json("fail", {"parameters": "missing parameters"}) except ValueError: return json("fail", {"parameters": "invalid parameters"}) if "fid" in bite: food = scraper.get_food(bite["fid"]) if not food: return json("fail", {"fid": "food not found"}) bite["name"] = food["name"] for p1, p2 in BASIC_STATS: bite[p1] = round(food["data"][p2][0] / 100.0 * amount) else: recipe = db.get_recipe(bite["rid"]) if not recipe: return json("fail", {"rid": "recipe not found"}) bite["name"] = recipe["name"] for p1, p2 in BASIC_STATS: bite[p1] = round(recipe[p1] / 100.0 * amount) db.add_bite(bite) return json()